您的位置:首页 > 编程语言 > PHP开发

[PHP Excel 输出]使用 PHP 输出带格式的 Excel 文件

2013-04-07 21:39 405 查看
工作中的一点心得,拿出来分享一下。

用 PHP 生成 Excel 文件早已不是什么难题了,但若生成的 Excel 文件没有格式(边框线、居中、字体等),其实就是生成文本文件再用 Excel 打开而已。
在 Windows 平台下,可能用 COM 组件可以做到这点,但在 Unix 类系统环境下就没这么幸运了。

首先看一下当时这个新闻吧: http://bbs.chinaunix.net/viewthread.php?tid=204738&highlight=excel [微软发Office免费许可 开放其XML文件格式]
基本上还是要感谢微软,否则白搭。
我做的也就是使用其 XML 格式。注意:也就是说,这种方法生成的文件可能只对 Office 2003 有效。

至于 Excel 的 XML 格式,各位可以自己仔细研究,新建一个工作簿,然后简单的设置一些单元格,输入一些内容,然后选择另存为,保存类型选择“XML表格”即可。
用文本编辑器打开刚才那个 XML 文件,看看它是怎么描述一个单元格的格式的。它也是用 <Style> 标记,分配不同的 id 值,然后在下面进行任意调用。

我这里介绍一个模板的应用,就是先做好 Excel 样表(因为大多数应用都是输出的文件事先已设计好格式了),然后存成“XML表格”格式文件,再用 PHP 配合 Smarty 来输出。
先看模板文件(excel.tpl)(注意,我用的标记是“<{”和“}>”)
在这个例子中,关键就是把循环显示员工信息部分用 <{section}> 来处理。
<?xml version="1.0"?>

<?mso-application progid="Excel.Sheet"?>

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

xmlns:o="urn:schemas-microsoft-com:office:office"

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"

xmlns:html="http://www.w3.org/TR/REC-html40">

<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">

<Author>Diana</Author>

<LastAuthor>Diana</LastAuthor>

<Created>2006-04-25T11:58:52Z</Created>

<LastSaved>2006-04-25T13:10:20Z</LastSaved>

<Version>11.5606</Version>

</DocumentProperties>

<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">

<WindowHeight>12495</WindowHeight>

<WindowWidth>16035</WindowWidth>

<WindowTopX>0</WindowTopX>

<WindowTopY>105</WindowTopY>

<ProtectStructure>False</ProtectStructure>

<ProtectWindows>False</ProtectWindows>

</ExcelWorkbook>

<Styles>

<Style ss:ID="Default" ss:Name="Normal">

<Alignment ss:Vertical="Center"/>

<Borders/>

<Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>

<Interior/>

<NumberFormat/>

<Protection/>

</Style>

<Style ss:ID="s21">

<Font ss:FontName="宋体" x:CharSet="134" ss:Size="18" ss:Bold="1"/>

</Style>

<Style ss:ID="s29">

<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>

<Borders>

<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>

<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>

<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>

<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>

</Borders>

</Style>

<Style ss:ID="s35">

<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>

<Borders>

<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>

<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>

<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>

<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>

</Borders>

<NumberFormat ss:Format="@"/>

</Style>

</Styles>

<Worksheet ss:Name="Sheet1">

<Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="21" x:FullColumns="1"

x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="17.25">

<Column ss:AutoFitWidth="0" ss:Width="36"/>

<Column ss:AutoFitWidth="0" ss:Width="69"/>

<Column ss:AutoFitWidth="0" ss:Width="78.75"/>

<Row ss:Height="22.5">

<Cell ss:StyleID="s21"><Data ss:Type="String">员工信息表</Data></Cell>

</Row>

<Row ss:AutoFitHeight="0"/>

<Row ss:AutoFitHeight="0">

<Cell ss:StyleID="s29"><Data ss:Type="String">序号</Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="String">工号</Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="String">姓名</Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="String">性别</Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="String">年龄</Data></Cell>

</Row>

<{* 在这里对要循环输出的数据使用 section 处理。*}>

<{section name=list loop=$Emps}>

<Row ss:AutoFitHeight="0">

<Cell ss:StyleID="s29"><Data ss:Type="Number"><{$smarty.section.customer.rownum}></Data></Cell>

<Cell ss:StyleID="s35"><Data ss:Type="String"><{$Emps[list].id}></Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="String"><{$Emps[list].name}></Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="String"><{$Emps[list].sexual}></Data></Cell>

<Cell ss:StyleID="s29"><Data ss:Type="Number"><{$Emps[list].age}></Data></Cell>

</Row>

<{/section}>

</Table>

</Worksheet>

</Workbook>

不一定要和我上面这个一模一样,以上我经过少许删减,比如我把最后的 <WorksheetOptions> 段给删掉了。

配套的 PHP 测试程序(excel.php):

<?php

// 实验资料,实际作业中,这里应该是从数据库取得资料

$emps[0]['id'] = '00001';

$emps[0]['name'] = 'ABC';

$emps[0]['sexual'] = '男';

$emps[0]['age'] = 28;

$emps[1]['id'] = '00002';

$emps[1]['name'] = 'BBC';

$emps[1]['sexual'] = '男';

$emps[1]['age'] = 23;

$emps[2]['id'] = '00003';

$emps[2]['name'] = 'CBA';

$emps[2]['sexual'] = '女';

$emps[2]['age'] = 20;

ini_set('include_path', '/data/website/htdocs/includes');

require_once('class.Smarty.php');

$smarty = new mySmarty();

$smarty->assign('Emps', $emps);

// 输出文件头,表明是要输出 excel 文件

header("Content-type: application/vnd.ms-excel");

header("Content-Disposition: attachment; filename=test.xml"); //<----- 这里,改成 test.xls 也可以,这样就可以直接用 Excel 打开了。不过本质还是 xml,用记事本打开就知道了。所以打开后,再另存一下,存为真正的 xls 格式。

$smarty->display('excel.tpl');

?>

复制代码

完工,放到服务器上跑一下吧。
麻烦的地方,就是 Excel 样表转换成 XML 并修改成 Smarty 模板那一步。
我上面那个例子相对来说简单,因为格式比较单一。更复杂点的报表就要麻烦点了,要在模板中一个个单元格自己去放置 StyleID 值了。
不过,总算能输出一份漂亮的带格式的 Excel 了,这点辛苦还是值得的。

上面那个例子的效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: