Loading...

Pear SpreadSheet 真是个好东西

2006-11-16 17:52:30 发表于HTML客户端, 网站技术 本文链接: Pear SpreadSheet 真是个好东西

在网上搜索到的PHP写excel文件或者word文件的方法无非就两种,一种是真正的调用com,另外一种是通过header欺骗浏览器,使之认为即将下载的文件的minetype为ms-word或者ms-excel,实际发送的是html文件或者cvs的文本文件。

调用com涉及到服务器平台的问题,欺骗浏览器的问题是排版可能不会和你想象的一样,特别是有表格的时候,打印预览简直一塌糊涂。

当然我也发现一个更好的方法来生成word文件,那就是你先用word做好模板,然后另存为html文件,打开html文件,用php替代里面的数据,或者循环里面的数据,这是我认为我发现最好的方法来制作word文件了。

说了这么多废话,不得不提及Excel和 Pear SpreadSheet了,这真是一个好东西。通过pear的这个包,我们可以满足写xls的大部分功能。下面是一个简单的例子,具体操作请参考pear manual

require_once (‘PEAR.php’);

require_once ‘Spreadsheet/Excel/Writer.php’;

// Creating a workbook

$workbook = new Spreadsheet_Excel_Writer();

$format =& $workbook->addFormat(array(‘Size’ => 10,

‘Align’ => ‘center’,

‘Color’ => ‘black’,

‘Pattern’ => 1,

‘FgColor’ => ‘white’,

‘Border’=>1,

‘HAlign’=>’Left’,

‘Align’=>’merge’));

$format_warning =& $workbook->addFormat(array(‘Size’ => 10,

‘Align’ => ‘center’,

‘Color’ => ‘red’,

‘Pattern’ => 1,

‘FgColor’ => ‘white’,

‘Border’=>1,

‘HAlign’=>’Left’,

‘Align’=>’merge’));

$format_underline =& $workbook->addFormat(array(‘Size’ => 10,

‘Align’ => ‘center’,

‘Color’ => ‘black’,

‘Pattern’ => 1,

‘FgColor’ => ‘white’,

‘Border’=>1,

‘HAlign’=>’Left’,

‘Top’=>0,

‘Left’=>0,

‘Bottom’=>1,

‘right’=>0,

‘Align’=>’merge’));

$format_noborder =& $workbook->addFormat(array(‘Size’ => 10,

‘Border’=>0,

‘HAlign’=>’Left’,

‘Align’=>’merge’));

$format_title =& $workbook->addFormat(array(‘Size’ => 20,

‘Border’=>0,

‘HAlign’=>’Left’,

‘Align’=>’merge’,

‘Shadow’=>1,

‘VAlign’=>’Top’));

$file_name = “客人名单(“.$_GET[‘line_name’].”).xls”;

$workbook->send($file_name);

$worksheet =& $workbook->addWorksheet(‘客人名单’);

$worksheet->setInputEncoding(‘gb2312’);

//title

$worksheet->write (0, 0,”客人名单”,$format_title);

$worksheet->write(0, 1,””,$format_title);

$worksheet->write (0, 2,””,$format_title);

$worksheet->write (0, 3,””,$format_title);

$worksheet->write (0, 4,””,$format_title);

$worksheet->write (0, 5,””,$format_title);

//end of title

$worksheet->writeString(1, 0, ‘序号’,$format);

$worksheet->writeString(1, 1, ‘姓名’,$format);

$worksheet->writeString(1, 2, ‘身份证’,$format);

$worksheet->writeString(1, 3, ‘序号’,$format);

$worksheet->writeString(1, 4, ‘姓名’,$format);

$worksheet->writeString(1, 5, ‘身份证’,$format);

$worksheet->setColumn (2, 2,22 );

$worksheet->setColumn (5, 5,22 );

$worksheet->setColumn (0, 0,4 );

$worksheet->setColumn (3, 3,4 );

$worksheet->setColumn (1, 1,10 );

$worksheet->setColumn (4, 4,10 );

$workbook->close();

?>

该日志未加标签
发表于 2006-11-16 17:52:30 目录:HTML客户端, 网站技术 [RSS 2.0] 您可以评论. Pinging 不可用.
  • 相关阅读
  • homezz 美国专业主机商
    板凳还空着的
    评论分页: 1
    (Required)
    (Required, not published)
    如果留言未显示无需重复留言,我将为你恢复!