poi导出excel文件,单元数值格式用office和wps打开显示不一致

炎发灼眼讨伐者 2017-03-22 02:51:01
一个web项目使用poi导出页面数据为excel,客户要求金额在excel中用数值格式而不是文本,所以使用cellstyle把当前单元格式进行了转换

private HSSFCellStyle cellStyle;
private HSSFDataFormat df;
cellStyle = wb.createCellStyle();
df = wb.createDataFormat();
cellStyle.setDataFormat(df.getBuiltinFormat("#,##0.00"));
conCell.setCellStyle(cellStyle);
conCell.setCellValue(Double.parseDouble(t.getText().replaceAll(",", "")));

一切都安装计划一样,完美无缺,导出的文件打开后虽然显示的是货币格式,但是也满足了需求;
可是,当客户使用这个功能导出时,却发现只有前面两行的金额是正常显示,后面的都是文本,而且没有格式,如图:
这是用office打开的效果,如果我用wps打开则完全没问题,
后来我把这段创建collStyle的代码放在for循环外面,导出的excel用office打开后也能实现数值的效果。
完全想不明白是为什么,有没有大神解释一下。下面附上poi导出的代码
private HSSFWorkbook wb;
private HSSFSheet sheet;
private int column;
private List contentLi;
private HSSFCellStyle cellStyle;
private HSSFDataFormat df;
public void createExcel(Document doc, OutputStream out) {
try {
init();
List listTR = doc.getRootElement().getChild("TBODY").getChildren();
// 这几句设置格式的代码之前放在for循环里,导致导出的excel如果用offic打开的话就只有前面两行的格式正确
cellStyle = wb.createCellStyle();
df = wb.createDataFormat();
cellStyle.setDataFormat(df.getBuiltinFormat("#,##0.00"));
for (int i = 0; i < listTR.size(); ++i) {
HSSFRow row = this.sheet.createRow((short) i);
List list = ((Element) listTR.get(i)).getChildren();
this.column = 0;
for (int j = 0; j < list.size(); ++j) {
Element t = (Element) list.get(j);
directColumon(row.getRowNum());
HSSFCell conCell = row.createCell((short) this.column);
conCell.setEncoding((short) 1);
addMergedRegion(i, t);
fillCell(conCell, t);
}
}
this.wb.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void fillCell(HSSFCell conCell, Element t) {
String value = t.getText();
if ("TH".equals(t.getName())) {
conCell.setCellValue(value);
conCell.setCellStyle(getCellStyle(4));
} else if ("money".equals(t.getAttributeValue("dataType"))) {
if ((value != null) && (!("".equals(value.trim())))) {
String res = Pattern.compile(",").matcher(value).replaceAll("")
.trim();

double stringDouble = Double.valueOf(res).doubleValue();
conCell.setCellValue(stringDouble);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
conCell.setCellStyle(cellStyle);
} else {
conCell.setCellValue("");
}
conCell.setCellStyle(getCellStyle(1));
} else if ("date".equals(t.getAttributeValue("dataType"))) {
conCell.setCellValue(value);
conCell.setCellStyle(getCellStyle(2));
} else if ("int".equals(t.getAttributeValue("dataType"))) {
if ((value != null) && (!("".equals(value.trim()))))
conCell.setCellValue(Double.valueOf(value).doubleValue());
else {
conCell.setCellValue("");
}
conCell.setCellStyle(getCellStyle(3));
} else {
// 如果是金额则设置成数值格式,使用poi的cellstyle来实现
// 但实际导出是货币格式
if (CastUtil.isNumeric(t.getText())) {
// cellStyle = wb.createCellStyle();
// df = wb.createDataFormat();
// cellStyle.setDataFormat(df.getBuiltinFormat("#,##0.00"));
conCell.setCellStyle(cellStyle);
// conCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
conCell.setCellValue(Double.parseDouble(t.getText().replaceAll(",", "")));
} else {
conCell.setCellValue(t.getText());
conCell.setCellStyle(getStyleFromDoc(t));
}
}
}
...全文
1306 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lmfluck 2017-09-05
  • 打赏
  • 举报
回复
我也遇到过这个问题,应该是在循环里每一次都创建了样式,会占用大量的存储空间。导致后面的行没有建立样式,就变成了默认的样式了。
  • 打赏
  • 举报
回复
同问,遇到相同的问题了,但是不知道为啥啊。。。。。。
  • 打赏
  • 举报
回复
我想过会不会是创建cellstyle时出问题了,但是debug之后发现在for循环里的每个cellstyle,除了地址不一样,其他都一模一样

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧