
如何利用php语法从数据库中查询数据并生成excel表格并导出下载到本地,下面web建站小编给大家简单介绍一下具体实现代码!
代码如下:
//连接数据库
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
//查询数据库
$sql = "SELECT * FROM `table_name`";
$result = mysqli_query($conn, $sql);
//利用PHPExcel类库生成Excel表格
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("your name")
->setLastModifiedBy("your name")
->setTitle("title")
->setSubject("subject")
->setDescription("description")
->setKeywords("keywords")
->setCategory("category");
$objPHPExcel->setActiveSheetIndex(0);
$i = 1;
while ($row = mysqli_fetch_array($result)) {
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $row['column1']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $row['column2']);
$i++;
}
//输出Excel文件
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="filename.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
//下载Excel文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('file_path/filename.xls');
php删除接口:在指定的数据库数据表中根据ID删除指定的数据
标签: excel表格, PHPExcel类库, php语法
上面是“php语法如何生成excel表格并导出”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3972.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

Excel函数公式:用于排名的6个常用公式
seo小白必须了解的外链几种作弊行为的详细描述
一款准确率高达99%的AI内容检测工具——GPTZero