0

我有一个使用 Java 程序创建的 excel 表。我需要在 Excel 表中插入一个Long类型值(如361272004652)。它正在正确插入。但问题是当我打开 Excel 工作表时,Long 值以指数形式显示(如3.61272E+11)。

我需要它的价值。不是指数形式。

我的代码是这样的:

 else if (cellVal instanceof Long) {    
    cell.setCellValue((Long)cellVal);

 ...
 }

cellVal对象类型在哪里

4

1 回答 1

2

有必要在字段中设置格式。我在下面的jsp页面中做了这个例子,但是,它有效。没有必要将值转换为浮点数。将“.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();    
%>
于 2013-05-13T13:41:50.650 回答