Reading and generating Excel files using the PHP Excel class

Source: Internet
Author: User
Tags php excel

There are many ways to use PHP to generate Excel, such as using the simplest carriage returns, tab characters, or directly using the HTML table format, but these are not compatible in any way. Test the Phpexce class, found too strong, can output very complex Excel table, in addition to say can also read the Excel file. Gossip less, release the Digest code:

<?php
Require_once './classes/phpexcel.php ';

$objExcel = new Phpexcel ();
$objWriter = new Phpexcel_writer_excel5 ($objExcel); For other version formats
$objExcel->setactivesheetindex (0);
$objActSheet = $objExcel->getactivesheet ();
Sets the name of the currently active sheet

$objActSheet->settitle (' Sheet1 ');

$objActSheet->setcellvalue (' A2 ', ' China 11 '); Set content in Excel A2 representation coordinates

Generate Excel to File
$objWriter->save ('./test.xls ');
or a direct browser download (optional)
$outputFileName = "Output.xls";
Header ("Content-type:application/octet-stream;charset=utf-8");
Header (' content-disposition:attachment; Filename= '. $outputFileName);
$objWriter->save (' php://output ');
?>

In addition, you can read the Excel file, sample code

  1. <? PHP
  2. $xlsPath = './test.xls '; //Specify the Exls path to read
  3. $type = ' Excel2007 '; Set the type of Excel to resolve Excel5 (2003 or later) or Excel2007
  4. $type = ' Excel5 ';
  5. Introducing the Excel Class
  6. Include ' classes/phpexcel.php ';
  7. Include ' classes/phpexcel/iofactory.php ';
  8. $xlsReader = phpexcel_iofactory::createreader($type);
  9. $xlsReader-setreaddataonly(true);
  10. $xlsReader-setloadsheetsonly(true);
  11. $Sheets = load($xlsPath) $xlsReader
  12. Start reading
  13. $Sheet = toArray,getsheet(0),$Sheets(); //Read First worksheet (note number starting from 0) If reading multiple can do a loop 0,1,2,3 ....

  14. Get the two-dimensional array, where each decimal group is a row of Excel table content containing data for each column of this row
  15. echo ' <pre> ';
  16. Print_r($Sheet);
  17. ?>

Here's an example file and an Excel class

Download the Phpexcel class and sample files

Reading and generating Excel files using the PHP Excel class

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.