我有一个使用 Java 程序创建的 Excel 工作表。我需要在 Excel 工作表中插入一个 Long
类型值(如 361272004652
)。插入正确。但问题是,当我打开 Excel 工作表时,Long 值以指数形式显示(如 3.61272E+11
)。
我需要原样的值。不是指数形式。
我的代码是这样的:
else if (cellVal instanceof Long) {
cell.setCellValue((Long)cellVal);
...
}
其中 cellVal
是对象类型
最佳答案
需要在字段中设置格式。 我在 jsp 页面中做了下面的示例,但是它有效。 没有必要将值转换为 float 。将“.0”放在末尾,CellStyle 属性会负责。
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.ss.usermodel.Cell"%>
<%@ page import="org.apache.poi.ss.usermodel.CellStyle"%>
<%@ page import="org.apache.poi.ss.usermodel.DataFormat"%>
<%@ page import="org.apache.poi.ss.usermodel.Row"%>
<%
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
short rowNum = 0;
short colNum = 0;
Row row = sheet.createRow(rowNum++);
Cell cell = row.createCell(colNum);
cell.setCellValue(361272004652.0);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("filexample.xls");
wb.write(fileOut);
fileOut.close();
%>
关于java - 使用 apachePOI 将 Long 类型值插入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16522602/