朱广源 2019-02-22 20:19
浏览 1274

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();
}

    //希望有大佬能告诉我哪里出了问题,谢谢
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算