Ⅰ 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了,你可以试试(我的是输出一个表格的例子)