Sto provando ad applicare uno stile di cella a woekbook defferenti. Funziona bene quando lo applico alla prima cartella di lavoro, ma quando sto provando a farlo con la seconda e la successiva cartella di lavoro, non viene applicato alcuno stile e viene generata la seguente eccezione.apache poi applica uno stile a diverse cartelle di lavoro
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)
Il codice seguente viene utilizzato:
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);
}
}
}
Grazie a tutti in anticipo!
ma come puoi vedere creo un nuovo stile per ogni libro. In realtà non so se ci sono stili creati e non penso che il qpproach che hai fornito sia adatto al mio caso. È possibile creare un nuovo stile per ogni libro? E quale errore ha il mio codice? Grazie mille per il tuo aiuto. –
È necessario assicurarsi di utilizzare solo uno stile con la cartella di lavoro per la quale è stato creato. Puoi creare stili una volta per cartella di lavoro (e traccia!), Oppure crearlo/caricarlo per una cartella di lavoro e clonarlo in seguito – Gagravarr