apache poi将一种样式应用于不同的工作簿

时间:2022-04-29 16:09:59

I'm trying to apply one cell style to defferent woekbooks. It works well, when I apply it to first workbook, but when I'm trying to do this with second and next workbooks - no style is applied and the following exception is thrown.

我想把一个单元格样式应用到其他的woekbooks上。当我将它应用到第一个工作簿时,它运行得很好,但是当我尝试使用第二个和下一个工作簿时——没有应用样式,并且抛出了下面的异常。

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)

The following code is used:

使用以下代码:

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);
            }
        }
    }  

Thanks everybody in advance!

提前谢谢大家!

1 个解决方案

#1


17  

You can't do that, CellStyle objects are specific to one workbook. They're quite deep objects, and much of the style is held in the workbook, so you can't just re-use. You even get a helpful exception which explains this to you!

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

What you need to do instead is to use the cloneStyleFrom(CellStyle) method, to copy the details of the style over. Something like:

相反,您需要使用cloneStyleFrom(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"));

#1


17  

You can't do that, CellStyle objects are specific to one workbook. They're quite deep objects, and much of the style is held in the workbook, so you can't just re-use. You even get a helpful exception which explains this to you!

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

What you need to do instead is to use the cloneStyleFrom(CellStyle) method, to copy the details of the style over. Something like:

相反,您需要使用cloneStyleFrom(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"));