使用Apache POI进行数据写入时,单元格格式可删除excel单元格中的小数点 [英] Cell formatting to remove decimal point in excel cell when data Writing Using Apache POI

查看:457
本文介绍了使用Apache POI进行数据写入时,单元格格式可删除excel单元格中的小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache POI将Excel函数写入单元格并评估该函数.我需要做的是从单元格中删除所有小数点.当前每个单元格值的末尾都不需要两个零.单元格格式如下.但是它总是在最后得到两个零.

I'm using Apache POI to Write a Excel function into a cell and evaluate the function. What i need to do is to remove all decimal points from the cell.Currently it getting unnecessary two Zeros at the end of each cell values.The cell formatting as follows. But it is always getting two zeros at the end.

     XSSFCellStyle cellStyle = cell.getCellStyle();
     DataFormat df = workbook.createDataFormat();
     cellStyle.setDataFormat(df.getFormat("###,##0"));
     cell.setCellStyle(cellStyle);


     if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
          evaluator.evaluateFormulaCell(cell);
      }

我引用了以下URL和其他几个站点,但找不到解决该错误的方法.

I have refered following URLs and several other sites and couldn't find a way to fix the bug.

  1. link1
  2. link2
  1. link1
  2. link2

注意 我正在将Apache POI版本3.14与Spring 4.3.1.RELEASE一起使用.

NOTE I'm using Apache POI version 3.14 with Spring 4.3.1.RELEASE.

推荐答案

尝试为工作簿创建单元格样式,并对列中的所有单元格使用"#,##0"数字格式应用此样式:

Try to create the cell style for the workbook and apply this style with "#,##0" number format for all the cells in the column:

CellStyle style = workbook.createCellStyle();
DataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("#,##0"));
cell.setCellStyle(style);

从工作簿中创建样式是最佳方法,并且不会使内存过载.

Creating the style from the workbook is the optimal way and does not overload memory.

您还可以调整公式以将数字四舍五入到小数点后0位:

You can also adjust your formula to round the number with 0 decimal places:

=ROUND(A1,0)

这篇关于使用Apache POI进行数据写入时,单元格格式可删除excel单元格中的小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆