|
<?php
Write content to an XLS file
Error_reporting (E_all);
Ini_set (' display_errors ', TRUE);
Include ' classes/phpexcel.php ';
Include ' classes/phpexcel/iofactory.php ';
$data: XLS file content body
$title: XLS file content title
$filename: Exported file name
The $data and $title must be utf-8 codes, or false values will be written
function Write_xls ($data =array (), $title =array (), $filename = ' report ') {
$objPHPExcel = new Phpexcel ();
Set document properties, set Chinese will be garbled, to perfect ...
$objPHPExcel->getproperties ()->setcreator ("Yun Shu")
->setlastmodifiedby ("Yun Shu")
->settitle ("Product URL export")
->setsubject ("Product URL export")
->setdescription ("Product URL export")
->setkeywords ("Product URL export");
$objPHPExcel->setactivesheetindex (0);
$cols = ' abcdefghijklmnopqrstuvwxyz ';
Set Www.111cn.net title
For ($i =0, $length =count ($title), $i < $length; $i + +) {
echo $cols {$i}. ' 1 ';
$objPHPExcel->getactivesheet ()->setcellvalue ($cols {$i}. ' 1 ', $title [$i]);
}
Set heading styles
$titleCount = count ($title);
$r = $cols {0}. ' 1 ';
$c = $cols {$titleCount}. ' 1 ';
$objPHPExcel->getactivesheet ()->getstyle ("$r: $c")->applyfromarray (
Array
' Font ' = = Array (
' Bold ' = True
),
' Alignment ' = Array (
' Horizontal ' = Phpexcel_style_alignment::horizontal_right,
),
' Borders ' = Array (
' Top ' = Array (
' Style ' = Phpexcel_style_border::border_thin
)
),
' Fill ' = = Array (
' Type ' = Phpexcel_style_fill::fill_gradient_linear,
' Rotation ' = 90,
' StartColor ' = Array (
' Argb ' = ' ffa0a0a0 '
),
' EndColor ' = Array (
' Argb ' = ' FFFFFFFF '
)
)
)
);
$i = 0;
foreach ($data as $d) {//here with foreach, supports associative arrays and numeric indexed arrays
$j = 0;
foreach ($d as $v) {//here with foreach, supports associative arrays and numeric indexed arrays
$objPHPExcel->getactivesheet ()->setcellvalue ($cols {$j}. ( $i +2), $v);
$j + +;
}
$i + +;
}
Generate XLS files in 2003excel format
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-disposition:attachment;filename= '. $filename. '. XLS "');
Header (' cache-control:max-age=0 ');
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
}
$array = Array (
Array (1111, ' name ', ' Brand ', ' Product name ', ' http://www.baidu.com '),
Array (1111, ' name ', ' Brand ', ' Product name ', ' http://www.baidu.com '),
Array (1111, ' name ', ' Brand ', ' Product name ', ' http://www.baidu.com '),
Array (1111, ' name ', ' Brand ', ' Product name ', ' http://www.baidu.com '),
Array (1111, ' name ', ' Brand ', ' Product name ', ' http://www.baidu.com '),
);
Write_xls ($array, Array (' Product ID ', ' Supplier name ', ' Brand ', ' Product name ', ' URL '), ' report ');
?>
|