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
- <? PHP
- $xlsPath = './test.xls '; //Specify the Exls path to read
- $type = ' Excel2007 '; Set the type of Excel to resolve Excel5 (2003 or later) or Excel2007
- $type = ' Excel5 ';
- Introducing the Excel Class
- Include ' classes/phpexcel.php ';
- Include ' classes/phpexcel/iofactory.php ';
- $xlsReader = phpexcel_iofactory::createreader($type);
- $xlsReader-setreaddataonly(true);
- $xlsReader-setloadsheetsonly(true);
- $Sheets = load($xlsPath) $xlsReader
- Start reading
- $Sheet = toArray,getsheet(0),$Sheets(); //Read First worksheet (note number starting from 0) If reading multiple can do a loop 0,1,2,3 ....
- Get the two-dimensional array, where each decimal group is a row of Excel table content containing data for each column of this row
- echo ' <pre> ';
- Print_r($Sheet);
- ?>
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