PHP输出CSV和EXCEL两种简单的方法

来源:互联网 发布:淘宝色差回复 编辑:程序博客网 时间:2024/06/04 19:47

1、定义header()头部输出格式

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

header("Content-Disposition:filename=php100.xls");

2、输出编码和支持的格式

(1)支持普通格式的CSV文本规范,以空格和换行来识别

常用:\t \n 填充空白和换行

(2)支持简单的HTML代码和表格规范

常用:table规范 ,表格合并规范,Font标签规范

<table>

    <tr>

      <td colspan="3"><fontcolor=red>PHP100财务统计</font></td>

    </tr>

    <tr>

      <td>t10</td><td>t11</td><td>t12</td>

    </tr>

    <tr>

      <td>t20</td><td>t21</td><td>t22</td>

    </tr>

  </table>

代码示例:

<?php
 header("Content-type:application/vnd.ms-excel");
 header("Content-Disposition:filename=mytest.xls");
 echo "A1\t B1\t C1\n";
 echo "A2\t B2\t C2\n";
 echo "A3\t B3\t C3\n";
 //也可以把HTML的表格代码输出
?>