PHP导出对应的数据

案例

导出对应的报价记录

1.首先在对应的js添加操作导出按钮

代码如下:

  buttons: [
{
               name: 'export',
               title: __('导出'),
               classname: 'btn btn-xs btn-primary',
               icon: 'fa fa-download',
               url: 'inquiry/export',
          },
]

2.在控制器里添加对应的代码

  public function export($ids)
    {


        set_time_limit(0);
        ini_set('memory_limit','2G');

        $excel = new Spreadsheet();
        $worksheet = $excel->setActiveSheetIndex(0);
        $worksheet->getColumnDimension('A')->setWidth(20);//设置宽度
        $worksheet->getColumnDimension('B')->setWidth(20);//设置宽度
        $worksheet->getColumnDimension('C')->setWidth(20);//设置宽度
        $worksheet->getColumnDimension('D')->setWidth(20);//设置宽度
        $worksheet->getColumnDimension('E')->setWidth(20);//设置宽度
        $worksheet->getStyle('I')->getAlignment()->setWrapText(true);

        $this->sharedStyle = new Style();
        $this->sharedStyle->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),
                )
            ));
        $line = 1;
        $arr_ids = [];
  
         //根据传来的$ids查询对应的数据
        $quotation = Db::name('quotation')->where('inquiry_id',$ids)->select();
          //循环数据到xlsx
        foreach($quotation as $k=>$val){

            $line++;
            $worksheet->setCellValueByColumnAndRow(1, $line, $val['company']);
            $worksheet->setCellValueByColumnAndRow(2, $line, $val['mobile']);
            $worksheet->setCellValueByColumnAndRow(3, $line, $val['name']);
            $worksheet->setCellValueByColumnAndRow(4, $line, $val['price']);
            $worksheet->setCellValueByColumnAndRow(5, $line,  date("Y-m-d H:i:s", $val['create_time']) );

        }
        //自定义设置表头
        $excel->getActiveSheet()->setCellValue('A1','公司名称');
        $excel->getActiveSheet()->setCellValue('B1','手机号');
        $excel->getActiveSheet()->setCellValue('C1','联系人');
        $excel->getActiveSheet()->setCellValue('D1','价格');
        $excel->getActiveSheet()->setCellValue('E1','报价时间');

        $excel->createSheet();
        $title = date("YmdHis");

        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');exit;
        return;

    }

导出成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值