Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6536077
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类:

2008-10-16 11:13:48

使用PEAR Spreadsheet_Excel_Writer创建Excel,MS EXCEL不是必需的, OpenOffice Calc也是一个不错的选择。

安装pear:
双击php目录中的go-pear.bat,一路回车安装完毕。
双击PEAR_ENV.reg写入注册表信息。

在命令窗口输入如下命令安装OLE及Spreadsheet_Excel_Writer

$ pear install
$ pear install

生成一个简单的电子表格,代码如下:(test.php)

// Include PEAR::Spreadsheet_Excel_Writer
require_once "Spreadsheet/Excel/Writer.php";
 
// Create an instance
$xls =& new Spreadsheet_Excel_Writer();
 
// Send HTTP headers to tell the browser what's coming
$xls->send("test.xls");
 
// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet('Binary Count');
 
// Write some numbers
for ( $i=0;$i<11;$i++ ) {
 // Use PHP's decbin() function to convert integer to binary
 $sheet->write($i,0,decbin($i));
}
 
// Finish the spreadsheet, dumping it to the browser
$xls->close();
 
?>

在浏览器中输入上面test.php文件所在的地址,就可以用excel或者OpenOffice打开一个电子表格。

将生成的excel文档保存到文件系统。代码如下:

 
// 如果文件不存在
if ( !file_exists('sheets/binary.xls') ) {
 
// Include PEAR::Spreadsheet_Excel_Writer
require_once "Spreadsheet/Excel/Writer.php";
 
// Create an instance, passing the filename to create
$xls =& new Spreadsheet_Excel_Writer('sheets/binary.xls');
 
// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet('Binary Count');
 
// Write some numbers
 for ( $i=0;$i<11;$i++ ) {
   // Use PHP's decbin() function to convert integer to binary
   $sheet->write($i,0,decbin($i));
 }
 
// Finish the spreadsheet, dumping it to the browser
$xls->close();
}
 
?>

注意,在linux或Unix下,文件所在目录必须可读。

不是原文照译,参考原文地址如下:

 
=
阅读(4007) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~