php 操作excel文件的方法小结


Posted in PHP onDecember 31, 2009

一、php,不用COM,生成excel文件

<? 
header("Content-type:application/vnd.ms-excel"); 
header("Content-Disposition:filename=test.xls"); 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
?>

在php环境运行上面的代码,大家就可以看到浏览器询问用户是否下载excel文档,点击保存,硬盘上就多了一个excel的文件,使用excel打开就会看到最终的结果,怎么样不错吧。

其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每一列数据结束后加\t,每一行数据结束后加\n的方法echo出来,在php的开头用header("Content-type:application/vnd.ms-excel");表示输出的是excel文件,用header("Content-Disposition:filename=test.xls");表示输出的文件名为text.xls。这样就ok了。

我们更可以修改header让他输出更多格式的文件,这样php在处理各种类型文件方面就更加方便了.
二、用PHP将mysql数据表转换为excel文件格式

<?php 
$DB_Server = "localhost"; 
$DB_Username = "mydowns"; 
$DB_Password = ""; 
$DB_DBName = "mydowns"; 
$DB_TBLName = "user"; 
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) 
or die("Couldn@#t connect."); 
$Db = @mysql_select_db($DB_DBName, $Connect) 
or die("Couldn@#t select database."); 
$file_type = "vnd.ms-excel"; 
$file_ending = "xls"; 
header("Content-Type: application/$file_type"); 
header("Content-Disposition: attachment; filename=mydowns.$file_ending"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
$now_date = date(@#Y-m-d H:i@#); 
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date"; 
$sql = "Select * from $DB_TBLName"; 
$ALT_Db = @mysql_select_db($DB_DBName, $Connect) 
or die("Couldn@#t select database"); 
$result = @mysql_query($sql,$Connect) 
or die(mysql_error()); 
echo("$title\n"); 
$sep = "\t"; 
for ($i = 0; $i < mysql_num_fields($result); $i++) { 
echo mysql_field_name($result,$i) . "\t"; 
} 
print("\n"); 
$i = 0; 
while($row = mysql_fetch_row($result)) 
{ 
$schema_insert = ""; 
for($j=0; $j<mysql_num_fields($result);$j++) 
{ 
if(!isset($row[$j])) 
$schema_insert .= "NULL".$sep; 
elseif ($row[$j] != "") 
$schema_insert .= "$row[$j]".$sep; 
else 
$schema_insert .= "".$sep; 
} 
$schema_insert = str_replace($sep."$", "", $schema_insert); 
$schema_insert .= "\t"; 
print(trim($schema_insert)); 
print "\n"; 
$i++; 
} 
return (true); 
?>

三、PHP操作excel的一个例子(用COM对象生成excel)
这是对于那些只喜欢简单处理一下excel朋友来说的
<?php 
//定义一个excel文件 
$workbook = "C:/My Documents/test.xls"; 
$sheet = "Sheet1"; 
//生成一个com对象$ex 
$ex = new COM("Excel.sheet") or Die ("连不上!!!"); 
//打开一个excel文件 
$book = $ex->application->Workbooks->Open($workbook) or Die ("打不开!!!"); 
$sheets = $book->Worksheets($sheet); 
$sheets->activate; 
//获取一个单元格 
$cell = $sheets->Cells(5,5); 
$cell->activate; 
//给该单元格赋值 
$cell->value = 999; 
//保存为另一文件newtest.xls 
$ex->Application->ActiveWorkbook->SaveAs("newtest.xls"); 
//关掉excel,如果想看效果,则注释掉下面两行,由用户手动关掉excel 
$ex->Application->ActiveWorkbook->Close("False"); 
unset ($ex); 
?>

四、php生成EXCEL的东东
可以通过PHP来产生EXCEL档。
----------------------------
Excel Functions
----------------------------
将下面的代码存为excel.php ,然后在页面中包括进来
然后调用
1. Call xlsBOF()
2. 将一些内容写入到xlswritenunber() 或者 xlswritelabel()中.
3.然后调用 Call xlsEOF()
也可以用 fwrite 函数直接写到服务器上,而不是用echo 仅仅在浏览器上显示。
<?php 
// ----- begin of function library ----- 
// Excel begin of file header 
function xlsBOF() { 
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
return; 
} 
// Excel end of file footer 
function xlsEOF() { 
echo pack("ss", 0x0A, 0x00); 
return; 
} 
// Function to write a Number (double) into Row, Col 
function xlsWriteNumber($Row, $Col, $Value) { 
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
echo pack("d", $Value); 
return; 
} 
// Function to write a label (text) into Row, Col 
function xlsWriteLabel($Row, $Col, $Value ) { 
$L = strlen($Value); 
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
echo $Value; 
return; 
} 
// ----- end of function library ----- 
?> 
// 
// To display the contents directly in a MIME compatible browser 
// add the following lines on TOP of your PHP file: 
<?php 
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT"); 
header ("Cache-Control: no-cache, must-revalidate"); 
header ("Pragma: no-cache"); 
header (@#Content-type: application/x-msexcel@#); 
header ("Content-Disposition: attachment; filename=EmplList.xls" ); 
header ("Content-Description: PHP/INTERBASE Generated Data" ); 
// 
// the next lines demonstrate the generation of the Excel stream 
// 
xlsBOF(); // begin Excel stream 
xlsWriteLabel(0,0,"This is a label"); // write a label in A1, use for dates too 
xlsWriteNumber(0,1,9999); // write a number B1 
xlsEOF(); // close the stream 
?>
PHP 相关文章推荐
PHP Token(令牌)设计
Mar 15 PHP
PHP与MySQL开发中页面乱码的产生与解决
Mar 27 PHP
在MongoDB中模拟Auto Increment的php代码
Mar 06 PHP
PHP手机号码归属地查询代码(API接口/mysql)
Sep 04 PHP
php中设置index.php文件为只读的方法
Feb 06 PHP
php日历制作代码分享
Jan 20 PHP
PHP使用GIFEncoder类生成gif动态滚动字幕
Jul 01 PHP
php实现通用的从数据库表读取数据到数组的函数实例
Mar 21 PHP
PHP永久登录、记住我功能实现方法和安全做法
Apr 27 PHP
PHP中单例模式与工厂模式详解
Feb 17 PHP
php封装的验证码类分享
Feb 26 PHP
php写入txt乱码的解决方法
Sep 17 PHP
PHP编程过程中需要了解的this,self,parent的区别
Dec 30 #PHP
用php实现让页面只能被百度gogole蜘蛛访问的方法
Dec 29 #PHP
PHP类的使用 实例代码讲解
Dec 28 #PHP
php 多线程上下文中安全写文件实现代码
Dec 28 #PHP
PHP 获取目录下的图片并随机显示的代码
Dec 28 #PHP
phpMyAdmin链接MySql错误 个人解决方案
Dec 28 #PHP
php 需要掌握的东西 不做浮躁的人
Dec 28 #PHP
You might like
使用PHP生成二维码的两种方法(带logo图像)
2014/03/14 PHP
深入分析PHP引用(&amp;)
2014/09/04 PHP
php实现统计网站在线人数的方法
2015/05/12 PHP
php获取图片信息的方法详解
2015/12/10 PHP
实现JavaScript中继承的三种方式
2009/10/16 Javascript
游览器中javascript的执行过程(图文)
2012/05/20 Javascript
javascript与有限状态机详解
2014/05/08 Javascript
node.js中的fs.close方法使用说明
2014/12/17 Javascript
jquery实现弹出层效果实例
2015/05/19 Javascript
nodejs 中模拟实现 emmiter 自定义事件
2016/02/22 NodeJs
非常实用的js验证框架实现源码 附原理方法
2016/06/08 Javascript
简单实现JS倒计时效果
2016/12/23 Javascript
angular框架实现全选与单选chekbox的自定义
2017/07/06 Javascript
vue、react等单页面项目应该这样子部署到服务器
2018/01/03 Javascript
vue.js配合$.post从后台获取数据简单demo分享
2018/08/11 Javascript
支付宝小程序tabbar底部导航
2018/11/06 Javascript
JavaScript cookie原理及使用实例
2020/05/08 Javascript
Python字符串转换成浮点数函数分享
2015/07/24 Python
Python实现的微信公众号群发图片与文本消息功能实例详解
2017/06/30 Python
python中文分词教程之前向最大正向匹配算法详解
2017/11/02 Python
python中实现k-means聚类算法详解
2017/11/11 Python
python中lambda()的用法
2017/11/16 Python
Python实现桶排序与快速排序算法结合应用示例
2017/11/22 Python
python多行字符串拼接使用小括号的方法
2020/03/19 Python
Django Rest framework解析器和渲染器详解
2019/07/25 Python
Python使用百度翻译开发平台实现英文翻译为中文功能示例
2019/08/08 Python
使用Python的networkx绘制精美网络图教程
2019/11/21 Python
CSS3 mask 遮罩的具体使用方法
2017/11/03 HTML / CSS
美国地毯购买网站:Rugs USA
2019/02/23 全球购物
为什么要使用servlet
2016/01/17 面试题
十佳青年个人事迹材料
2014/01/28 职场文书
索赔员岗位职责
2015/02/15 职场文书
会计稽核岗位职责
2015/04/13 职场文书
Mysql8.0递归查询的简单用法示例
2021/08/04 MySQL
MySQL 用 limit 为什么会影响性能
2021/09/15 MySQL
Windows 11上手初体验:任务栏和开始菜单等迎来大改
2021/11/21 数码科技