Evgeny Polyakov
Evgeny Polyakov

Reputation: 11

Comma convert to dot after reading xls file with Apache POI

I have a problem with reading xls file by Apache POI.

test.xls contains a cell with general type: 123,56.

Test Code:

FileInputStream myxls = new FileInputStream(path);
HSSF myworkbook = new HSSFWorkbook(myxls);
HSSF mysheet = myworkbook.getSheetAt(0);
HSSF myrow = mysheet.getRow(1);
HSSF mycell = myrow.getCell(0);
System.out.Println(mycell.toString()); 

output: 123.56. The comma converted to dot, but i want to get result as 123,56

I tried to use different methods:

mycell.getNumericCellValue(); returns 123.56
mycell.toString(); returns 123.56
mycell.getRichStringCellValue(); returns an exception
mycell.getStringCellValue(); returns an exception 

I tried to use it:

static DataFormatter dataFormatter = new DataFormatter();

static String getStringValue(Cell cell) {
    return dataFormatter.formatCellValue(cell);
}

but result the same: 123.56

Upvotes: 1

Views: 1716

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109613

The value is held as number, and toString conversions in java follow the programmer's notation with a dot.

Locale locale = new Locale(...);
String repr = String.format(locale, "%.2f", mycell.getNumericCellValue());

Fancy with NumberFormat:

NumberFormat format = NumberFormat.getInstance(locale);
String repr = format.format(mycell.getNumericCellValue());

Upvotes: 2

Andrey E
Andrey E

Reputation: 866

If you need it just for output, than try this:

mycell.toString().replace(".",",")

Or you can set specific style for your cell: Write Double value in numeric cell with specific format in Apache Poi 3.7

Upvotes: 0

Related Questions