您的位置:首页 > 编程语言 > PHP开发

TP5.1 使用PhpSpreadsheet插件导出excel文件出现乱码问题如何解决啊?

2018-11-22 10:38 1971 查看

html页面里我ajax条件过去,是不是ajax返回和excel导出数据冲突了?,如果是这样该如何解决啊?

已经查询出需要打印的二维数组了

php代码如下:

[code]$head = ['订单号','品牌名称','产品名称','产品类型','工艺','颜色','下单时间','出货时间','审核状态','结算状态','出货状态','备注'];
        $keys = ['order_num','username','pro_name','mold','technics','colour','order_time','shipment_time','audit','settlement','shipment','remarks'];

        dump($orderList);die;

        $count = count($head);  //计算表头数量
        // dump($count);die;
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        // dump($sheet);die;
        for ($i = 65; $i < $count + 65; $i++) {     //数字转字母从65开始,循环设置表头:
            $sheet->setCellValue(strtoupper(chr($i)) . '1', $head[$i - 65]);
        }

        /*--------------开始从数据库提取信息插入Excel表中------------------*/
        $name = '订单表'; 
        foreach ($orderList as $key => $item) {             //循环设置单元格:
            //$key+2,因为第一行是表头,所以写到表格时   从第二行开始写
            // dump($key);die;
            for ($i = 65; $i < $count + 65; $i++) {     //数字转字母从65开始:
                $sheet->setCellValue(strtoupper(chr($i)) . ($key + 2), $item[$keys[$i - 65]]);
                $spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setWidth(20); //固定列宽
            }

        }
        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
        header('content-type:application/octet-stream');
        header('Cache-Control: max-age=0');
        header("Content-Type:application/force-download");
        header("Content-Type:application/download");
        header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //删除清空:
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;

可是出现了乱码,截图如下

请问大神们,该如何解决?急!!!

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: