Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using poi , How to set the Cell type as number

Tags:

I am using poi 3.6

I am able to create the excel properly. But when I trying to set the cell type as number , it always give me cell type as general.

i.e. In the newly create excel , when I right click and go to format cell ->there I always found number to be a General.

My code is like this

style.setAlignment(CellStyle.ALIGN_RIGHT); dataCell.setCellValue(Float.parseFloat(value as String); dataCell.setCellType(Cell.CELL_TYPE_NUMERIC); dataCell.setCellStyle(style); 

Can you please suggest what is missing here ?

like image 434
Anup Avatar asked Mar 06 '13 13:03

Anup


People also ask

How do I change cell format in Apache POI?

To create date cell which display current date to the cell, we can use Apache POI's style feature to format the cell according to the date. The setDateformat() method is used to set date format.

How do you change the cell type?

Select the cells. Select Home > Cell Style and select a style.

How do I get cell type in Apache POI?

Apache POI uses the Workbook interface to represent an Excel file. It also uses Sheet, Row, and Cell interfaces to model different levels of elements in an Excel file. At the Cell level, we can use its getCellType() method to get the cell type.

What is cell type in Apache POI?

The cell type specifies whether a cell can contain strings, numeric value, or formulas. A string cell cannot hold numeric values and a numeric cell cannot hold strings. The following code is used to create different types of cells in a spreadsheet.


1 Answers

You can try this approach too:

HSSFRow row = sheet.createRow(sheet.getLastRowNum()); HSSFCell cell = row.createCell(0); HSSFCellStyle style = workbook.createCellStyle(); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); cell.setCellStyle(style); cell.setCellValue(Float.parseFloat("21.5")); 

Of course, take a look at the documentation and examples about data formats.

like image 140
Dani Avatar answered Oct 25 '22 13:10

Dani