不使用PHPExcel导出Excel文件

2019/07/22 posted in  Tips
$table = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><style type="text/css">
.excelTable  {border-collapse:collapse;border:thin solid #999;}
.excelTable  th {border: thin solid #999;padding:20px;text-align: center;border-top: thin solid #999;background-color: #E6E6E6;}
.excelTable  td {border:thin solid #999;padding:2px 5px;text-align: center;}
</style></head><body>';
$table .= "<table>
            <thead>
                <tr>
                    <th class='name'>追逆ID</th>
                    <th class='name'>用户ID</th>
                    <th class='name'>状态</th>
                    <th class='name'>创建时间</th>
                    <th class='name'>问题描述</th>
                    <th class='name'>问题类型</th>
                    <th class='name'>问题图片1</th>
                    <th class='name'>问题图片2</th>
                    <th class='name'>问题地址</th>
                    <th class='name'>投诉人电话</th>
                    <th class='name'>备注</th>
                </tr>
            </thead>
            <tbody>";
foreach ($innerdata as $k => $v) {
    $id = $v['id'];
    $userid = $v['users_id'];
    $status = $v['status']==1?'已过审':'未过审';
    $create_time = $v['create_time'];
    $qcon = $v['qcon'];
    $qid = getQName($v['qid']);
    $photo1 = $v['photo1'];
    $address = $v['address'];
    $phone = $v['phone'];
    //$unionid = $v['wx_unionid'];
    $unionid = $v['users_id'];
    $tag = $v['tag'];

    $tmp = json_decode($v['photo2']);
    $ttmp = '';
    foreach ($tmp as $v){
        $ttmp .= $v.',';
    }
    $photo2 = $ttmp;

    $table .= "<tr>
                    <td class='name'>{$id}</td>
                    <td class='name'>{$userid}</td>
                    <td class='name'>{$status}</td>
                    <td class='name'>{$create_time}</td>
                    <td class='name'>{$qcon}</td>
                    <td class='name'>{$qid}</td>
                    <td class='name'>{$photo1}</td>
                    <td class='name'>{$photo2}</td>
                    <td class='name'>{$address}</td>
                    <td class='name'>{$phone}</td>
                    <td class='name'>{$tag}</td>
                </tr>";
}
$table .= "</tbody>
        </table>
        </body>
        </html>";

//通过header头控制输出excel表格
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="百日攻坚.xls"');
header("Content-Transfer-Encoding:binary");
echo $table;