PHP reads Excel file contents

Source: Internet
Author: User
Tags echo date

Project needs to read the contents of Excel, from Baidu Search under, there are two main choices, the first is Phpexcelreader, the other is phpexcel.

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). Phpexcel is powerful, able to output in-memory data into Excel files, but also can do a variety of Excel operations, the following are mainly described below how to use the Phpexcel Excel 2007 format (. xlsx) file read. 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 your own 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. How to get the current sheet by sheet name I do not know if there is know can stand within me. 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:
1<table id= "table_id" >2<?PHP3      $objWorksheet=$objPHPExcel-Getactivesheet ();4      $i= 0;5      foreach($objWorksheet->getrowiterator () as $row){6?>7<tr>8<?PHP9                $cellIterator=$row-getcelliterator ();Ten                $cellIterator->setiterateonlyexistingcells (false); One  A                     if($i= = 0 ){ -                          Echo' <thead> '; -                     } the                foreach($cellIterator  as $cell){ -  -                     Echo' <td> '.$cell->getvalue (). ' </td> '; -  +                } -                     if($i= = 0 ){ +                          Echo' </thead> '; A                     } at                $i++; -?> -</tr> -<?PHP -      } -?> in</table>

Reference: 1, phpexcel official website 2, use Phpexcel to judge and format the date in Excel 3, Phpexcel Chinese help (Knowledge point) 4, use Phpexcel Import export Excel file 5, Phpexcel common function guide

PHP reads Excel file contents

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.