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 socket方式提交的post详解
Jul 19 PHP
php 从数据库提取二进制图片的处理代码
Sep 09 PHP
PHP中的session永不过期的解决思路及实现方法分享
Apr 20 PHP
Trying to clone an uncloneable object of class Imagic的解决方法
Jan 11 PHP
解析Ubuntu下crontab命令的用法
Jun 24 PHP
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
Apr 08 PHP
php创建session的方法实例详解
Jan 27 PHP
PHP生成短网址方法汇总
Jul 12 PHP
php中Ioc(控制反转)和Di(依赖注入)
May 07 PHP
php脚本守护进程原理与实现方法详解
Jul 20 PHP
Yii2结合Workerman的websocket示例详解
Sep 10 PHP
tp5框架前台无限极导航菜单类实现方法分析
Mar 29 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数组函数序列之array_unshift() 在数组开头插入一个或多个元素
2011/11/07 PHP
10个实用的PHP正则表达式汇总
2014/10/23 PHP
Zend Framework动作助手Json用法实例分析
2016/03/05 PHP
Laravel中前端js上传图片到七牛云的示例代码
2017/09/04 PHP
JQuery筛选器全系列介绍
2013/08/27 Javascript
详解jquery中$.ajax方法提交表单
2014/11/03 Javascript
node.js中的http.createClient方法使用说明
2014/12/15 Javascript
Vue2.0实现1.0的搜索过滤器功能实例代码
2017/03/20 Javascript
Vue.js在使用中的一些注意知识点
2017/04/29 Javascript
基于JavaScript实现图片连播和联级菜单实例代码
2017/07/28 Javascript
Element-ui table中过滤条件变更表格内容的方法
2018/03/02 Javascript
NodeJS服务器实现gzip压缩的示例代码
2018/10/12 NodeJs
详解React项目如何修改打包地址(编译输出文件地址)
2019/03/21 Javascript
Vue动态生成表格的行和列
2019/07/18 Javascript
[42:36]DOTA2上海特级锦标赛B组败者赛 VG VS Spirit第二局
2016/02/26 DOTA
python中的多重继承实例讲解
2014/09/28 Python
Python中使用Beautiful Soup库的超详细教程
2015/04/30 Python
基于Python_脚本CGI、特点、应用、开发环境(详解)
2017/05/23 Python
浅析python打包工具distutils、setuptools
2018/04/20 Python
python实现点对点聊天程序
2018/07/28 Python
pytorch: Parameter 的数据结构实例
2019/12/31 Python
python统计字符串中字母出现次数代码实例
2020/03/02 Python
python tkinter之 复选、文本、下拉的实现
2020/03/04 Python
python的sys.path模块路径添加方式
2020/03/09 Python
tensorflow模型转ncnn的操作方式
2020/05/25 Python
最新pycharm安装教程
2020/11/18 Python
英国太阳镜品牌:Taylor Morris Eyewear
2018/04/18 全球购物
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
高中学生评语大全
2014/04/25 职场文书
会计专业应届生自荐信
2014/06/28 职场文书
2014年减负工作总结
2014/12/10 职场文书
运动会200米广播稿
2015/08/19 职场文书
vue点击弹窗自动触发点击事件的解决办法(模拟场景)
2021/05/25 Vue.js
万能密码的SQL注入漏洞其PHP环境搭建及防御手段
2021/09/04 SQL Server
TV动画《八十龟酱观察日记》第四季宣传PV公布
2022/04/06 日漫
python在package下继续嵌套一个package
2022/04/14 Python