1. Download Phpexcel
2. Introduce the Phpexcel class file to the page and create the object of the class
Include (".. /chajian/phpexcel/classes/phpexcel.php "); $excel = new Phpexcel ();
3. Query the database to get a two-dimensional array here the array is named: $attr
4. Export Excel code, table header array and data array corresponding to:
//excel table format, here is a shorthand for 10 columns $letter = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' f ', ' f ', ' G ', ' H ', ' I ');//table header Array $tableheader = array (' Reporting time ' Report completion ', ' reporting staff ', ' final accomplishment ', ' tutor Review ');//Fill header information for ($i = 0; $i < count ($tableheader); $i + +) {$excel->getactivesheet ()- >setcellvalue ("$letter [$i]1", "$tableheader [$i]");} Fill in the table information for ($i = 2; $i <= count ($attr) + 1; $i + +) {$j = 0;foreach ($attr [$i-2] as $key + = $value) {$excel->getacti Vesheet ()->setcellvalue ("$letter [$j] $i", "$value"); $j + +;}} Create an Excel input object $write = new Phpexcel_writer_excel5 ($excel), header ("Pragma:public"), Header ("expires:0"), Header (" Cache-control:must-revalidate, Post-check=0, pre-check=0 "); Header (" Content-type:application/force-download "); Header ("Content-type:application/vnd.ms-execl"); Header ("Content-type:application/octet-stream"); Header (" Content-type:application/download "); Header ("Content-disposition:attachment;filename= ' {$sj}_{$bt} ' job. xls '); Header (" Content-transfer-encoding:binary "); $write->save (' php://output ');
5. If the long digit string is displayed as a scientific counting solution
(1) Specify the type of data to display when setting the value
$excel->getactivesheet ()->setcellvalueexplicit (' D1 ', 120000000000,phpexcel_cell_datatype::type_string);
(2) Add a space before the numeric string to make it a string
$excel->getactivesheet ()->setcellvalue (' D1 ', '. 120000000000);
Phpexcel Exporting data tables