Melody_Zxy 2021-09-15 16:13 采纳率: 0%
浏览 12

tp5+phpExcel导出报错 有知道咋回事么

img



```php
use PHPExcel;
use PHPExcel_IOFactory;


public function export(){
        $objExcel = new PHPExcel();
        $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');

        $objActSheet = $objExcel->getActiveSheet(0);
        $objActSheet->setTitle('领取报表');//设置excel的标题
        $objActSheet->setCellValue('A1', '编号');
        $objActSheet->setCellValue('B1', '姓名');
        $objActSheet->setCellValue('C1', '电话');
        $objActSheet->setCellValue('D1', '区域');
        $objActSheet->setCellValue('E1', '房型');
        $objActSheet->setCellValue('F1', '面积');
        $objActSheet->setCellValue('G1', '总房款');
        $objActSheet->setCellValue('H1', '关注楼盘');
        $objActSheet->setCellValue('I1', '选填楼盘');
        $objActSheet->setCellValue('J1', '添加时间');

        if (!empty($post['pstart_time']) and !empty($post['pend_time']) ) {
            $where['create_time'] = array(
                array('egt', strtotime($post['pstart_time'])),
                array('lt',strtotime($post['pend_time']))
            );
        }
        $data = empty($where) ? Db::name('send')->order('create_time desc')->select() : Db::name('send')->where($where)->order('create_time desc')->select();
        $baseRow = 2; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
        foreach ( $data as $r => $d ) {
            $i = $baseRow + $r;
            $objExcel->getActiveSheet()->setCellValue('A' . $i, $d['id']);
            $objExcel->getActiveSheet()->setCellValue('B' . $i, $d['name']);
            $objExcel->getActiveSheet()->setCellValue('C' . $i, $d['tel']);
            $objExcel->getActiveSheet()->setCellValue('D' . $i, $d['sxregion_id']);
            $objExcel->getActiveSheet()->setCellValue('E' . $i, $d['sxhouse_id'].$d['houseOther']);
            $objExcel->getActiveSheet()->setCellValue('F' . $i, $d['sxarea_id']);
            $objExcel->getActiveSheet()->setCellValue('G' . $i, $d['sxtotal_id']);
            $objExcel->getActiveSheet()->setCellValue('H' . $i, $d['sxbuild_id']);
            $objExcel->getActiveSheet()->setCellValue('I' . $i, $d['house1'].$d['house2'].$d['house3']);
            $objExcel->getActiveSheet()->setCellValue('J' . $i, date("Y-m-d H:i:s",$d['create_time']));
        }
        $objExcel->setActiveSheetIndex(0);
        //4、输出
        $objExcel->setActiveSheetIndex();
        header('Content-Type: applicationnd.ms-excel');
        $time=date('Y-m-d');
        header("Content-Disposition: attachment;filename=导出$time.xls");
        header('Cache-Control: max-age=0');
        header ('Pragma: public');
        $objWriter->save('php://output');
    }


  • 写回答

1条回答 默认 最新

  • Jason Ho 2021-09-15 18:19
    关注

    太复杂了,参考下这个标准范例吧,不要直接输出,用文件接受

     $objPHPExcel = new \PHPExcel();
     $sheet = $objPHPExcel->setActiveSheetIndex(0);
    
     $titleExcelss = [
                [
                    'Handle',
                    'Title',
                    'Body (HTML)',
                    'Vendor',
                    'Type',
                    'Tags',
                    'Published',
                    'Option1 Name',
                    'Option1 Value',
                    'Option2 Name',
                    'Option2 Value',
                    'Option3 Name',
                    'Option3 Value',
                    'Variant SKU',
                    'Variant Grams',
                    'Variant Inventory Tracker',
                    'Variant Inventory Qty',
                    'Variant Inventory Policy',
                    'Variant Fulfillment Service',
                    'Variant Price',
                    'Variant Compare At Price',
                    'Variant Requires Shipping',
                    'Variant Taxable',
                    'Variant Barcode',
                    'Image Src',
                    'Image Position',
                    'Image Alt Text',
                    'Gift Card',
                    'SEO Title',
                    'SEO Description',
                    'Google Shopping / Google Product Category',
                    'Google Shopping / Gender',
                    'Google Shopping / Age Group',
                    'Google Shopping / MPN',
                    'Google Shopping / AdWords Grouping',
                    'Google Shopping / AdWords Labels',
                    'Google Shopping / Condition',
                    'Google Shopping / Custom Product',
                    'Google Shopping / Custom Label 0',
                    'Google Shopping / Custom Label 1',
                    'Google Shopping / Custom Label 2',
                    'Google Shopping / Custom Label 3',
                    'Google Shopping / Custom Label 4',
                    'Variant Image',
                    'Variant Weight Unit',
                    'Variant Tax Code',
                    'Cost per item',
                    'Status'
                ]
            ];
            $count = count($titleExcelss[0]);
            $sheet->getStyle('A1:AV1')->getFont()->setSize(12);//字体大小
            foreach ($titleExcelss as $ki => $node) {
                $k = 'A';
                foreach ($node as $one) {
                    $sheet->setCellValue($k . strval($ki + 1), $one);
                    #$sheet->getStyle('A1:AV1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居中
                    $sheet->getColumnDimension($k)->setWidth(25);#设置列的宽度
                    $k++;
                }
    
            }
            $startRow = 1;
            foreach ($new_list_array as $ki => $node) {
                $startRow++;
                $m = 'A';
                for ($i = 0; $i < $count; $i++) {
                    $sheet->setCellValue($m . $startRow, $node[$i] ?? 0);
                    $m++;
                }
    
            }
            $path = ROOT_PATH . 'goods' . rand(1, 3333333333333) . '.xls';
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save($path);
    
    评论

报告相同问题?

问题事件

  • 创建了问题 9月15日

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法