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 '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . $strTable . '</html>'; }
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 ='<table width="500" border="1">'; $strTable .= '<tr>'; $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">ID</td>'; $strTable .= '<td style="text-align:center;font-size:12px;" width="*">用户账号</td>'; $strTable .= '<td style="text-align:center;font-size:12px;" width="*">昵称</td>'; $strTable .= '<td style="text-align:center;font-size:12px;" width="*">时间</td>'; $strTable .= '</tr>'; $aa = db("user")->select(); foreach ($aa as $k => &$v) { $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']); } foreach($aa as $k=>$val){ $strTable .= '<tr>'; $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['user_id'].'</td>'; $strTable .= '<td style="text-align:left;font-size:12px; vnd.ms-excel.numberformat:@">'.$val['tel'].'</td>'; $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ni_name'].' </td>'; $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>'; $strTable .= '</tr>'; } $strTable .='</table>'; downloadExcel($strTable,'用户列表'); exit(); }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.
