java apache poi 将一种样式应用于不同的工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10773961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
apache poi apply one style to different workbooks
提问by mr.nothing
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)
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!
提前谢谢大家!
回答by Gagravarr
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"));