我的解决方案
我已经通过的要求缩小解决我的问题。现在我只专注于XSSFWorkbook
s。
正确复制XSSFWorkbook
真的很简单。要将XSSFCellStyle
复制到另一个工作簿中只使用下面的代码:
// Copy cell style from `sourceCell` to `targetCell`
XSSFCellStyle sourceCellStyle = sourceCell.getCellStyle();
XSSFCellStyle clonedCellStyle = newWorkbook.createCellStyle();
clonedCellStyle.cloneStyleFrom(sourceCellStyle);
targetCell.setCellStyle(clonedCellStyle);
重要的是,在目标工作簿具有相同的样式源作为源工作簿。否则,克隆的单元格样式会有所不同。
final StylesTable sourceStylesSource = sourceWorkbook.getStylesSource();
final StylesTable tagetStylesSource = targetWorkbook.getStylesSource();
sourceStylesSource.getFonts().forEach(font -> targetStylesSource.putFont(font, true));
sourceStylesSource.getFills().forEach(fill -> targetStylesSource.putFill(new XSSFCellFill(fill.getCTFill())));
sourceStylesSource.getBorders().forEach(border -> targetStylesSource.putBorder(new XSSFCellBorder(border.getCTBorder())));
我希望这可以帮助别人!
问候,winklerrr
PS
如果有人不能缩减的要求的话,也许是有帮助的任务分成两个较小的任务:
- 转换一个
HSSFWorkbook
到一个XSSFWorkbook
,反之亦然。
- 准确复制
XSSFWorkbook
。
如果你喜欢把它变成一个独立的例子[去Apache POI的例子部分](https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf),在[增强bug](https://bz.apache.org/bugzilla/enter_bug.cgi?product=POI)或[GitHub pull request](https:/ /github.com/apache/poi),我们很乐意添加它! – Gagravarr