使用java合并和编辑Excel文件

使用java合并和编辑Excel文件,java,apache-poi,Java,Apache Poi,我目前正在尝试合并两个Excel文件。但是有一个问题,我需要在文件之间添加文件名,每个文件和一个空行。当我尝试这样做时,它添加了文件名和空行,但重写了每个文件的前两行。如何在不丢失文件前两行的情况下执行此操作 public static void main(String[] args) { try { File firstFile = new File("/Users/TLQ/Desktop/report-12689-M2398-1.xlsx"); Fi

我目前正在尝试合并两个Excel文件。但是有一个问题,我需要在文件之间添加文件名,每个文件和一个空行。当我尝试这样做时,它添加了文件名和空行,但重写了每个文件的前两行。如何在不丢失文件前两行的情况下执行此操作

public static void main(String[] args) {

    try {
        File firstFile = new File("/Users/TLQ/Desktop/report-12689-M2398-1.xlsx");
        File secondFile = new File("/Users/TLQ/Desktop/report-12695-M2390-1.xlsx");
        FileInputStream excellFile1 = new FileInputStream(firstFile);
        FileInputStream excellFile2 = new FileInputStream(secondFile);

        org.apache.poi.ss.usermodel.Workbook workbook1 = WorkbookFactory.create(excellFile1);
        org.apache.poi.ss.usermodel.Workbook workbook2 = WorkbookFactory.create(excellFile2);


        org.apache.poi.ss.usermodel.Sheet sheet1a = workbook1.getSheetAt(0);
        org.apache.poi.ss.usermodel.Sheet sheet2a = workbook2.getSheetAt(0);


        addSheet(sheet1a, sheet2a,secondFile.getAbsolutePath().substring(secondFile.getAbsolutePath().lastIndexOf("/")+1, secondFile.getAbsolutePath().indexOf(".")));
        excellFile1.close();

        // save merged file
        File mergedFile = new File(
        "/Users/TLQ/Desktop/Albert_1.xlsx");
        if (!mergedFile.exists()) {
            mergedFile.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(mergedFile);
        workbook1.write(out);
        out.close();
        System.out.println(firstFile.getAbsolutePath().substring(firstFile.getAbsolutePath().lastIndexOf("/")+1, firstFile.getAbsolutePath().indexOf(".")));


    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static void addSheet(org.apache.poi.ss.usermodel.Sheet mergedSheet, org.apache.poi.ss.usermodel.Sheet sheet,String name) {
    // map for cell styles
    Map<Integer, org.apache.poi.ss.usermodel.CellStyle> styleMap = new HashMap<Integer, org.apache.poi.ss.usermodel.CellStyle>();
    org.apache.poi.ss.usermodel.Row row2 = mergedSheet.createRow((short) 0);

    org.apache.poi.ss.usermodel.Cell mcell2 = row2.createCell((short) 0);
    mcell2.setCellValue("report-12689-M2398-1.xlsx");
    // This parameter is for appending sheet rows to mergedSheet in the end
    org.apache.poi.ss.usermodel.Row row1 = sheet.createRow((short) 0);

    org.apache.poi.ss.usermodel.Cell mcell = row1.createCell((short) 0);
    mcell.setCellValue(name);

    org.apache.poi.ss.usermodel.Row row1Empty = sheet.createRow((short) 0);
    org.apache.poi.ss.usermodel.Cell mcellEmpty = row1.createCell((short) 0);

    int len = mergedSheet.getLastRowNum();
    for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {

        org.apache.poi.ss.usermodel.Row row = sheet.getRow(j);
        org.apache.poi.ss.usermodel.Row mrow = mergedSheet.createRow(len + j + 1);
        try {
            for (int k = row.getFirstCellNum(); k < row.getLastCellNum(); k++) {
                org.apache.poi.ss.usermodel.Cell cell=null;
                if(row.getCell(k)!=null){
                    cell = row.getCell(k);
                }else{
                    cell= row.createCell(k);
                }
                mcell = mrow.createCell(k);

                if (cell.getSheet().getWorkbook() == mcell.getSheet()
                        .getWorkbook()) {
                    mcell.setCellStyle(cell.getCellStyle());
                } else {
                    int stHashCode = cell.getCellStyle().hashCode();
                    org.apache.poi.ss.usermodel.CellStyle newCellStyle = styleMap.get(stHashCode);
                    if (newCellStyle == null) {
                        newCellStyle = mcell.getSheet().getWorkbook()
                        .createCellStyle();
                        newCellStyle.cloneStyleFrom(cell.getCellStyle());
                        styleMap.put(stHashCode, newCellStyle);
                    }
                    mcell.setCellStyle(newCellStyle);
                }

                switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_FORMULA:
                    mcell.setCellFormula(cell.getCellFormula());
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    mcell.setCellValue(cell.getNumericCellValue());
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    mcell.setCellValue(cell.getStringCellValue());
                    break;
                case HSSFCell.CELL_TYPE_BLANK:
                    mcell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    mcell.setCellValue(cell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    mcell.setCellErrorValue(cell.getErrorCellValue());
                    break;
                default:
                    mcell.setCellValue(cell.getStringCellValue());
                    break;
                }
            }
        } catch (Exception e) {
        }

    }
}
publicstaticvoidmain(字符串[]args){
试一试{
File firstFile=新文件(“/Users/TLQ/Desktop/report-12689-M2398-1.xlsx”);
File secondFile=新文件(“/Users/TLQ/Desktop/report-12695-M2390-1.xlsx”);
FileInputStream ExcelFile1=新的FileInputStream(第一个文件);
FileInputStream ExcelFile2=新的FileInputStream(第二个文件);
org.apache.poi.ss.usermodel.Workbook workbook1=WorkbookFactory.create(ExcelFile1);
org.apache.poi.ss.usermodel.Workbook workbook2=WorkbookFactory.create(ExcelFile2);
org.apache.poi.ss.usermodel.sheet1a=workbook1.getSheetAt(0);
org.apache.poi.ss.usermodel.sheet2a=workbook2.getSheetAt(0);
addSheet(sheet1a,sheet2a,secondFile.getAbsolutePath()子字符串(secondFile.getAbsolutePath().lastIndexOf(“/”)+1,secondFile.getAbsolutePath().indexOf(“.”));
ExcelFile1.close();
//保存合并文件
文件合并文件=新文件(
“/Users/TLQ/Desktop/Albert_1.xlsx”);
如果(!mergedFile.exists()){
mergedFile.createNewFile();
}
FileOutputStream out=新的FileOutputStream(合并文件);
工作手册1.写(出);
out.close();
System.out.println(firstFile.getAbsolutePath().substring(firstFile.getAbsolutePath().lastIndexOf(“/”)+1,firstFile.getAbsolutePath().indexOf(“.”));
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void addSheet(org.apache.poi.ss.usermodel.Sheet mergedSheet,org.apache.poi.ss.usermodel.Sheet,字符串名称){
//单元格样式的映射
Map styleMap=newhashmap();
org.apache.poi.ss.usermodel.row2=mergedSheet.createRow((短)0);
org.apache.poi.ss.usermodel.Cell mcell2=row2.createCell((短)0);
mcell2.setCellValue(“报告-12689-M2398-1.xlsx”);
//此参数用于在最后将图纸行追加到合并图纸
org.apache.poi.ss.usermodel.Row行1=sheet.createRow((短)0);
org.apache.poi.ss.usermodel.Cell-mcell=row1.createCell((短)0);
mcell.setCellValue(名称);
org.apache.poi.ss.usermodel.Row row1Empty=sheet.createRow((短)0);
org.apache.poi.ss.usermodel.Cell mcellEmpty=row1.createCell((短)0);
int len=mergedSheet.getLastRowNum();

对于(int j=sheet.getFirstRowNum();j不确定哪一个是好的,但是试试看
workbook1.写出(out,true);
workbook1.写入(out,false);
它应该设置删除之前的内容或不删除之前的内容的事实。(true不应该是删除您得到的内容)

希望能有所帮助;)