0

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

1
  • 3
    That's just the cell's display format, no? Commented May 13, 2013 at 12:56

1 Answer 1

2

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();    
%>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.