Common methods for exporting Excel files using PHP
- /**
- * Export data to an excel table
- * @ Param $ data: A two-dimensional array with a structure similar to the array found from the database
- * @ Param $ title the title of the first line of excel, an array. if it is null, no title is displayed.
- * @ Param $ file name of the downloaded file
- * @ Examlpe
- $ Stu = M ('user ');
- $ Arr = $ stu-> select ();
- Exportexcel ($ arr, array ('id', 'account', 'password', 'Nickname '), 'File name! ');
- */
- Function exportexcel ($ data = array (), $ title = array (), $ filename = 'report '){
- Header ("Content-type: application/octet-stream ");
- Header ("Accept-Ranges: bytes ");
- Header ("Content-type: application/vnd. ms-excel ");
- Header ("Content-Disposition: attachment; filename =". $ filename. ". xls ");
- Header ("Pragma: no-cache ");
- Header ("Expires: 0 ");
- // Start exporting xls
- If (! Empty ($ title )){
- Foreach ($ title as $ k =>$ v ){
- $ Title [$ k] = iconv ("UTF-8", "GB2312", $ v );
- }
- $ Title = implode ("\ t", $ title );
- Echo "$ title \ n ";
- }
- If (! Empty ($ data )){
- Foreach ($ data as $ key => $ val ){
- Foreach ($ val as $ ck => $ cv ){
- $ Data [$ key] [$ ck] = iconv ("UTF-8", "GB2312", $ cv );
- }
- $ Data [$ key] = implode ("\ t", $ data [$ key]);
- }
- Echo implode ("\ n", $ data );
- }
- }
|
PHP, Excel