美文网首页PHP程序猿程序员
PHP导出带样式的Excel

PHP导出带样式的Excel

作者: 新亮笔记 | 来源:发表于2016-08-29 19:38 被阅读688次

工作中做导出的时候,需要导出自定义的表格或嫌弃导出的Excel格式太难看了。
需要设置颜色、字号大小、加粗、合并单元格等等。

效果图:

PHP导出带样式的Excel

PHP代码:

/**
 * 导出文件
 * @return string
 */
public function export()
{
    $file_name   = "成绩单-".date("Y-m-d H:i:s",time());
    $file_suffix = "xls";
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$file_name.$file_suffix");
    //根据业务,自己进行模板赋值。
    $this->display();
}

HTML代码:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
</head>

<body>
<table border=1 cellpadding=0 cellspacing=0 width="100%" >
     <tr>
         <td colspan="5" align="center">
             <h2>成绩单</h2>
         </td>
     </tr>
     <tr>
         <td style='width:54pt' align="center">编号</td>
         <td style='width:54pt' align="center">姓名</td>
         <td style='width:54pt' align="center">语文</td>
         <td style='width:54pt' align="center">数学</td>
         <td style='width:54pt' align="center">英语</td>
     </tr>

     <tr>
        <td align="center">1</td>
        <td style="background-color: #00CC00;" align="center">Jone</td>
        <td style="background-color: #00adee;" align="center">90</td>
        <td style="background-color: #00CC00;" align="center">85</td>
        <td style="background-color: #00adee;" align="center">100</td>
     </tr>

    <tr>
        <td align="center">2</td>
        <td style="background-color: #00CC00;" align="center">Tom</td>
        <td style="background-color: #00adee;" align="center">99</td>
        <td style="background-color: #00CC00;" align="center">85</td>
        <td style="background-color: #00adee;" align="center">80</td>
    </tr>

</table>
</body>
</html>

Thanks ~

PHP工程师

相关文章

网友评论

  • 阿里困啊:这个是把前端页面导出表格,那我现在没有这个前端页面,就是把数据库中的数据导出表格,需要怎么弄
    新亮笔记:@阿里困啊 导出csv效率高,如果使用第三方的话,那就PHPExcel
    阿里困啊:@壁虎 导出csv我也做过,但是我最开始是想导出表格的,没解决了才转而求其次导出csv的,那个如果要实现的话是不是要第三发的插件
    新亮笔记:@阿里困啊 导出csv 即可

本文标题:PHP导出带样式的Excel

本文链接:https://www.haomeiwen.com/subject/cyffettx.html