Excel使用apache poi库写入某些行后丢失样式和格式

sdnqo3pr  于 2022-12-24  发布在  Apache
关注(0)|答案(1)|浏览(354)

我正在尝试创建Excel文件(.xls)在编写总计9000行的excel时,行号3000后所有的样式和格式都丢失了。我曾试图在使用workbook.getNumCellStyles编写完整的工作簿后获得单元格样式的数量(),返回2091。很明显,未超出单元格样式的数量(即小于4000),但样式仍然丢失。请帮助我理解为什么会发生这种情况?
我已经创建了一个示例代码,下面这失败,即使我正在创建75个CellStyle对象。

public class ExcelSheet {
    static Workbook workbook;
    static Sheet spreadsheet;
    static CellStyle tempCellStyle;
    static Map<Integer, CellStyle> styles = new HashMap<Integer, CellStyle>();
    static boolean flag = false;

    public static void main(String[] args) throws IOException {
        workbook = new HSSFWorkbook();
        spreadsheet = workbook.createSheet(" Employee Info ");
        Row row;
        System.out.println(" Writing data to excel...");
        for (int i = 1; i < 500; i++) {
            row = spreadsheet.createRow(i);
            createCells(row, i);
        }
        for (int i = 0; i < 10; i++) {
            spreadsheet.setColumnWidth(i, 4500);
        }
        FileOutputStream out = new FileOutputStream(new File("E:\\Dan\\Writesheet.xls"));
        System.out.println(" No. of cell styles : " + workbook.getNumCellStyles());
        System.out.println(" No. of font styles : " + workbook.getNumberOfFonts());
        workbook.write(out);
        out.close();
    }

    private static void createCells(Row row, int rowNo) {
        for (int i = 0; i < 10; i++) {
            Cell cell = row.createCell(i);
            try {
                writeData(cell, i, rowNo);
            } catch (Exception e) {
                System.out.println("Exception : " + e);
            }
        }
    }

    private static void writeData(Cell cell, int i, int rowNo) throws Exception {
        CellStyle style;
        if (workbook.getNumCellStyles() < 75) {
            style = createStyleObject();
        } else {
            style = null;
        }

        switch (i) {
        case 0:
            if (style == null) {
                style = styles.get(0);
            } else {
                styles.put(0, style);
            }
            cell.setCellValue(rowNo);
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            cell.setCellStyle(style);
            break;
        case 1:
            if (style == null) {
                style = styles.get(1);
            } else {
                styles.put(1, style);
            }
            cell.setCellValue("Test Data " + rowNo);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellStyle(style);
            styles.put(0, style);
            break;
        case 2:
            if (style == null) {
                style = styles.get(2);
            } else {
                styles.put(2, style);
            }
            DataFormat poiFormat = spreadsheet.getWorkbook().createDataFormat();
            String excelFormatPattern = DateFormatConverter.convert(Locale.US, "yyyy-MM-dd");
            style.setDataFormat(poiFormat.getFormat(excelFormatPattern));
            cell.setCellValue(new Date(rowNo % 10, rowNo % 12, rowNo % 28, rowNo % 12, rowNo % 60, rowNo % 60));
            cell.setCellStyle(style);
            break;
        case 3:
            if (style == null) {
                style = styles.get(3);
            } else {
                styles.put(3, style);
            }
            cell.setCellValue(rowNo + rowNo);
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            cell.setCellStyle(style);
            break;
        case 4:
            if (style == null) {
                style = styles.get(4);
            } else {
                styles.put(4, style);
            }
            cell.setCellValue("Sample Text " + rowNo);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellStyle(style);
            break;
        case 5:
            if (style == null) {
                style = styles.get(5);
            } else {
                styles.put(5, style);
            }
            DataFormat poiFormat1 = spreadsheet.getWorkbook().createDataFormat();
            String excelFormatPattern1 = DateFormatConverter.convert(Locale.US, "yyyy-MM-dd");
            style.setDataFormat(poiFormat1.getFormat(excelFormatPattern1));
            cell.setCellValue(new Date(rowNo % 18, rowNo % 12, rowNo % 28, rowNo % 12, rowNo % 60, rowNo % 60));
            cell.setCellStyle(style);
            break;
        case 6:
            if (style == null) {
                style = styles.get(6);
            } else {
                styles.put(6, style);
            }
            cell.setCellValue(rowNo + i);
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            cell.setCellStyle(style);
            break;
        case 7:
            if (style == null) {
                style = styles.get(7);
            } else {
                styles.put(7, style);
            }
            cell.setCellValue("Exports Data " + rowNo);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellStyle(style);
            break;
        case 8:
            if (style == null) {
                style = styles.get(8);
            } else {
                styles.put(8, style);
            }
            DataFormat poiFormat2 = spreadsheet.getWorkbook().createDataFormat();
            String excelFormatPattern2 = DateFormatConverter.convert(Locale.US, "yyyy-MM-dd");
            style.setDataFormat(poiFormat2.getFormat(excelFormatPattern2));
            cell.setCellValue(new Date(rowNo % 25, rowNo % 12, rowNo % 28, rowNo % 12, rowNo % 60, rowNo % 60));
            cell.setCellStyle(style);
            break;
        case 9:
            if (style == null) {
                style = styles.get(9);
            } else {
                styles.put(9, style);
            }
            DataFormat poiFormat3 = spreadsheet.getWorkbook().createDataFormat();
            String excelFormatPattern3 = DateFormatConverter.convert(Locale.US, "yyyy-MM-dd");
            style.setDataFormat(poiFormat3.getFormat(excelFormatPattern3));
            cell.setCellValue(new Date());
            cell.setCellStyle(style);
            break;

        default:
            if (style == null) {
                style = styles.get(7);
            } else {
                styles.put(7, style);
            }
            cell.setCellValue("Default Value " + rowNo);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellStyle(style);
            break;
        }
    }

    private static CellStyle createStyleObject() {
        Workbook workbook = spreadsheet.getWorkbook();
        CellStyle style = (HSSFCellStyle) workbook.createCellStyle();
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFillForegroundColor((short) 67);
        style.setWrapText(true);
        return style;
    }

}
r6hnlfcb

r6hnlfcb1#

不要为工作簿中的每个单元格创建CellStyle。而是准备所有可能的和唯一的CellStyle并重用它们。例如:

var cellStyle = createStyleObject();

// ... some customization of the above style

for (int i = 0; i < 10; i++) {
     Cell cell = row.createCell(i);
     cell.setStyle(cellStyle);
}

相关问题