php Spreadsheet 导出功能

这里需要注意的是 $col = 1 ,必须要等于1,如果设置为0的话,首个字段导出文件的时候 跑到最后去了
在这里插入图片描述

    public function export_data($ids = ''){
        set_time_limit(0);

        $row = $this->model->get($ids);

        $excel = new Spreadsheet();
        $excel->getProperties()
            ->setCreator("KAE")
            ->setLastModifiedBy("KAE")
            ->setTitle("KAE")
            ->setSubject("KAE");
        $excel->getDefaultStyle()->getFont()->setName('Microsoft Yahei');
        $excel->getDefaultStyle()->getFont()->setSize(12);



        $excel->getDefaultStyle()->applyFromArray(
            array(
                'fill'      => array(
                    'type'  => Fill::FILL_SOLID,
                    'color' => array('rgb' => '000000')
                ),
                'font'      => array(
                    'color' => array('rgb' => "000000"),
                ),
                'alignment' => array(
                    'vertical'   => Alignment::VERTICAL_CENTER,
                    'horizontal' => Alignment::HORIZONTAL_CENTER,
                    'indent'     => 1
                ),
                'borders'   => array(
                    'allborders' => array('style' => Border::BORDER_THIN),
                )
            ));


        $worksheet = $excel->setActiveSheetIndex(0);
        $worksheet->setTitle('DOM项目:'.$row['name']);


        $line = 1;
        $list = [];
        \app\admin\model\produce\Domsku::where('produce_dom_id',$ids)
            ->field('id,sku,goods_name,specification_name,unit_name,number,mark')
            ->chunk(100, function ($items) use (&$list, &$line, &$worksheet) {

                $list = $items = collection($items)->toArray();
                foreach ($items as $index => $item) {
                    unset($item['id']);
                    $line++;
                    $col = 1;
                    foreach ($item as $field => $value) {
                        $worksheet->setCellValueByColumnAndRow($col, $line, $value);
                        $worksheet->getStyleByColumnAndRow($col, $line)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
                        $col++;
                    }
                }

            });

        $domskuFormList = (new \app\admin\model\produce\Domsku())->getDomSkuLangList();
        $first = array_keys($list[0]);

        $fields = [];
        foreach ($first as $k => $ele) {
            $fields[] = $domskuFormList[$ele]??'';
        }
        foreach ($fields as $index => $item) {
            $worksheet->setCellValueByColumnAndRow($index, 1, __($item));
        }
        $excel->createSheet();
        $title = 'DOM-'.$row['name'].'-'.date("Y-m-d H-i-s");

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0


        $objWriter = IOFactory::createWriter($excel, 'Xlsx');
        $objWriter->save('php://output');
        return;


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值