I'm using Apache POI to read an existing template excel file and would like to copy the existing styles in some header rows and apply them to new cells.
我正在使用Apache POI读取现有模板excel文件,并希望复制某些标题行中的现有样式并将它们应用于新单元格。
It seems like the existing formatting is not being applied (IE, Date, Currency, Percentage etc).
似乎没有应用现有格式(IE,日期,货币,百分比等)。
The code is pretty basic:
代码非常基本:
//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);
It does copy the font and background color etc but it seems like the formatting is lost (all cells have "general" formatting applied).
它会复制字体和背景颜色等,但似乎格式化丢失(所有单元格都应用了“常规”格式)。
Also when I do this:
当我这样做时:
currentStyle.getDataFormat(); // always 0
So it makes me think that I'm not reading the formatting correctly. Any ideas on how to accomplish this?
所以它让我觉得我没有正确阅读格式。有关如何实现这一目标的任何想法?
1 个解决方案
#1
0
OK I figured it out, it was my mistake. I was reading the style from the first cell in the row and applying it to all, instead of reading from each cell.
好吧我明白了,这是我的错。我正在从行中的第一个单元格中读取样式并将其应用于所有单元格,而不是从每个单元格中读取。
This fixes it
这解决了它
Cell existingCell = existingRow.getCell(i);
#1
0
OK I figured it out, it was my mistake. I was reading the style from the first cell in the row and applying it to all, instead of reading from each cell.
好吧我明白了,这是我的错。我正在从行中的第一个单元格中读取样式并将其应用于所有单元格,而不是从每个单元格中读取。
This fixes it
这解决了它
Cell existingCell = existingRow.getCell(i);