我有一个模板XSSF表 我正在尝试基于模板创建一个SXSSF文件,我可以成功创建工作表,但是当我尝试将样式从旧工作表复制到新工作表时我遇到了问题
这是我的代码
FileInputStream templateFile = null;
XSSFWorkbook templateWorkbook = null;
XSSFSheet templateSheet = null;
templateFile = new FileInputStream(this.templateFilePath);
templateWorkbook = new XSSFWorkbook(templateFile);
templateSheet = templateWorkbook.getSheetAt(0);
SXSSFWorkbook sworkbook = new SXSSFWorkbook(this.largeReportCashRows);
SXSSFSheet sworksheet = (SXSSFSheet) sworkbook.createSheet();
//Create styles
CellStyle newStyle = sworkbook.createCellStyle();
//style.setFillPattern(XSSFCellStyle.FINE_DOTS );
//style.setFillBackgroundColor(IndexedColors.RED.getIndex());
CellStyle oldCellStyle = templateSheet.getRow(0).getCell(0).getCellStyle();
newStyle.cloneStyleFrom(oldCellStyle);
//SOME CODE TO CREATE ROWS & CELLS IN THE new sheet "sworksheet"
Cell cell1;
cell1.setCellStyle(newStyle);
当我执行代码时,它会运行并生成工作表,但是当我打开它时,我收到错误,告诉我工作表已损坏,告诉Excel修复它。它是固定的,但风格不起作用!
请帮忙
答案 0 :(得分:1)
//to get old cell style from a cell
CellStyle oldCellStyle = sheet.getRow(0).getCell(0).getCellStyle();
//to create a new sheet in which you want to use this style
XSSFSheet sheet2 = workbook.createSheet("new2");
row=sheet2.createRow(0);
cell=row.createCell(0);
cell.setCellValue("test");
//to set old style to new cell
cell.setCellStyle(oldCellStyle);
FileOutputStream out1 = new FileOutputStream("D:\\test.xlsx");
workbook.write(out1);
out1.close();