1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
public function daochu() { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->setTitle('总分数表'); $worksheet->setCellValueByColumnAndRow(1, 1, '测试'); $worksheet->setCellValueByColumnAndRow(1, 2, '序号'); $worksheet->setCellValueByColumnAndRow(2, 2, '姓名'); $worksheet->setCellValueByColumnAndRow(3, 2, '年级'); $worksheet->setCellValueByColumnAndRow(4, 2, '总分数'); $worksheet->mergeCells('A1:E1'); $styleArray = [ 'font' => [ 'bold' => true ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, ], ]; $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
$worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14); ; $jzInfo=Db::query("select a.id,sum(score) sumscore,b.name,b.class from score a inner join student b on a.stu_id=b.id group by stu_id order by sumscore asc");
$len = count($jzInfo); $j = 0; for ($i=0; $i < $len; $i++) { $j = $i + 3; $worksheet->setCellValueByColumnAndRow(1, $j, $jzInfo[$i]['id']); $worksheet->setCellValueByColumnAndRow(2, $j, $jzInfo[$i]['name']); $worksheet->setCellValueByColumnAndRow(3, $j, $jzInfo[$i]['class']); $worksheet->setCellValueByColumnAndRow(4, $j, $jzInfo[$i]['sumscore']); }
$styleArrayBody = [ 'borders' => [ 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN, 'color' => ['argb' => '666666'], ], ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, ], ]; $total_jzInfo = $len + 2; $worksheet->getStyle('A1:C'.$total_jzInfo)->applyFromArray($styleArrayBody); $filename = '测试表.xlsx'; header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0'); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); }
|