本文目錄一覽:
- 1、PHP如何導出Excel文件
- 2、php導出excel表、壓縮成文件下載到本地如何實現?
- 3、php導出excel表後,打包成壓縮包,然後下載到本地如何實現?
- 4、php怎麼導出大量數據的Excel
PHP如何導出Excel文件
昨天項目里有個新需求,客戶希望把一些數據能導出成為Excel表格,剛開始用PHP原生輸入Excel表格,發現效果不是很理想,於是找到一個比較著名的庫:PHPExcel。下面是一個簡單的demo,分享給大家,希望可以幫到有同樣需求的朋友。
1.百度:phpexcel,結果如圖所示,點擊第一個結果;
PHP導出Excel,PHP輸入Excel
2.進入官網後,找到右邊的download按鈕,下載,下載完成的是一個壓縮文件,解壓放到你的項目目錄里,根據個人情況而定;
PHP導出Excel,PHP輸入Excel
PHP導出Excel,PHP輸入Excel
3.因為這裡給大家做演示,所以建了一個測試文件,有點基礎的都能明白是怎麼回事,下面進入代碼;
PHP導出Excel,PHP輸入Excel
4.
//引入PHPExcel庫文件(路徑根據自己情況)
include ‘./phpexcel/Classes/PHPExcel.php’;
//創建對象
$excel = new PHPExcel();
//Excel表格式,這裡簡略寫了8列
$letter = array(‘A’,’B’,’C’,’D’,’E’,’F’,’F’,’G’);
//表頭數組
$tableheader = array(‘學號’,’姓名’,’性別’,’年齡’,’班級’);
//填充表頭信息
for($i = 0;$i count($tableheader);$i++) {
$excel-getActiveSheet()-setCellValue(“$letter[$i]1″,”$tableheader[$i]”);
}
PHP導出Excel,PHP輸入Excel
5.
//表格數組
$data = array(
array(‘1′,’小王’,’男’,’20’,’100′),
array(‘2′,’小李’,’男’,’20’,’101′),
array(‘3′,’小張’,’女’,’20’,’102′),
array(‘4′,’小趙’,’女’,’20’,’103′)
);
//填充表格信息
for ($i = 2;$i = count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i – 2] as $key=$value) {
$excel-getActiveSheet()-setCellValue(“$letter[$j]$i”,”$value”);
$j++;
}
}
PHP導出Excel,PHP輸入Excel
6.
//創建Excel輸入對象
$write = new PHPExcel_Writer_Excel5($excel);
header(“Pragma: public”);
header(“Expires: 0”);
header(“Cache-Control:must-revalidate, post-check=0, pre-check=0”);
header(“Content-Type:application/force-download”);
header(“Content-Type:application/vnd.ms-execl”);
header(“Content-Type:application/octet-stream”);
header(“Content-Type:application/download”);;
header(‘Content-Disposition:attachment;filename=”testdata.xls”‘);
header(“Content-Transfer-Encoding:binary”);
$write-save(‘php://output’);
PHP導出Excel,PHP輸入Excel
7.打開頁面,刷新的時候會彈出對話框,讓你選擇文件保存路徑和文件名稱,我直接放在了桌面上,如圖所示;
PHP導出Excel,PHP輸入Excel
PHP導出Excel,PHP輸入Excel
8.打開表格後,數據和格式跟代碼中的一致,說明PHP導出的Excel是正確的。如果出現錯誤,檢查一下你的表格數組和數據數組吧。
PHP導出Excel,PHP輸入Excel
php導出excel表、壓縮成文件下載到本地如何實現?
你好!你所提出的三個問題.我沒怎麼看懂.如果以第二個為主.我倒可以說說我的看法.
一.如何把數據賦到excel中?
答:sorry.沒怎麼明白.見諒.
二.導出為excel表?
答:有兩種方法實現.1.phpexcel.(稍顯複雜)
2.簡單的(我講這個.簡單^_^)直接上代碼.自己改一下名字
和字段名.就成.
?php
error_reporting(0);
//屏蔽警告和NOTICE等所有提示.包括error
Header(
“Content-type:
application/octet-stream
“);
Header(
“Accept-Ranges:
bytes
“);
Header(
“Content-type:application/vnd.ms-excel;charset=Big5”);
//此處寫編碼,如,UTF-8….
Header(
“Content-Disposition:attachment;filename=abnormal_Report.xls
“);
//自己寫文件名
*.xls
require
“conn_mysql.php”;
//連接mysql
$sql
=
“select
*
from
`netart`.`abnormal_records`
order
by
record_abtime
desc”;
$result
=
mysql_query($sql,$conn);
echo
“table
width=’100%’
border=’1′
“;
echo”tr”;
echo
“td
style=’color:red’
font
size=4
ID
/font/td”;
echo
“td
style=’color:red’
font
size=4異常時間
/font/td”;
echo
“td
style=’color:red’
font
size=4異常地點
/font/td”;
echo
“td
style=’color:red’
font
size=4詳細內容
/font/td”;
echo
“td
style=’color:red’
font
size=4提交人
/font/td”;
echo
“td
style=’color:red’
font
size=4提交時間
/font
/td”;
echo
“/tr”;
while
($rs=mysql_fetch_array($result)){
echo
“tr”;
echo
“td
width=’30’
{$rs[‘record_id’]}/td”;
//用width
控制表格的寬度.自己改變.
echo
“td
width=’150′
{$rs[‘record_abtime’]}/td”;
echo
“td
width=’80’
{$rs[‘record_abplace’]}/td”;
echo
“td
width=’700′
{$rs[‘record_content’]}
/td”;
echo
“td
width=’60’
{$rs[‘record_username’]}
/td”;
echo
“td
width=’120′
{$rs[‘record_uptime’]}
/td”;
echo
“/tr”;
}
echo
“/tbale”;
?
//以上代碼.自己去改一下名字.和字段名就可以運行了.
==========================================================================
下面的代碼針對MSSQL:(基本跟
Mysql一樣啦.只是改用了ODBC)
?php
error_reporting(0);
Header(
“Content-type:
application/octet-stream”);
Header(
“Accept-Ranges:
bytes
“);
Header(
“Content-type:application/vnd.ms-excel;charset=Big5”);
Header(
“Content-Disposition:attachment;filename=Syslog_view.xls
“);
require
“conn_mssql.php”;
session_start();
$flag1=@$_SESSION[‘flag_1’];
$flag2=@$_SESSION[‘flag_2’];
$flag3=@$_SESSION[‘flag_3’];
$content=@$_SESSION[‘content’];
$ip=@$_SESSION[‘ip’];
$content_2=@$_SESSION[‘content_2’];
$ip_2=@$_SESSION[‘ip_2’];
$time=@$_SESSION[‘time’];
if($flag1==1)
{
$sql_s=”select
SysLog.DateTime,SysLog.IP,SysLog.Message
from
NetPerfMon.dbo.SysLog
where
IP=’$ip’
and
convert(varchar(10),DateTime,120)=’$time’
order
by
DateTime
desc”;}
if($flag2==2)
{
$sql_s=”select
SysLog.DateTime,SysLog.IP,SysLog.Message
from
NetPerfMon.dbo.SysLog
where
IP=’$ip’
and
convert(varchar(10),DateTime,120)=’$time’
order
by
DateTime
desc”;}
if($flag3==3)
{$sql_s=”select
SysLog.DateTime,SysLog.IP,SysLog.Message
from
NetPerfMon.dbo.SysLog
where
IP=’$ip’
and
Message
like
‘%$content%’
and
convert(varchar(10),DateTime,120)=’$time’
order
by
DateTime
desc”;}
$res=odbc_do($link,$sql_s);
echo
“table
width=’100%’
border=’1′”;
echo”tr”;
echo
“td
style=’color:red’
font
size=4
DateTime
/font/td”;
echo
“td
style=’color:red’
font
size=4
Switch
IP
/font/td”;
echo
“td
style=’color:red’
font
size=4
Content/font/td”;
echo
“/tr”;
while
($rs=odbc_fetch_array($res))
{
echo
“tr”;
echo
“td
width=’130′
{$rs[‘DateTime’]}/td”;
echo
“td
width=’110′
{$rs[‘IP’]}/td”;
echo
“td
width=’800′
{$rs[‘Message’]}/td”;
echo
“/tr”;
}
echo
“/tbale”;
session_stop();
?
三.壓縮成文件下載到本地?
答:此處也沒怎麼明白.因為,你做個按鈕/鏈接至上面的代碼.不就可以保存成excel到本地了..還要做什麼壓縮呢.
綜:回答完畢.希望能幫到你.
php導出excel表後,打包成壓縮包,然後下載到本地如何實現?
用PHPExcel,PHPExcel是相當強大的 MS Office Excel 文檔生成類庫。
你上它的官/網把程序包下/載下來,裡面有 PHPExcel 的程序、還有30個實例程序和三個文檔。
看一下其中的開發文檔你就會用了。
讀取(這段在開發文檔里有的,在13頁):
require_once ‘../Classes/PHPExcel/IOFactory.php’;
$objReader = PHPExcel_IOFactory::createReader(‘Excel2007’);
$objReader-setReadDataOnly(true);
$objPHPExcel = $objReader-load(“test.xlsx”);
$objWorksheet = $objPHPExcel-getActiveSheet();
echo ‘table’ . “\n”;
foreach ($objWorksheet-getRowIterator() as $row) {
echo ‘tr’ . “\n”;
$cellIterator = $row-getCellIterator();
$cellIterator-setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
echo ‘td’ . $cell-getValue() . ‘/td’ . “\n”;
}
echo ‘/tr’ . “\n”;
}
echo ‘/table’ . “\n”;
?
php怎麼導出大量數據的Excel
php導出大量數據到Excel,可以通過生成多個Excel文件,然後壓縮成壓縮包解決。
方案是:假如我們數據庫有10w條數據,每2000條數據生成一個Excel文件,這樣每次只要從數據庫里查詢出2000條數據即可,一定要分頁去查詢。
原因:主要是數據庫性能和寫文件性能。分頁查詢可以解決數據庫壓力的問題, 生成多個文件可以解決單個文件太大,後期維護Excel文件的問題。
要注意的:
1. 在導出邏輯文件開頭,一定要聲明 set_time_limit(0) ,防止腳本超時;
2. 每個文件生成後,適當的sleep一下,讓程序休息一下下;
3. 因為一次導出最後要將生成的多個Excel文件打包成一個壓縮包,所以要刪除掉生成的Excel文件,節省服務器存儲空間;
下面是我實際工作中,寫的一個php導出大量數據到Excel的代碼,你可以參考一下:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/199959.html