Apache POI setCellType not correctly setting cell type for numeric value
<p>I'm trying to export some data into Excel files. For which I'm using POI.</p>

<p>As I understand <a href="https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellValue(double)" rel="nofollow noreferrer">Cell.setCellType(double)</a> sets the appropriate excel equivalent numeric value. But this doesn't change type of the cell.</p>

<p>Column type when I open the generated excel file is <code>General</code> in the <code>Home tab</code>.</p>

<p><a href="https://i.stack.imgur.com/s5tfV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s5tfV.jpg" alt="Poi Generated Cell Type for Numeric value"></a></p>

<p>Following is a sample code to test it.</p>

<pre class="lang-java prettyprint-override"><code>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");
    }
}
</code></pre>

<p>Is there any other way available to set the cell type to appropriate type ?</p>

<p>I have also tried <a href="https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellType(int)" rel="nofollow noreferrer">Cell.setCellType(Cell.CELL_TYPE_NUMERIC)</a> with exactly same result.</p>

<p><strong>Correct type of cell that I expect is <code>Number</code>.</strong></p>

<p>I also tried using all <code>XSSF</code> classess (code below) as suggested in this post <a href="https://stackoverflow.com/questions/27840258/setcelltypehssfcell-cell-type-numeric-is-not-working-in-apache-poi">setCellType(HSSFCELL.CELL_TYPE_NUMERIC) is not working in apache poi</a></p>

<pre class="lang-java prettyprint-override"><code>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");
    }
}
</code></pre>

<p><strong>Edit</strong></p>

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

<pre><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");
    }
}
</code></pre>