Home > PHP Framework > ThinkPHP > Use thinkphp5 to export excel files

Use thinkphp5 to export excel files

(*-*)浩
Release: 2020-01-07 17:52:39
Original
3007 people have browsed it

Use thinkphp5 to export excel files

Hello! Some people think that Excel export is complicated, but it is actually very simple. You can export the data you want in two steps, so give it a try.

The first step is to configure excel into the common configuration of the TP framework (Recommended learning: thinkphp5)

// 应用公共文件
/**
 * 导出excel
 * @param $strTable    表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable, $filename)
{
    header("Content-type: application/vnd.ms-excel");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=" . $filename . "_" . date('Y-m-d') . ".xls");
    header('Expires:0');
    header('Pragma:public');
    echo &#39;<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />&#39; . $strTable . &#39;</html>&#39;;
}
Copy after login

Second step controller code

You can query the data you want to report and just traverse it in the following td. Simple and convenient, have you learned it?

public function excel_daochu(){

        $strTable =&#39;<table width="500" border="1">&#39;;
        $strTable .= &#39;<tr>&#39;;
        $strTable .= &#39;<td style="text-align:center;font-size:12px;width:120px;">ID</td>&#39;;
        $strTable .= &#39;<td style="text-align:center;font-size:12px;" width="*">用户账号</td>&#39;;
        $strTable .= &#39;<td style="text-align:center;font-size:12px;" width="*">昵称</td>&#39;;
        $strTable .= &#39;<td style="text-align:center;font-size:12px;" width="*">时间</td>&#39;;
        $strTable .= &#39;</tr>&#39;;
            $aa = db("user")->select();

   foreach ($aa as $k => &$v) {
               $v[&#39;add_time&#39;] = date(&#39;Y-m-d H:i:s&#39;,$v[&#39;add_time&#39;]);
            }
          
        foreach($aa as $k=>$val){
            $strTable .= &#39;<tr>&#39;;
            $strTable .= &#39;<td style="text-align:center;font-size:12px;">&#39;.$val[&#39;user_id&#39;].&#39;</td>&#39;;
            $strTable .= &#39;<td style="text-align:left;font-size:12px; vnd.ms-excel.numberformat:@">&#39;.$val[&#39;tel&#39;].&#39;</td>&#39;;
            $strTable .= &#39;<td style="text-align:left;font-size:12px;">&#39;.$val[&#39;ni_name&#39;].&#39; </td>&#39;;
            $strTable .= &#39;<td style="text-align:left;font-size:12px;">&#39;.$val[&#39;add_time&#39;].&#39;</td>&#39;;
            $strTable .= &#39;</tr>&#39;;
        }

        $strTable .=&#39;</table>&#39;;
        downloadExcel($strTable,&#39;用户列表&#39;);
        exit();
    }
Copy after login

The above is the detailed content of Use thinkphp5 to export excel files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
tp5
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Error running tp5 on tomcat
From 1970-01-01 08:00:00
0
0
0
tp5 video upload
From 1970-01-01 08:00:00
0
0
0
TP5 paging always has problems
From 1970-01-01 08:00:00
0
0
0
Set up that tp5.com
From 1970-01-01 08:00:00
0
0
0
tp5 search paging
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template