很多站长朋友们都不太清楚php压缩excel,今天小编就来给大家整理php压缩excel,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php导出excel表后,打包成压缩包,然后下载到本地如何实现? 2、 php导出excel表、压缩成文件下载到本地如何实现? 3、 php怎么导出大量数据的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表、压缩成文件下载到本地如何实现?你好!你所提出的三个问题.我没怎麼看懂.如果以第二个为主.我倒可以说说我的看法.
一.如何把数据赋到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怎么导出大量数据的Excelphp导出大量数据到Excel,可以通过生成多个Excel文件,然后压缩成压缩包解决。
方案是:假如我们数据库有10w条数据,每2000条数据生成一个Excel文件,这样每次只要从数据库里查询出2000条数据即可,一定要分页去查询。
原因:主要是数据库性能和写文件性能。分页查询可以解决数据库压力的问题, 生成多个文件可以解决单个文件太大,后期维护Excel文件的问题。
要注意的:
1. 在导出逻辑文件开头,一定要声明 set_time_limit(0) ,防止脚本超时;
2. 每个文件生成后,适当的sleep一下,让程序休息一下下;
3. 因为一次导出最后要将生成的多个Excel文件打包成一个压缩包,所以要删除掉生成的Excel文件,节省服务器存储空间;
下面是我实际工作中,写的一个php导出大量数据到Excel的代码,你可以参考一下:
关于php压缩excel的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php压缩excel PHP压缩图片类的详细内容...