Home » excel » java – Writing a long value to excel in apache POI

java – Writing a long value to excel in apache POI

Posted by: admin May 14, 2020 Leave a comment


When i write a long value to a cell in Excelsheet using Apache POI, it writes it with some numerical manipulation. Example:

cell = row.createCell(6);
// long value returned is 365646975447
// in excel sheet, it is written as 3.65647E+11.
// but when i click on the cell, i can see 365646975447 as the value.

I want it to display the exact value as i write. How can i do this?

How to&Answers:

The cell is of type general and not numeric.

You need to do this cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

If you copy paste 365646975447 by hand in an Excel Cell that is of type General it will be displayed 3.65647E+11. If you change it to numeric then it displays correctly.

Try this:

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;

public class Test
    public static void main(String[] args)
    throws Exception
        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);
        style = wb.createCellStyle();

        FileOutputStream fileOut = new FileOutputStream("workbook.xls");

Also check:


In POI version 4.1 the API is as follows:

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;

XSSFCell cell = row.createCell(key);