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对字符串的递增运算分析
Aug 08 PHP
PHP计划任务、定时执行任务的实现代码
Apr 23 PHP
php调用方法mssql_fetch_row、mssql_fetch_array、mssql_fetch_assoc和mssql_fetch_objcect读取数据的区别
Aug 08 PHP
解析mysql left( right ) join使用on与where筛选的差异
Jun 18 PHP
PHP中的替代语法介绍
Jan 09 PHP
php实现俄罗斯乘法实例
Mar 07 PHP
PHP中is_file()函数使用指南
May 08 PHP
PHP实现实时生成并下载超大数据量的EXCEL文件详解
Oct 23 PHP
360搜索引擎自动收录php改写方案
Apr 28 PHP
PHP超低内存遍历目录文件和读取超大文件的方法
May 01 PHP
关于Yii中模型场景的一些简单介绍
Sep 22 PHP
利用PHP计算有多少小于当前数字的数字方法示例
Aug 26 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动态生成VRML网页
2006/10/09 PHP
php数组函数序列之in_array() - 查找数组中是否存在指定值
2011/11/07 PHP
php循环创建目录示例分享(php创建多级目录)
2014/03/04 PHP
php将字符串转换成16进制的方法
2015/03/17 PHP
PHP扩展框架之Yaf框架的安装与使用
2016/05/18 PHP
PHP执行普通shell命令流程解析
2020/08/24 PHP
客户端脚本中常常出现的一些问题和调试技巧
2007/01/09 Javascript
js focus不起作用的解决方法(主要是因为dom元素是否加载完成)
2010/11/05 Javascript
js禁止页面复制功能禁用页面右键菜单示例代码
2013/08/29 Javascript
jquery中animate动画积累的解决方法
2013/10/05 Javascript
jQuery判断checkbox是否选中的小例子
2013/12/02 Javascript
三种方式获取XMLHttpRequest对象
2014/04/21 Javascript
jQuery选择id属性带有点符号元素的方法
2015/03/17 Javascript
javascript实现控制的多级下拉菜单
2015/07/05 Javascript
D3.js实现折线图的方法详解
2016/09/21 Javascript
使用Vue动态生成form表单的实例代码
2018/04/26 Javascript
python中Genarator函数用法分析
2015/04/08 Python
利用Python的Twisted框架实现webshell密码扫描器的教程
2015/04/16 Python
python 简单的绘图工具turtle使用详解
2017/06/21 Python
python的变量与赋值详细分析
2017/11/08 Python
python 基本数据类型占用内存空间大小的实例
2018/06/12 Python
详解python函数的闭包问题(内部函数与外部函数详述)
2019/05/17 Python
Windows下pycharm创建Django 项目(虚拟环境)过程解析
2019/09/16 Python
用Python在Excel里画出蒙娜丽莎的方法示例
2020/04/28 Python
利用python下载scihub成文献为PDF操作
2020/07/09 Python
CSS3中的注音对齐属性ruby-align用法指南
2016/07/01 HTML / CSS
10 套华丽的CSS3 按钮小结
2012/10/03 HTML / CSS
HTML5 使用 sessionStorage 进行页面传值的方法
2018/07/02 HTML / CSS
欧舒丹英国官网:购买欧舒丹护手霜等明星产品
2017/01/17 全球购物
意大利男装网店:Vrients
2019/05/02 全球购物
Sisley法国希思黎美国官方网站:享誉全球的奢华植物美容品牌
2020/06/27 全球购物
二年级语文教学反思
2014/02/02 职场文书
授权委托书公证
2014/09/14 职场文书
新闻稿件写作技巧
2015/07/18 职场文书
python基础之while循环语句的使用
2021/04/20 Python
Python中OpenCV实现查找轮廓的实例
2021/06/08 Python