Java Apache POI - 单元格格式化停止并重新开始工作
Java Apache POI - Cell Formatting Stops and Restarts Working
我有一个程序可以获取输入文件并将其放入 excel。一切正常,直到第 32000 行左右。左对齐格式突然停止工作。此外,它似乎突然重新开始在第 65000 行附近工作。我不确定发生了什么或为什么。我已经提供了格式化发生的函数的代码。
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...");
问题是您每行创建一个单元格样式,这不是您应该做的事情。 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);
这将防止您 运行 超出可用的单元格样式,因此应该允许您的程序正常工作
我有一个程序可以获取输入文件并将其放入 excel。一切正常,直到第 32000 行左右。左对齐格式突然停止工作。此外,它似乎突然重新开始在第 65000 行附近工作。我不确定发生了什么或为什么。我已经提供了格式化发生的函数的代码。
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...");
问题是您每行创建一个单元格样式,这不是您应该做的事情。 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);
这将防止您 运行 超出可用的单元格样式,因此应该允许您的程序正常工作