user968221
user968221

Reputation: 1

Inserting Long type value in to Excel using apachePOI

I have an excel sheet created using Java program. I need to insert a Long type value( like 361272004652) in to the excel sheet. It is inserting properly. But the problem is when I opened the Excel sheet the Long value is displaying in exponential form (like 3.61272E+11).

I need the value as it is. Not in exponent form.

My code is like this:

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

 ...
 }

where cellVal is of Object type

Upvotes: 0

Views: 930

Answers (1)

Vinicius Lima
Vinicius Lima

Reputation: 544

it's necessary set a format in field. I did this example below in a jsp page, but, it works. Is not necessary convert the value in a float. Put ".0" at end and the CellStyle property takes care.

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

Upvotes: 2

Related Questions