Phpexcel reading an Excel file sample

Source: Internet
Author: User
Tags echo date

Phpexcel Read an example of an Excel file
Phpexcel latest official Download URL: http://phpexcel.codeplex.com/
Phpexcel is a very convenient class for generating Excel format files. The official download package contains a lot of examples of how to generate various styles of Excel files, but not a complete example of reading Excel files, here is an example of reading:
reading the contents of Excel has two main choices: Phpexcelreader, Phpexcel. The
Phpexcelreader is relatively lightweight and supports only Excel reading, which is actually a reader. Unfortunately, it is not possible to support Excel 2007 format (. xlsx). The
Phpexcel is powerful enough to output in-memory data into an Excel file and can do a variety of things with Excel, and here's how to read the Excel 2007 format (. xlsx) file using Phpexcel.

After you download Phpexcel, save it to your own class file directory, and then use the following code to open the Excel (xlsx) format file:
require_once '/libs/phpexcel-1.8.0/classes /phpexcel.php '; Modify the directory for Yourself
Echo ' <p>test phpexcel 1.8.0:read xlsx file</p> ';
$objReader = Phpexcel_iofactory::createreaderforfile ($filename);
$objPHPExcel = $objReader->load ($filename);
$objPHPExcel->setactivesheetindex (1);
$date = $objPHPExcel->getactivesheet ()->getcell (' A16 ')->getvalue ();

You can see the contents of the file in the output of the variable of the. Phpexcel uses the Phpexcel_iofactory class to automatically match the uploaded file types, and of course we can make our own to parse the file types.
The php file is then loaded into the Objphpexcel object via the Load method. If the Excel file has more than one sheet, you can set the currently active sheet through Setactivesheetindex.

It is important to note that for date formats in Excel, Phpexcel is not a date type, and we need to use the following methods to do date type conversions.
echo Date ("Y-m-d h:i:s", phpexcel_shared_date::exceltophp ($date));

The following code shows how to traverse the contents of an Excel display:
<table>
<?php
$objWorksheet = $objPHPExcel->getactivesheet ();
$i = 0;
foreach ($objWorksheet->getrowiterator () as $row) {
?>
<tr>
<?php
$cellIterator = $row->getcelliterator ();
$cellIterator->setiterateonlyexistingcells (FALSE);

if ($i = = 0) {
Echo ' <thead> ';
}
foreach ($cellIterator as $cell) {

Echo ' <td> '. $cell->getvalue (). ' </td> ';

}
if ($i = = 0) {
echo ' </thead> ';
}
$i + +;
;
</tr>
<?php
}
</table>
====================================== ===========

Traditional method:
<?php
require_once '. /classes/phpexcel/iofactory.php ';
//Check Prerequisites
If (!file_exists ("Test.xls")) {
exit ("not found test.xls.\n");
}
$reader = Phpexcel_iofactory::createreader (' Excel5 ');//set to EXCEL5 format (excel97-2003 workbook)
$PHPExcel = $reader- >load ("Test.xls"); Load the Excel file
$sheet = $PHPExcel->getsheet (0);//Read the first worksheet
$highestRow = $sheet->gethighestrow ();//Get Total rows
$highestColumm = $sheet->gethighestcolumn ();//Gets the total number of columns
$highestColumm = Phpexcel_cell:: Columnindexfromstring ($colsNum); The letter column is converted to a numeric column such as: AA to

/** Loop reads the data for each cell */
for ($row = 1; $row <= $highestRow; $row + +) {//lines start with line 1th
for ($c olumn = 0; $column < $highestColumm; $column + +) {//Number of columns starts with column No. 0
$columnName = phpexcel_cell::stringfromcolumnindex ($column);
Echo $columnName. $row. " : ". $sheet->getcellbycolumnandrow ($column, $row)->getvalue ()." <br/> ";
}
}
?

Streamlined method:
<?php
Require_once '. /classes/phpexcel/iofactory.php ';
Check Prerequisites
if (!file_exists ("Test.xls")) {
Exit ("not found test.xls.\n");
}
$reader = Phpexcel_iofactory::createreader (' Excel5 '); Set in EXCEL5 format (excel97-2003 workbook)
$PHPExcel = $reader->load ("Test.xls"); Loading Excel files
$sheet = $PHPExcel->getsheet (0); Read the first worksheet
$highestRow = $sheet->gethighestrow (); Total number of rows obtained
$highestColumm = $sheet->gethighestcolumn (); Total number of columns obtained

/** cycle through the data of each cell */
for ($row = 1; $row <= $highestRow; $row + +) {//lines start at line 1th
for ($column = ' a '; $column <= $highestColumm; $column + +) {//The number of columns starts with column A
$dataset [] = $sheet->getcell ($column. $row)->getvalue ();
Echo $column. $row. ":". $sheet->getcell ($column. $row)->getvalue (). " <br/> ";
}
}
?>

Phpexcel reading an Excel file sample

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.