惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

皮皮赖's Blog

Script @php think service:discover handling the post-autoload-dump event returned with error code 255 Centos7 网卡型号 网卡硬件型号 网卡芯片型号支持列表 - 皮皮赖's Blog uniapp IOS15、16 uni.scanCode 扫码后页面跳转卡死 - 皮皮赖's Blog Javascript导出数据至Excel JS导出数据至Excel - 皮皮赖's Blog golang 实现 SD-WAN IWAN 例子 简单例子 示例 宝塔面板邮局管理器Postfix无法启动,重装后不能正常运行Postfix修复 - 皮皮赖's Blog idea goland phpstorm pycharm IntelliJ 打开项目后,只显示文件,项目中的目录不显示 《掌上 WeGame》宣布退市,9月8日正式停运 - 皮皮赖's Blog 常见内网IP段,不跟公网冲突的内网IP段有那些? - 皮皮赖's Blog
PHP导出数据至Excel PHPExcel库使用方法 例子 示例 - 皮皮赖's Blog
博主: Reaper · 2023-02-19 · via 皮皮赖's Blog
  • 发布时间:
  • 19942 次浏览
  • 暂无评论
  • 1510字数
  • 分类: 破代码 踩坑记
  1. 首页
  2. 正文  

导出数据到 Excel 可以使用 PHPExcel 类库,以下是一个将数据导出到 Excel 文件的示例:

// 引入 PHPExcel 类库
require_once 'path/to/PHPExcel.php';

// 创建新的 Excel 文件和工作表对象
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();

// 设置表头
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', '姓名');
$sheet->setCellValue('C1', '邮箱');
$sheet->setCellValue('D1', '电话');

// 从数据库中查询数据
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$sql = 'SELECT id, name, email, phone FROM users';
$stmt = $pdo->query($sql);

// 遍历数据,并将其添加到 Excel 文件中
$row = 2;
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $sheet->setCellValue('A' . $row, $data['id']);
    $sheet->setCellValue('B' . $row, $data['name']);
    $sheet->setCellValue('C' . $row, $data['email']);
    $sheet->setCellValue('D' . $row, $data['phone']);
    $row++;
}

// 设置文件头,告诉浏览器这是一个 Excel 文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="users.xlsx"');
header('Cache-Control: max-age=0');

// 输出 Excel 文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

这个示例中,首先创建了一个新的 Excel 文件和工作表对象,然后设置了表头并从数据库中查询数据,最后将数据添加到 Excel 文件中。然后设置文件头,告诉浏览器这是一个 Excel 文件,并输出 Excel 文件。

赞赏作者

您的赞赏是对我最大的支持。

PHP导出数据至Excel PHPExcel库使用方法 例子 示例

 •