10
我试图将一种单元格样式应用于不同的woekbooks。它运行良好,当我将它应用到第一个工作簿时,但是当我试图用第二个和下一个工作簿来完成时 - 没有应用样式并引发以下异常。apache poi将一种样式应用于不同的工作簿
Exception in thread "Thread-3" java.lang.IllegalArgumentException: This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook?
at org.apache.poi.xssf.usermodel.XSSFCellStyle.verifyBelongsToStylesSource(XSSFCellStyle.java:118)
at org.apache.poi.xssf.usermodel.XSSFCell.setCellStyle(XSSFCell.java:500)
at CoreLayer.ExportManager.ExcelExproter.applyStyle(ExcelExproter.java:224)
at CoreLayer.ExportManager.ExcelExproter.groupSchedule(ExcelExproter.java:47)
at UILayer.ExportDialog$ExportWorker.run(ExportDialog.java:111)
at java.lang.Thread.run(Thread.java:722)
下面的代码被用于:
public void professorSchedule(Professor professor) {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet(TextConstants.SCHEDULE);
String safeName = WorkbookUtil.createSafeSheetName(professor.toString() + ".xlsx");
LinkedHashMap<ScheduleSlot, Lesson> professorSchedule = data.getSchedule().getProfessorSchedule(professor);
fillProfessorSchedule(sheet, professorSchedule);
applyStyle(wb, sheet);
try {
FileOutputStream fileOutputStream = new FileOutputStream(settings.getSchedulesPath() + safeName);
wb.write(fileOutputStream);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void applyStyle(Workbook wb, Sheet sheet) {
CellStyle style = wb.createCellStyle();
style.setWrapText(true);
int columnNumber = 0;
sheet.autoSizeColumn(columnNumber);
for (Row row : rows) {
for (Cell cell : row) {
cell.setCellStyle(style);
sheet.setColumnWidth(columnNumber++, 5000);
}
}
}
谢谢大家提前!
但正如您所看到的,我为每本书创建了一种新风格。其实我不知道是否有任何风格创造,我不认为你提供的qpproach是适合我的情况。那么,是否有可能为每本书创建一种新风格?我的代码有什么错误?非常感谢您的帮助。 –
您需要确保您只使用您创建工作簿的样式。您可以为每个工作簿创建一次样式(并跟踪!),也可以为一个工作簿创建/加载样式并在以后进行克隆 – Gagravarr