Ⅰ php導出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表、壓縮成文件下載到本地如何實現
你好!你所提出的三個問題.我沒怎麼看懂.如果以第二個為主.我倒可以說說我的看法.
一.如何把數據賦到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文件
昨天項目里有個新需求,客戶希望把一些數據能導出成為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文件
構造函數:
function down_xls($data, $keynames, $name='dataxls') {
$xls[] = "<html><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"><body><table border='1'>";
$xls[] = "<tr><td>ID</td><td>" . implode("</td><td>", array_values($keynames)) . '</td></tr>';
foreach($data As $o) {
$line = array(++$index);
foreach($keynames AS $k=>$v) {
$line[] = $o[$k];
}
$xls[] = '<tr><td>'. implode("</td><td>", $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'));
}
函數引用:
if(strval($_GET['download'])){
$orders = DB::LimitQuery('order', array(
'condition' => $condition,
'order' => 'ORDER BY id DESC',
));
if (!$orders) die('沒有符合條件的記錄');
$name = 'order_'.date('Ymd');
$kn = array( //excel表列名與數據欄位的對應關系
'id' => '訂單號',
'price' => '訂單金額',
'card' => '代金券',
'create_time' => '下單時間',
'pay_time' => '付款時間',
);
foreach( $orders AS $one ){
$one['create_time'] =date("Y-m-d",$one['create_time']);
$one['pay_time']=date("Y-m-d",$one['pay_time']);
$eorders[] = $one;
}
down_xls($eorders, $kn, $name);
}
Ⅳ 用PHP怎麼保存一段HTML代碼為excel文件並下載
這個簡單,但是有些繁瑣,關鍵代碼:
$filename=「頁面";
header("Content-type:application/vnd.ms-word");
header("Content-Disposition:attachment;filename=".$filename.".xls");
echo "<table width='608' border='0' align='center' cellpadding='0' cellspacing='0'>";
...
需要保存的內容都用echo輸出就OK了,你可以試試(我的是輸出一個表格的例子)