一、问题代码
我想改变导出的单元格的样式,按照如下代码操作之后,导出excel发现其他的样式也变化了,百思不得其解
HSSFRow row = sheet.getRow(1);
HSSFCellStyle cellStyle = row.getCell(1).getCellStyle();
Font f = wb.createFont();
f.setFontHeightInPoints((short) 10);
cellStyle.setFont(f);
二、解决办法
代码如下,使用 cloneStyleFrom方法克隆样式,然后根据需求修改样式,再给单元格设置新的样式:
HSSFRow row = sheet.getRow(1);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.cloneStyleFrom(row.getCell(1).getCellStyle());
Font f =wb.createFont();
font.setFontHeightInPoints((short) 10);
cellStyle.setFont(font);
row.getCell(1).setCellStyle(cellStyle);