Recently i am doing my work with barcode,I want read
generated code128 in Excel file
.So i find the following method of reading xlsx file using PHPExcel.Now i'd like to share with you guys.
First, we need to create a reader for our file. PHP Excel makes this easy for us, as we can use PHPExcel_IOFactory::createReaderForFile() method. It will try to detect filetype by analyzing the extension of the provided file. It can read XLS, XLSX, CSV, PDF
and probably even more file formats. So to create a reader, use this code:
1 require('PHPExcel.php'); // found when you download the PHPExcel
2 $Reader = PHPExcel_IOFactory::createReaderForFile($file);
3 $Reader->setReadDataOnly(true); // set this, to not read all excel properties, just data
Reader is ready and I’ve also specified setReadDataOnly() to read only data, not all the properties around it. Usually we don’t need all the properties and this API is quote memory expensive, so try to read as little as possible to get the job done.
Next step is to load the file – you do that by calling load() method:
1 $objXLS = $Reader->load($file);
To read a value from cell A1 in sheet 0 (first sheet) use this code:
1 $value = $objXLS->getSheet(0)->getCell('A1')->getValue();
2 // or to get calculated value, if there is a formula, etc
3 $value = $objXLS->getSheet(0)->getCell('A1')->getCalculatedValue();
API is huge, so please check their not-so-well written documentation about it on their web page.
As I’ve written before, this API is using quite some memory to read in the Excel files, so if you have a long running script, it’s a good practice to unload it as soon as possible.
You can do that by calling disconnectWorksheets() method.