您的位置:

php下载excel表格(php导出excel表格)

本文目录一览:

PHP请问我需要在页面上显示 EXCEL表格,并且能下载怎么做呢

$retdata=array(); //内容 //二维数组

$filename= "文件.csv";

//生成头文件

$now = gmdate("D, d M Y H:i:s");

header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");

header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");

header("Last-Modified: {$now} GMT");

header("Content-Type: application/force-download");

header("Content-Type: application/octet-stream");

header("Content-Type: application/download");

header("Content-Disposition: attachment;filename={$filename}");

header("Content-Transfer-Encoding: binary");

//生成表格

$title=array('"金额"','"会员"','"余额"'); //每列的标题

$str=implode(',',$title) . PHP_EOL;

foreach ($retdata as $row) {

// print_r($row);

$str_arr = array();

foreach ($row as $column) {

$str_arr[] = '"' . str_replace('"', '""', $column) . '"';

}

$str.=implode(',', $str_arr) . PHP_EOL;

}

print(chr(0xEF).chr(0xBB).chr(0xBF));

echo $str;

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";

?