Java Apache POI – 单元格格式化停止并重新启动工作

我有一个程序,需要一个input文件,并将其放入Excel中。 一切都很好,直到第32000行。 突然左alignment格式停止工作。 此外,似乎突然重新开始在第65000行的工作。 我不确定发生了什么事情或为什么。 我提供了格式化function的代码。

private static void addDataToExcel(ArrayList<String[]> rowList, int amtOfColumns){ SXSSFRow row = null; String[] colValues = null; for(int i = 1 ; i<= rowList.size() ;i++){ row = (SXSSFRow) worksheet.createRow(rowCount); //TODO: Check if really necessary since for loop starts at 1 now if(i == 0){ } else { colValues = rowList.get(i-1); } CellStyle style = workbook.createCellStyle(); style.setAlignment(XSSFCellStyle.ALIGN_RIGHT); for(int j=0; j < amtOfColumns; j++){ if(colValues[j] == null){ SXSSFCell cell = (SXSSFCell) row.createCell(j); cell.setCellValue(colValues[j+1]); cell.setCellStyle(style); } else{ //System.out.println(j); SXSSFCell cell = (SXSSFCell) row.createCell(j); cell.setCellValue(colValues[j]); cell.setCellStyle(style); } } rowCount++; } System.out.println("Running..."); 

问题是你每行创build一个单元格样式,这不是你应该做的事情。 Excel对工作簿可以处理的单元格样式的最大数量具有相当低的限制。

由于单元格样式是工作簿的范围,所以您应该将您的代码更改为

 SXSSFRow row = null; String[] colValues = null; CellStyle style = workbook.createCellStyle(); style.setAlignment(XSSFCellStyle.ALIGN_RIGHT); for (int i = 1 ; i<= rowList.size() ; i++) { row = (SXSSFRow) worksheet.createRow(rowCount); 

这将防止你用尽可用的单元格样式,所以应该让你的程序正常工作