3

SetCellType(HSSFCELL.CELL_TYPE_NUMERIC) is not working in apache poi. Anyone have used this feature of POI in JAVA?

When I created XLS file using POI and cell contains integer value and I set cell type as numeric at that time that cell reflects some other integer value. Example: Using JAVA program and poi utility, I am putting in Cell A1 value "5". Now this cell contains Integer value but by default cell type is CELL_TYPE_STRING. So this cell gives me error and asked me to convert this cell to Number. But using program when I will set this cell type as CELL_TYPE_NUMERIC at that time it does not give me error in XLS file but it shows me different value.

Anyone has faced such issue? If yes then is there any solution for this. I stuck up with this one.

Please ask me if you need some more clarification.

Thanks in advance.

Akshay
  • 3,558
  • 4
  • 43
  • 77
Nishith Shah
  • 81
  • 1
  • 2
  • 8
  • The extension has little to do with this problem. However, the file type does. You can create an Excel 2012 document and save it using .XLS instead of .XSLS, but it will still be Excel 2012 format. You have to make sure that you use the right classes (XSSF vs. HSSF) depending on the type of file you are handling. – hfontanez Jan 08 '15 at 14:49
  • 2
    If you have an integer value, why are you not setting that as the cell value? Why are you going via a string? – Gagravarr Jan 08 '15 at 16:44

3 Answers3

2

Since you are using XSSF not HSSF, use cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC) instead of setCellType(HSSFCELL.CELL_TYPE_NUMERIC).

Akshay
  • 3,558
  • 4
  • 43
  • 77
1

** Give a Integer value instead of a String Value.**

HSSFCell cell = row.createCell(col);
cell.setCellStyle(style);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
if(value!=null)
    cell.setCellValue(Integer.parseInt(value));
Uday kumar
  • 81
  • 1
  • 1
  • 5
0

use:

HSSFCell cell = row.createCell( col );
cell.setCellType ( CellType.NUMERIC );

or

cell.setCellType ( CellType.NUMERIC );
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19