2013-05-13 180 views
0

我有一個使用Java程序創建的Excel表單。我需要將一個Long類型值(如361272004652)插入到Excel工作表中。它正確插入。但問題是,當我打開Excel表單時Long值以指數形式顯示(如3.61272E+11)。使用apachePOI插入Long類型的值到Excel中

我需要它的價值。不是指數形式。

我的代碼是這樣的:

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

... 
} 

其中cellVal是Object類型

+3

這只是單元格的顯示格式,不是? – 2013-05-13 12:56:44

回答

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();  
%> 
+0

感謝Vinicius和Matt。它工作正常。 – user968221 2013-05-14 02:49:47

相關問題