apache poi将一种样式应用于不同的工作簿 [英] apache poi apply one style to different workbooks

查看:140
本文介绍了apache poi将一种样式应用于不同的工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一种单元格样式应用于不同的woekbooks.当我将其应用于第一个工作簿时,它工作得很好,但是当我尝试对第二个和下一个工作簿进行此操作时-没有应用任何样式,并且引发了以下异常.

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.

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

谢谢大家!

推荐答案

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

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!

您需要做的是使用

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

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

这篇关于apache poi将一种样式应用于不同的工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆