案例
导出对应的报价记录
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;
}
导出成功