设为首页 收藏本站
查看: 563|回复: 0

[经验分享] PHP 输出带格式的 Excel 文件

[复制链接]

尚未签到

发表于 2017-4-3 10:51:04 | 显示全部楼层 |阅读模式
  第一步:生成一个excel的xml模板,保存为excel-xml.tpl

<?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 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
    .id}</Data></Cell>
    <Cell ss:StyleID="s29"><Data ss:Type="String">{$Emps
      .name}</Data></Cell>
      <Cell ss:StyleID="s29"><Data ss:Type="String">{$Emps
        .sexual}</Data></Cell>
        <Cell ss:StyleID="s29"><Data ss:Type="Number">{$Emps
          .age}</Data></Cell>
          </Row>
          {/section}
          </Table>
          </Worksheet>
          </Workbook>
            说明:上面的模板里用到了smarty的标签,要使用上面的模板,请通过smarty来调用。
            第二步:
            生成一个测试用的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('Smarty.php');
          $smarty = new Smarty();
          $smarty->assign('Emps', $emps);
          // 输出文件头,表明是要输出 excel 文件
          header("Content-type: application/vnd.ms-excel");
          header("Content-Disposition: attachment; filename=test.xls");
          $smarty->display('excel-xml.tpl');
          ?>
            最后生成的excel效果:

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359486-1-1.html 上篇帖子: 用PHP进行图片POST上传 下篇帖子: php中数组和对象相互转换
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表