php導(dǎo)出大量數(shù)據(jù)到Excel,可以通過生成多個Excel文件,然后壓縮成壓縮包解決。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、洛隆網(wǎng)站維護、網(wǎng)站推廣。
方案是:假如我們數(shù)據(jù)庫有10w條數(shù)據(jù),每2000條數(shù)據(jù)生成一個Excel文件,這樣每次只要從數(shù)據(jù)庫里查詢出2000條數(shù)據(jù)即可,一定要分頁去查詢。
原因:主要是數(shù)據(jù)庫性能和寫文件性能。分頁查詢可以解決數(shù)據(jù)庫壓力的問題, 生成多個文件可以解決單個文件太大,后期維護Excel文件的問題。
要注意的:
1. 在導(dǎo)出邏輯文件開頭,一定要聲明 set_time_limit(0) ,防止腳本超時;
2. 每個文件生成后,適當(dāng)?shù)膕leep一下,讓程序休息一下下;
3. 因為一次導(dǎo)出最后要將生成的多個Excel文件打包成一個壓縮包,所以要刪除掉生成的Excel文件,節(jié)省服務(wù)器存儲空間;
下面是我實際工作中,寫的一個php導(dǎo)出大量數(shù)據(jù)到Excel的代碼,你可以參考一下:
$keynames=array(
'0'='訂單號',
'1'='項目名稱',
'2'='單價',
'3'='數(shù)量',
'4'='運費',
'5'='金額',
'6'='支付狀態(tài)',
'7'='支付時間',
'8'='選項',
'9'='備注',
'10'='收件人',
'11'='手機',
'12'='送貨地址',
'13'='物流',
'14'='物流單號'
);
down_excel($eorders, $keynames,$array_key, $name);
//生成簡單excel。table生成。生成excel非標(biāo)準(zhǔn)。phpexcel生成的標(biāo)準(zhǔn),可讀寫excel
function down_xls($data, $keynames, $name = 'dataxls') {
$xls [] = "htmlmeta http-equiv=content-type content=\"text/html; charset=UTF-8\"bodytable border='1'";
$xls [] = "trtdID/tdtd" . implode ( "/tdtd", array_values ( $keynames ) ) . '/td/tr';
foreach ( $data as $o ) {
$line = array (++ $index );
foreach ( $keynames as $k = $v ) {
$line [] = $o [$k];
}
$xls [] = 'trtd' . implode ( "/tdtd", $line ) . '/td/tr';
}
$xls [] = '/table/body/html';
$xls = join ( "\r\n", $xls );
header ( 'Content-Disposition: attachment; filename="' . $name . '.xls"' );
die ( mb_convert_encoding ( $xls, 'UTF-8', 'UTF-8' ) );
}
//add by wqc excel
function down_excel($data, $keynames,$array_key, $name = 'dataxls') {
require_once(dirname(dirname(dirname(__FILE__))) . '/PHPExcel/PHPExcel.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/PHPExcel/PHPExcel/IOFactory.php');
//實例化phpexcel
$objPHPExcel = new PHPExcel();
$objPHPExcel-getActiveSheet()-getDefaultColumnDimension()-setWidth(16);//設(shè)置單元格寬度
$objPHPExcel-getActiveSheet()-setTitle('kutuan');//設(shè)置當(dāng)前工作表的名稱
foreach($keynames as $k=$r){
$objPHPExcel-getActiveSheet()-getStyleByColumnAndRow($k, 1)-getFont()-setBold(true);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow($k, 1, $r);
}
foreach($data as $key=$value)
{
//$objPHPExcel-getSheet(1)-getProtection()-setSheet(true);
//$objPHPExcel-getSheet(1)-protectCells(0, ($key+2),$value['id']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(0, ($key+2),$value['id']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(1, ($key+2),$value['team_id']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(2, ($key+2),$value['price']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(3, ($key+2),$value['quantity']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(4,($key+2),$value['fare']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(5, ($key+2),$value['origin']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(6, ($key+2),$value['state']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(7, ($key+2),$value['pay_time']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(8, ($key+2),$value['condbuy']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(9, ($key+2),$value['remark']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(10, ($key+2),$value['realname']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(11, ($key+2),$value['mobile']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(12, ($key+2),$value['address']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(13, ($key+2),$value['express']);
$objPHPExcel-getActiveSheet()-setCellValueByColumnAndRow(14, ($key+2),$value['express_no']);
}
$objPHPExcel-setActiveSheetIndex(0);//設(shè)置打開excel時顯示哪個工作表$name.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".$name);
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
return $objWriter-save('php://output');
}
超簡單,假設(shè)當(dāng)前腳本文件在a/目錄中,代碼如下:
//?首先需要檢測b目錄是否存在
if?(!is_dir('b/'))?mkdir('b/');?//?如果不存在則創(chuàng)建
//?在檢測b/目錄中是否存在c.php文件
if?(!file_exists('b/c.php'))?file_put_contents('b/c.php',?'d');?//?如果c.php不存在則創(chuàng)建,當(dāng)然還可以使用$file?=?fopen('b/c.php',?'a+')的方式創(chuàng)建然后用fwrite('d',?$file)寫入d
以上若還有任何問題,歡迎追問~