Balaji Rajendran
Balaji Rajendran

Reputation: 87

Cell type of an Excel sheet is shown as Numeric which is actually a Text Type

I am using apache-poi-3.9, to process Excel documents. I have a cell in the document of type TEXT(the cell value is 445). But when I check that cell type in Java it shows NUMERIC. Is there a way to get the cell type as what is there in the excel document. Note : The cell is defined as TEXT. It may contain alphanumeric values or numeric values but I need that type as Text.

Upvotes: 2

Views: 1777

Answers (1)

alex.pulver
alex.pulver

Reputation: 2125

Try to set the cell type as string:

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

or to set the number format as text:

CellStyle style = workbook.createCellStyle();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("@"));
cell.setCellStyle(style);

Check the following links for more details:
http://poi.apache.org/spreadsheet/quick-guide.html#Working+with+different+types+of+cells http://poi.apache.org/spreadsheet/quick-guide.html#DataFormats

To extract a formatted value from a cell, after using previous code:

DataFormatter formatter = new DataFormatter();
String formatted_value = formatter.formatCellValue(cell);

Upvotes: 3

Related Questions