XLS to CSV - Number-like string

Topics: User Forum
Jun 5, 2012 at 4:43 PM

I'm converting a .xls to a .csv using:

$xls = PHPExcel_IOFactory::load('input.xls');
$writer = PHPExcel_IOFactory::createWriter($xls, 'CSV');
$writer->setDelimiter(',');
$writer->setEnclosure('');
$writer->save('output.csv');

I have a column with data like these:

1,2
2,1
1,3

The output .csv returns me

1.2
2.1
1.3

How could I avoid this?

Coordinator
Jun 5, 2012 at 10:53 PM
Edited Jun 8, 2012 at 9:10 AM

I'm assuming that your xls file cells are formatted with a , as a decimal separator. This will normally be translated to use the . decimal separator used by most computer systems for floating point values.

Currently PHPExcel's CSV Writer does not support automatic locale formatting features such as the decimal , separator. However, if you check section 6.6.2 of the Developer documentation on writing CSV files, you'll find an example showing how to set decimal and thousands separators manually.

Jul 31, 2012 at 8:18 AM

"I'm assuming that your xls file cells are formatted with a , as a decimal separator."

Not exactly, I have a field that is a comma separated list of IDs (in my example there are only 2 IDs, and it looks like he thinks it's a number).

Coordinator
Jul 31, 2012 at 8:56 AM

If you open the file in a text editor, is it being written correctly? Is this just MS Excel or other CSV reader program failing to read the file correctly?