生活随笔
收集整理的這篇文章主要介紹了
php 使用table方式导出excel文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這些天在使用PHPExcel導出數據時,5000條數據竟然掛了。后來跟同事聊聊,有些明悟,PHPExcel做了很多處理,我在這里理解為渲染,就會暫用過多的空間,‘膨脹’的空間導致內存暫用過大,就掛了。其實只要我們只是簡單的導出操作,沒有必要使用PHPExcel。
就是將html的表格轉換excel的表格;此種方法適應于設置各種單元格的顯示,合并,只需設置html的table,設置css就能導出各式各樣的excel模板
實例如下:
導出一個帶表頭,設置字體大小,居中,排版適中;
<?
php
/*
*處理Excel導出
*@param $datas array 設置表格數據
*@param $titlename string 設置head
*@param $title string 設置表頭
*/
function excelData(
$datas,
$title,
$filename){$str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"
;$str .="<style>tr,td,th{text-align: center;height: 22px;line-height: 22px;}</style>"
;$str .="<table border=1>"
;$str .=
$title;foreach (
$datas as $key=>
$rt ){$str .= "<tr>"
;foreach (
$rt as $k =>
$v ){$str .= "<td>{
$v}</td>"
;}$str .= "</tr>\n"
;}$str .= "</table></body></html>"
;header( "Content-Type: application/vnd.ms-excel; name='excel'"
);header( "Content-type: application/octet-stream"
);header( "Content-Disposition: attachment; filename=".
$filename );header( "Cache-Control: must-revalidate, post-check=0, pre-check=0"
);header( "Pragma: no-cache"
);header( "Expires: 0"
);exit(
$str );
}
//todo:導出數據(自行設置)
$dataResult =
array(['騰訊','百度','阿里巴巴'
],
['騰訊2','百度2','阿里巴巴2'
]
);
//表頭
$headList =
['合作單位1','合作單位2','合作單位3',
];
array_unshift(
$dataResult,
$headList);
$headTitle = "合作單位 優惠券贈送記錄"
;
$title = "合作單位記錄"
;
$headtitle= "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='".
count(
$headList)."' >{
$headTitle}</th></tr>"
;
$filename =
$title.".xls"
;
excelData($dataResult,
$headtitle,
$filename);
?
轉載于:https://www.cnblogs.com/-mrl/p/7686640.html
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的php 使用table方式导出excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。