Hey all,
I am uploading some files that could be csv, xls, xlsx... so im using the PHPExcel_IOFactory::identify($filepath) to create the file object.
But when i upload csv files I can't seem to pull leading zeroes. The other formats (xsls,xlsx) I can retrieve the column with the leading zeroes, since I guess the column has meta data set to Text formatting in Excel.
But with simple csv files i was hoping to force the column to be interpreted as Text it via:
$objWorksheet->getStyle('A1:A'.$maxRow)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
But it has had no effect... Thought I could upload a simple php file and sample csv.
For example, It should output: 'Cell #A2 is 0101' but it outputs currently 'Cell #A2 is 101'
Here is the php and csv file: https://dl.dropboxusercontent.com/u/36600570/phpexcel_example/php-excel-example.zip
Here is the code if afraid to test/download above file:
Looking for any feedback,
Thanks!
Arian
I am uploading some files that could be csv, xls, xlsx... so im using the PHPExcel_IOFactory::identify($filepath) to create the file object.
But when i upload csv files I can't seem to pull leading zeroes. The other formats (xsls,xlsx) I can retrieve the column with the leading zeroes, since I guess the column has meta data set to Text formatting in Excel.
But with simple csv files i was hoping to force the column to be interpreted as Text it via:
$objWorksheet->getStyle('A1:A'.$maxRow)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
But it has had no effect... Thought I could upload a simple php file and sample csv.
For example, It should output: 'Cell #A2 is 0101' but it outputs currently 'Cell #A2 is 101'
Here is the php and csv file: https://dl.dropboxusercontent.com/u/36600570/phpexcel_example/php-excel-example.zip
Here is the code if afraid to test/download above file:
Looking for any feedback,
Thanks!
Arian
<?php
require '../../libs/PHPExcel/Classes/PHPExcel.php';
require '../../libs/PHPExcel/Classes/PHPExcel/IOFactory.php';
$filepath = 'test.csv'; //for now simple script to
try {
$fileType = PHPExcel_IOFactory::identify($filepath);
$objReader = PHPExcel_IOFactory::createReader($fileType);
} catch ( Exception $e ){
echo 'Please specific the correct file location in script';
exit;
}
$objPHPExcel = $objReader->load($filepath);
$objWorksheet = $objPHPExcel->getActiveSheet();
//!!!make the 1st column A1:A4 Formatted as Text so leading zeroes come through.
$maxRow = $objWorksheet->getHighestDataRow();
$objWorksheet->getStyle('A1:A'.$maxRow)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
foreach ($objWorksheet->getRowIterator() as $row) {
$row_num = $row->getRowIndex();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells, even if it is not set. By default, only cells that are set will be iterated.
$row_num = $row->getRowIndex();
foreach ($cellIterator as $cell_num => $cell) { //Note: $cell_num index is 0 based!, so add a 1 to make more sense when viewing spreadsheet
$cell_num = $cell_num + 1;
$val = trim($cell->getValue());
$formatted_val = trim($cell->getFormattedValue());
echo 'Cell #'. chr($cell_num-1 +65) . $row_num .' is '. $val.'<br/>'; //should output 0101<br/>0101<br/>00102<br/>
}
echo '<br/>';
}