weixin_43828869
2019-02-22 20:19 浏览 983

TP5.0 使用PhpSpreadsheet插件导出excel文件出现乱码问题希望大佬帮忙解决

html页面里我ajax条件过去,是不是ajax返回和excel导出数据冲突了?,如果是这样该如何解决啊?
public function excle_one()
{
$res= DataService::educe($this->table); //获取数据
$len = count($res); //计算有多少个数据
$data=$this->orderopt->excle_config(); //获取设置的单元格的值
$data_counts=count($data);
$orderoptions=$this->orderopt->orderoptions($this->table);
$comment_arr=array();
foreach ($orderoptions as $key=>$vo){
if($data[$vo["Field"]]==="on"){
$comment_arr[]=$vo["Comment"];
}
}
foreach ($orderoptions as $key=>$vo){
if($data[$vo["Field"]]==="on"){
$comment_arrS[]=$vo["Field"];
}
}
$spreadsheet = new Spreadsheet(); //实例化Spreadsheet对象
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->setCellValueByColumnAndRow(1, 1, '订单表'); //单元格数值列坐标 单元格的数字行坐标 单元格的值
foreach ($comment_arr as $comment_key=>$comment_val)
{
$worksheet->setCellValueByColumnAndRow($comment_key+1, 2, $comment_val);
}
//合并单元格
$worksheet->mergeCells('A1:H1');

    //设置单元格样式
    $worksheet->getStyle('A1')->getFont()->setSize(20); //设置A1的字体大小
    $worksheet->getStyle('A2:H2')->getFont()->setSize(13);//设置A2到H2的字体大小
    $worksheet->getDefaultColumnDimension()->setWidth(28);//设置宽度

        $j = 0;
        foreach ($comment_arrS as $comment_key=>$comment_val)
        {
            for ($i=0; $i < $len; $i++){
                $j=$i+3;
                $worksheet->setCellValueByColumnAndRow($comment_key+1,$j,$res[$i][$comment_val]);
            }
        }

   $styleArrayBody = [
        'borders' => [
            'allBorders' => [
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                'color' => ['argb' => '#7F7F7F'],
            ],
        ],
        'alignment' => [
            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
        ],
    ];
    $total_rows = $len + 2;
    //添加所有边框/居中
    $worksheet->getStyle('A1:H'.$total_rows)->applyFromArray($styleArrayBody);


    $file_name = date('Y-m-d', time()).rand(1000, 9999);

    $file_name = $file_name . ".xlsx";

ob_end_clean();

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$file_name.'"');
header('Cache-Control: max-age=0');
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit();
}

    //希望有大佬能告诉我哪里出了问题,谢谢
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐