在合并两个excel文件

时间:2015-06-19 16:24:16

标签: java excel apache-poi

当我合并它们时,我试图在两个excel文件之间添加一个标题。这两个文件有表,我可以将它们合并在一起,但我无法弄清楚如何在这两个表之间添加标题。 我的代码中负责合并工作表的部分看起来像这样;

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

        // This parameter is for appending sheet rows to mergedSheet in the end
        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);
                }
                org.apache.poi.ss.usermodel.Cell 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) {
            }

        }
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

可能不是你想要的,但你可以创建一个只有标题的第三个excel文件。