Java ApachePOI将一种样式应用于不同的工作簿

Java ApachePOI将一种样式应用于不同的工作簿,java,excel,apache-poi,Java,Excel,Apache Poi,我试着将一个单元格的样式应用于不同的Woerkbooks。当我将其应用于第一个工作簿时,它工作得很好,但当我尝试对第二个和下一个工作簿执行此操作时,没有应用任何样式,并且引发以下异常 Exception in thread "Thread-3" java.lang.IllegalArgumentException: This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign

我试着将一个单元格的样式应用于不同的Woerkbooks。当我将其应用于第一个工作簿时,它工作得很好,但当我尝试对第二个和下一个工作簿执行此操作时,没有应用任何样式,并且引发以下异常

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);
            }
        }
    }  
public void Professor课程表(教授){
工作簿wb=新XSSFWorkbook();
Sheet Sheet=wb.createSheet(TextConstants.SCHEDULE);
字符串safeName=WorkbookUtil.createSafeSheetName(professor.toString()+“.xlsx”);
LinkedHashMap professorSchedule=data.getSchedule().getProfessorSchedule(professor);
填写教授课程表(第页,教授课程表);
applyStyle(wb,表格);
试一试{
FileOutputStream FileOutputStream=新的FileOutputStream(settings.getSchedulesPath()+safeName);
write(fileOutputStream);
fileOutputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
专用作废应用程序样式(工作簿wb,工作表){
CellStyle=wb.createCellStyle();
style.setWrapText(true);
int columnNumber=0;
sheet.autoSizeColumn(列号);
用于(行:行){
用于(单元格:行){
cell.setCellStyle(style);
sheet.setColumnWidth(columnNumber++,5000);
}
}
}  

提前谢谢大家

您不能这样做,CellStyle对象特定于一个工作簿。它们是很深的对象,而且大部分样式都保存在工作簿中,因此不能重复使用。你甚至会得到一个有用的例外,它可以向你解释这一点

相反,您需要做的是使用该方法,将样式的细节复制过来。比如:

Workbook wb = WorkbookFactory.create(new File("existing.xls"));
CellStyle origStyle = wb.getCellStyleAt(1); // Or from a cell

Workbook newWB = new XSSFWorkbook();
Sheet sheet = newWB.createSheet();
Row r1 = sheet.createRow(0);
Cell c1 = r1.createCell(0);

CellStyle newStyle = newWB.createCellStyle();
newStyle.cloneStyleFrom(origStyle);
c1.setCellStyle(newStyle);

newWB.write(new FileOutpuStream("new.xlsx"));

但正如你所看到的,我为每本书创建了一种新的风格。事实上,我不知道是否有任何样式创建,我不认为您提供的qpproach适合我的情况。那么,是否可以为每本书创建一种新的风格呢?我的代码有什么错误?非常感谢您的帮助。您需要确保仅在为其创建的工作簿中使用样式。您可以为每个工作簿创建一次样式(并跟踪!),也可以为一个工作簿创建/加载样式,稍后再克隆