主页 M

原生php导出数据到excel,支持多数据类型

2022-05-08 网页编程网 网页编程网

1.简单生成xls

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=data.xls");
//输出内容如下:
echo "姓名"."\t";
echo ;"生日"."\t[这有一个空格,转字符显示长文本]";
echo "学历"."\t";
echo "\n";
echo "张三"."\t";
echo "1992-10-12"."\t";
echo "本科"."\t";

2.二维数组数据

ob_get_clean();
ob_start();

$lists[0]['phone']='1212121212';
$lists[0]['name']='asfsddsf';
$lists[1]['phone']='545454';
$lists[1]['name']='张四';
$lists[2]['phone']='989898';
$lists[2]['name']='tyty';
//生成文件名
$date = date("Y-m-d_H:i:s", time());
$fileName = "file_" . $date . ".xls";
//头部标题
$xlsx_header = ['手机号', '姓名'];

echo implode("\t", $xlsx_header),"\n";

foreach ($lists as $key => $value) {
    $data = [];
    $data[] = $value['phone'];
    $data[] = $value['name'];
    echo implode("\t", $data),"\n";
}
header('Content-Disposition:attachment;filename='.$fileName);
header('Accept-Ranges:bytes');
header('Content-Length:'.ob_get_length());
header('Content-Type:application/vnd.ms-excel');
ob_end_flush();

3.用html方式,用于报单输出

<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: inline; filename=filename.xls");
?>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<table>
<p>title</p>
<tr><td>sddsdssd</td><td>中文</td></tr>
<tr><td>111111</td><td>中文</td></tr>
</table>
</body>
</html>
阅读原文
阅读 2953
123 显示电脑版