clonejoy 2018-12-21 23:59 采纳率: 0%
浏览 479

请问我这个java代码怎么添加poi包的代码才能实现删除excel文件的第一行?

请问我这个java代码怎么添加poi包的代码才能实现删除excel文件的第一行?

package cn.ayee.xhd_Ledger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class NhdLedger {
    public static void main(String[] args) throws IOException {
        File srcFolder = new File("G:\\share");
        File destFolder = new File("G:\\newShare");

        if(!destFolder.isDirectory()) {
            destFolder.mkdir();
        }

        //获取该目录下的被复制文件的File数组
        File[] fileArray = srcFolder.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return new File(dir, name).isFile() && name.endsWith(".xls");
            }
        });
        //遍历被复制文件的File数组,得到每一个被复制文件对象
        for(File file : fileArray) {
            String name = file.getName();   
            File newFile = new File(destFolder, name);
            copyFile(file, newFile);
            File[] destFileArray = destFolder.listFiles();
            //在目的地目录下改名
            //获取复制文件的File数组
            for(File destFile : destFileArray) {
                String newName = destFile.getName().replace(name,"enterbill.xls");
                //封装复制的新文件的路径名
                File destNewFile = new File(destFolder, newName);
                //改名
                destFile.renameTo(destNewFile);
                //加上下面这段POI包的代码就报错
/*      FileInputStream is = new FileInputStream(destNewFile);      
                HSSFWorkbook workbook = new HSSFWorkbook(is);
                HSSFSheet sheet = workbook.getSheetAt(0);
                sheet.shiftRows(1, 1, -1);
                FileOutputStream os = new FileOutputStream(destNewFile);
                workbook.write(os);
                is.close();  
         os.close();*/
            }
        }

    }

    public static void copyFile(File file, File newFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));

        byte[] bys = new byte[1024];
        int len;
        while((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bis.close();
        bos.close();
    }
}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 12:35
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要使用Apache POI库删除Excel文件的第一行,你需要先创建一个新的Excel文件并写入数据。然后,你可以通过读取原始文件中的数据,并将其与新文件中的数据进行比较来删除第一行。

    以下是一个简单的示例:

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.*;
    
    public class Main {
        private static final String EXCEL_FILE_PATH = "your_excel_file.xlsx";
        private static final String DESTINATION_FOLDER = "destination_folder";
    
        public static void main(String[] args) throws IOException {
            deleteFirstRow(EXCEL_FILE_PATH);
    
            File sourceFolder = new File(EXCEL_FILE_PATH);
            File destinationFolder = new File(DESTINATION_FOLDER);
    
            if (!destinationFolder.exists()) {
                destinationFolder.mkdirs();
            }
    
            File[] files = sourceFolder.listFiles();
            if (files == null) {
                System.out.println("No files found in the specified folder.");
                return;
            }
    
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".xlsx")) {
                    FileInputStream fis = new FileInputStream(file);
                    XSSFWorkbook workbook = new XSSFWorkbook(fis);
                    XSSFRow firstRow = workbook.getSheetAt(0).getRow(0);
                    if (firstRow != null) {
                        for (int i = 1; i < firstRow.getLastCellNum(); i++) {
                            XSSFCell cell = firstRow.getCell(i);
                            if (cell != null && !cell.toString().isEmpty()) {
                                CellStyle style = workbook.createCellStyle();
                                style.setDataFormat(workbook.createDataFormat().getFormat("#"));
                                firstRow.removeRow(firstRow.getRow(0));
                                break;
                            }
                        }
                    } else {
                        System.out.println("Error: First row not found.");
                        return;
                    }
                    workbook.close();
    
                    File newFile = new File(destinationFolder + "/" + file.getName());
                    FileOutputStream fos = new FileOutputStream(newFile);
                    workbook.write(fos);
                    fos.close();
                    fis.close();
                }
            }
        }
    
        private static void deleteFirstRow(String excelFilePath) throws IOException {
            File excelFile = new File(excelFilePath);
            Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFile));
            Sheet sheet = workbook.getSheetAt(0);
            Row firstRow = sheet.getRow(0);
            if (firstRow != null) {
                sheet.removeRow(firstRow);
            } else {
                System.out.println("Error: First row not found.");
            }
            workbook.close();
        }
    }
    

    在这个示例中,我们首先创建一个新的Excel文件并将数据写入。然后,我们从原始文件中读取数据,并检查是否有第一行存在。如果存在,我们就将第一行移除。最后,我们将修改后的数据写回文件。

    注意:这只是一个基本的示例,实际应用中可能需要处理更多的情况和异常。

    评论

报告相同问题?