在我的excel中包含1,28,237行和20列的所有列上调用OutOfMemoryError
函数时获取autoColumnWidth
。我使用SXSSF
api并将-Xms2048m -Xmx4096m -XX:PermSize=1024m -XX:MaxPermSize=2048m
分配给JVM但仍未成功。
CPU利用率高达100%,我的系统RAM为6gb,在此过程中使用高达5.8gb,然后我得到OutOfMemoryError
。任何解决方案?
有了这个,我可以想到一个解决方案,即为每列获取一个变量,找到最大长度,并在输入所有数据后最后将列设置为相应的变量。
还有其他解决方案吗?
我的代码创建Excel
public static void createVideodataExcelFile(String excelPath, int maxRiskArea){
FileOutputStream fileOut = null;
XSSFWorkbook workbook = new XSSFWorkbook();
CellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setBold(true);
style.setFont(font);
int cellCount = 0;
try {
File file = new File(excelPath);
if(file.createNewFile()){
SXSSFWorkbook wb = new SXSSFWorkbook(workbook);
wb.setCompressTempFiles(true);
Sheet sheet = wb.createSheet("GLE Video Data");
int rowCount = sheet.getPhysicalNumberOfRows();
//System.out.println("Row Count ="+rowCount);
Row row = sheet.createRow(rowCount);
Cell cell1 = row.createCell(cellCount);
createCell(cell1, row, "CourseContent name", style, cellCount);
createCell(cell1, row, "lang", style, ++cellCount);
createCell(cell1, row, "CourseTitle", style, ++cellCount);
createCell(cell1, row, "Audience", style, ++cellCount);
createCell(cell1, row, "ContentRegion", style, ++cellCount);
createCell(cell1, row, "CourseTitle", style, ++cellCount);
createCell(cell1, row, "DeprecatedId", style, ++cellCount);
createCell(cell1, row, "GleCode", style, ++cellCount);
createCell(cell1, row, "Guid", style, ++cellCount);
createCell(cell1, row, "LearningFormat", style, ++cellCount);
for(int i = 0 ; i < maxRiskArea ; ++i){
createCell(cell1, row, "RiskArea", style, ++cellCount);
}
createCell(cell1, row, "SalesforceId", style, ++cellCount);
createCell(cell1, row, "Setting", style, ++cellCount);
createCell(cell1, row, "SmsCode", style, ++cellCount);
createCell(cell1, row, "pageID", style, ++cellCount);
createCell(cell1, row, "title", style, ++cellCount);
createCell(cell1, row, "ID", style, ++cellCount);
createCell(cell1, row, "media_src", style, ++cellCount);
createCell(cell1, row, "cuePoint", style, ++cellCount);
createCell(cell1, row, "character", style, ++cellCount);
createCell(cell1, row, "line", style, ++cellCount);
fileOut = new FileOutputStream(excelPath);
wb.write(fileOut);
System.out.println("Excel Of GLE Data Created.");
}
} catch (IOException e) {
errorLog.error("Excel Read/Write Error ="+e.getMessage());
throw new GLEException(e);
}
finally{
try {
if(fileOut!=null)
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
errorLog.error("Excel Read/Write Error ="+e.getMessage());
throw new GLEException(e);
}
}
}
private static void createCell(Cell cell , Row row, String name, CellStyle style, int cellCount ){
cell = row.createCell(cellCount);
cell.setCellStyle(style);
cell.setCellValue(name);
}
答案 0 :(得分:0)
尝试在调用autoSizeColumn(columnIndex)之前在SXSSFSheet对象上调用trackAllColumnsForAutoSizing(),它将得到修复