11thdimension 11thdimension - 4 months ago 395
Java Question

Apache POI setCellType not correctly setting cell type for numeric value

I'm trying to export some data into Excel files. For which I'm using POI.

As I understand Cell.setCellType(double) sets the appropriate excel equivalent numeric value. But this doesn't change type of the cell.

Column type when I open the generated excel file is

General
in the
Home tab
.

Poi Generated Cell Type for Numeric value

Following is a sample code to test it.

package com.example;

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelMain {
public static void main(String[] args) throws Exception {
System.out.println("started");

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);

cell.setCellValue(3.14159);

workbook.write(new FileOutputStream("Test.xlsx"));

System.out.println("finished");
}
}


Is there any other way available to set the cell type to appropriate type ?

I have also tried Cell.setCellType(Cell.CELL_TYPE_NUMERIC) with exactly same result.

Correct type of cell that I expect is
Number
.


I also tried using all
XSSF
classess (code below) as suggested in this post setCellType(HSSFCELL.CELL_TYPE_NUMERIC) is not working in apache poi

package com.example;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelMain {
public static void main(String[] args) throws Exception {
System.out.println("started");

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);

cell.setCellValue(3.14159);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);

workbook.write(new FileOutputStream("Test.xlsx"));

System.out.println("finished");
}
}


Edit

As answered by Jim Garrison, setting cell style does change the type to number. Following is working code.

package com.example;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelMain {
public static void main(String[] args) throws Exception {
System.out.println("started");

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);

cell.setCellValue(3.14159);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);

XSSFDataFormat format = workbook.createDataFormat();
XSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));

cell.setCellStyle(style);

workbook.write(new FileOutputStream("Test.xlsx"));

System.out.println("finished");
}
}

Answer

From the Javadoc for setCellValue(double value) (emphasis mine):

value - the numeric value to set this cell to. For formulas we'll set the precalculated value, for numerics we'll set its value. For other types we will change the cell to a numeric cell and set its value.

So no matter what the cell was before, this method changes the cell type to numeric anyway.

Note that "General" has nothing to do with the cell type, and everything to do with the cell format. To change the way a cell displays, use setCellStyle().