Reader CSV (from MSO) : Is leading zeros as string possible ?

Feb 19, 2010 at 11:45 AM

Hello all,

I have got a problem. I get a CSV file without enclosure character (eg. David;031234;Even street;5190 cause windows export without).

I'd like to keep leading zeros. How can I do that ?

For the moment I use getValue() method. Is there another method ? Is it possible to specify the format I want.

Here my actual code :

			$objReader = PHPExcel_IOFactory::createReader('CSV');
			$objReader->setInputEncoding('ISO-8859-1');
			$objReader->setDelimiter(';');
			$objReader->setEnclosure('');
			$objReader->setLineEnding("\r\n");
			$objReader->setSheetIndex(0);
			$objPHPExcel = $objReader->load($this->_filename);
			$objWorksheet = $objPHPExcel->getActiveSheet();
			$data = array();
			$firstline = true;
			foreach ($objWorksheet->getRowIterator() as $row)
			{
				$record = array();
				$cellIterator = $row->getCellIterator();
				$cellIterator->setIterateOnlyExistingCells(false);
				foreach ($cellIterator as $cell) {
					if (!empty($cell))
					{
						$record[] = $cell->getValue();
					}
					else
					{
						$record[] = '""';
					}
				}
			}

Tyvm

Developer
Feb 20, 2010 at 11:46 PM

CSV reader uses the currently set value binder. Which means that you can try to use a custom value binder like this:

         class TextValueBinder implements PHPExcel_Cell_IValueBinder
{
public function bindValue(PHPExcel_Cell $cell, $value = null) {
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
return true;
}
}

$savedValueBinder = PHPExcel_Cell::getValueBinder();
PHPExcel_Cell::setValueBinder(new TextValueBinder());

$objReader = PHPExcel_IOFactory::createReader('CSV');
$objReader->setInputEncoding('ISO-8859-1');
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(0);


$objPHPExcel = $objReader->load($this->_filename);

PHPExcel_Cell::setValueBinder($savedValueBinder);

$objWorksheet = $objPHPExcel->getActiveSheet();
$data = array();
$firstline = true;
foreach ($objWorksheet->getRowIterator() as $row)
{
$record = array();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
if (!empty($cell))
{
$record[] = $cell->getValue();
}
else
{
$record[] = '""';
}
}
}