我正在使用Apache POI读取一个现有的模板Excel文件,并希望将一些标题行中的现有样式复制并应用于新单元格。
看起来现有的格式没有被应用(例如日期、货币、百分比等)。
代码非常基本:
所以这让我觉得我没有正确阅读格式。有什么想法可以实现这个吗?
看起来现有的格式没有被应用(例如日期、货币、百分比等)。
代码非常基本:
//read existing style
Row existingRow = sheet.getRow(headerRowIndex);
Cell existingCell = existingRow.getCell(0);
CellStyle currentStyle = existingCell.getCellStyle();
//apply date style here
Date date = StringUtil.toDate(map.get(column.getHeaderName()));
cell.setCellValue(date);
//apply previous style
cell.setCellStyle(currentStyle);
它会复制字体、背景颜色等,但似乎格式会丢失(所有单元格都应用了“常规”格式)。
还有,当我这样做时:
currentStyle.getDataFormat(); // always 0
所以这让我觉得我没有正确阅读格式。有什么想法可以实现这个吗?