生成excel 把數(shù)據(jù)庫的數(shù)據(jù)導(dǎo)入到excel就行了,教你個簡單的寫法
成都創(chuàng)新互聯(lián)是一家專業(yè)提供赤城企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站設(shè)計、HTML5建站、小程序制作等業(yè)務(wù)。10年已為赤城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
?
\n結(jié)尾的是換行 \T是下一個單元格,直接查詢輸出就行
根據(jù)下列編碼程序可以。
1./*** 批量導(dǎo)出數(shù)據(jù)* @param $arr 從數(shù)據(jù)庫查詢出來,即要導(dǎo)出的數(shù)據(jù)* ?$name excel表歌名*/
2.function expExcel($arr,$name){?require_once 'PHPExcel.php';
3. //實(shí)例化?$objPHPExcel = new PHPExcel();?/*右鍵屬性所顯示的信息*/
4.$objPHPExcel-getProperties()-setCreator("zxf") ?//?-setLastModifiedBy("zxf") ?//最后一? -setTitle('數(shù)據(jù)EXCEL導(dǎo)出') ?//標(biāo)題-setSubject('數(shù)據(jù)EXCEL導(dǎo)出') //主題setDescription('導(dǎo)出數(shù)據(jù)') ?//描setKeywords("excel") ? //標(biāo)記setCategory("result file"); ?//類別
5. //設(shè)置當(dāng)前的表格??$objPHPExcel-setActiveSheetIndex(0);// 設(shè)置表格第一行顯示內(nèi)容$objPHPExcel-getActiveSheet()? -setCellValue('A1', '業(yè)主姓名')?-setCellValue('B1', '密碼')-setCellValue('C1', '手機(jī)號碼'? -setCellValue('D1', '地址')
6.//設(shè)置第一行為紅色字體?-getStyle('A1:D1')-getFont()-getColor()-setARGB(PHPExcel_Style_Color::COLOR_RED);$key = 1;?/*以下就是對處理Excel里的數(shù)據(jù)。
php導(dǎo)出數(shù)據(jù)excel有專門的庫,當(dāng)導(dǎo)出少量數(shù)據(jù)的時候速度很快,但是當(dāng)數(shù)據(jù)量大的時候就會存在服務(wù)器內(nèi)存不夠之類的。
所以在導(dǎo)出大量數(shù)據(jù)的時候就應(yīng)該分頁查詢數(shù)據(jù),避免服務(wù)器宕機(jī)。正好PHP提供了fputcsv函數(shù)可以將數(shù)據(jù)寫入到csv文件中。
這樣我們就可以使用PHP對數(shù)據(jù)進(jìn)行分頁查詢,再寫入到csv文件中。
請參考 PHPExcel 這個開源項(xiàng)目,用來導(dǎo)出數(shù)據(jù)到excel表格,項(xiàng)目的Examples 文件夾有大量的實(shí)例可供參考
php 把數(shù)據(jù)導(dǎo)出到excel表格有多種方法,比如使用 phpExcel 等,以下代碼是直接通過 header 生成 excel 文件的代碼示例:
?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=xls_region.xls");
$cfg_dbhost?=?'localhost';
$cfg_dbname?=?'testdb';
$cfg_dbuser?=?'root';
$cfg_dbpwd?=?'root';
$cfg_db_language?=?'utf8';
//?END?配置
//鏈接數(shù)據(jù)庫
$link?=?mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//選擇編碼
mysql_query("set?names?".$cfg_db_language);
//users表
$sql?=?"desc?users";
$res?=?mysql_query($sql);
echo?"tabletr";
//導(dǎo)出表頭(也就是表中擁有的字段)
while($row?=?mysql_fetch_array($res)){
$t_field[]?=?$row['Field'];?//Field中的F要大寫,否則沒有結(jié)果
echo?"th".$row['Field']."/th";
}
echo?"/tr";
//導(dǎo)出100條數(shù)據(jù)
$sql?=?"select?*?from?users?limit?100";
$res?=?mysql_query($sql);
while($row?=?mysql_fetch_array($res)){
echo?"tr";
foreach($t_field?as?$f_key){
echo?"td".$row[$f_key]."/td";
}
echo?"/tr";
}
echo?"/table";
?
1 $fp = fopen('php://output', 'a');
2
3 // 輸出Excel列名信息
4 $head = array("郵件");
5 foreach ($head as $i = $v) {
6 // CSV的Excel支持GBK編碼,一定要轉(zhuǎn)換,否則亂碼
7 $head[$i] = iconv('utf-8', 'gbk', $v);
8 }
9
10 // 將數(shù)據(jù)通過fputcsv寫到文件句柄
11 fputcsv($fp, $head);
12
13 // 計數(shù)器
14 $cnt = 0;
15 // 每隔$limit行,刷新一下輸出buffer,不要太大,也不要太小
16 $limit = 100000;
17
18 // 逐行取出數(shù)據(jù),不浪費(fèi)內(nèi)存
19 $count = count($email);
20
21 for($t=0;$t$count;$t++) {
22
23 $cnt ++;
24 if ($limit == $cnt) { //刷新一下輸出buffer,防止由于數(shù)據(jù)過多造成問題
25 ob_flush();
26 flush();
27 $cnt = 0;
28 }
29 $row[] = $email[$t];
30 foreach ($row as $i = $v) {
31 $row[$i] = iconv('utf-8', 'gbk', $v);
32 }
33 fputcsv($fp, $row);
34 unset($row);
35 }
網(wǎng)頁題目:PHP百萬級數(shù)據(jù)導(dǎo)出表格,php導(dǎo)出大量數(shù)據(jù)到excel
分享網(wǎng)址:http://chinadenli.net/article12/hedggc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信小程序、品牌網(wǎng)站建設(shè)、定制開發(fā)、App設(shè)計、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)