ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ public function room_end_export() { $list = [1,2,3,4,5]; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); /** 全局设置 s**/ //设置货币符号$ // $sheet->getStyle('I9')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_GBP_SIMPLE); $sheet->getStyle('I9')->getNumberFormat()->setFormatCode( '"£"#,##0.00_-'); //设置宽度 $sheet->getDefaultColumnDimension()->setWidth('25'); $sheet->getColumnDimension('A')->setWidth('5'); $sheet->getColumnDimension('B')->setWidth('20'); $sheet->getColumnDimension('C')->setWidth('15'); $sheet->getColumnDimension('D')->setWidth('20'); $sheet->getColumnDimension('E')->setWidth('15'); $sheet->getColumnDimension('F')->setWidth('10'); $sheet->getColumnDimension('G')->setWidth('13'); $sheet->getColumnDimension('H')->setWidth('15'); $sheet->getColumnDimension('I')->setWidth('15'); $sheet->getColumnDimension('J')->setWidth('15'); //单元格水平和垂直都居中 $sheet->getStyle('B')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_RIGHT); $sheet->getStyle('H')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_RIGHT); $sheet->getStyle('I')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_RIGHT); $sheet->getStyle('B') ->getFont() ->setName('Times New Roman') ->setBold(true) ->setSize(12); $sheet->getStyle('H') ->getFont() ->setName('Times New Roman') ->setBold(true) ->setSize(12); $sheet->getStyle('B:L') ->getFont() ->setName('Times New Roman') ->setBold(false) ->setSize(12); $sheet->getStyle('C6:E17') ->getFont() ->setName('Times New Roman') ->setBold(false) ->setSize(12); $sheet->getStyle('I') ->getFont() ->setName('Times New Roman') ->setBold(false) ->setSize(12); $sheet->getStyle('B19:J19') ->getFont() ->setName('Times New Roman') ->setBold(true) ->setSize(12); /** 全局设置 end**/ // 合并单元格 合并后,赋值只能给A1,开始的坐标。 $sheet->mergeCells('A4:H4'); $sheet->getCell('A4')->setValue(' Service Inovice'); //单元格水平和垂直都居中 $sheet->getStyle('A4')->getAlignment()->setWrapText(true) // 自动换行 ->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_LEFT); $sheet->getRowDimension(4)->setRowHeight(50); //将单元格字体设置粗体,字体大小20 $sheet->getStyle('A4') ->getFont() ->setName('Chalkboard') ->setBold(false) ->setSize(36); $sheet->setCellValue('B6', 'Teacher Name'); $sheet->setCellValue('B7', 'Contact number'); $sheet->setCellValue('B8', 'Contact Email'); $sheet->setCellValue('B9', 'Contact Address'); $sheet->getRowDimension(9)->setRowHeight(35); $sheet->setCellValue('B11', 'Bill To'); $sheet->setCellValue('B12', 'Address'); $sheet->setCellValue('B16', 'Contact Number'); $sheet->setCellValue('B17', 'Email'); //值、real_name $sheet->setCellValue('C6', 'real_name'); $sheet->setCellValue('C7', ' '.'13800138000'); $sheet->mergeCells('C8:D8'); $sheet->setCellValue('C8', 'email'); $sheet->mergeCells('C9:D9'); $sheet->setCellValue('C9', 'address'); $sheet->getStyle('C9')->getAlignment()->setWrapText(true); $sheet->mergeCells('C11:E11'); $sheet->setCellValue('C11', 'Redford Education International Ltd'); $sheet->mergeCells('C12:E15'); $sheet->setCellValue('C12', 'Suite 2, Mission Hall, 53a Woking Road '. PHP_EOL .'Guildford'. PHP_EOL .'England'. PHP_EOL .'GU1 1QD'); $sheet->getStyle('C12')->getAlignment()->setWrapText(true); //单元格水平和垂直都居中 $sheet->getStyle('C11:H17')->getAlignment()->setWrapText(true) // 自动换行 ->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_RIGHT); $sheet->mergeCells('C16:E16'); $sheet->setCellValue('C16', ' +447466567888'); $sheet->mergeCells('C17:E17'); $sheet->setCellValue('C17', 'admin@redfordeducation.com'); $sheet->setCellValue('H6', 'Inovice #'); $sheet->setCellValue('H7', 'Inovice date'); $sheet->setCellValue('H8', 'Due date'); $sheet->setCellValue('H9', 'Amount Due'); //值、 $inovice_number = ' '.date('dmY',time()); $sheet->setCellValue('I6', $inovice_number); $sheet->setCellValue('I7', date('d/m/Y')); $sheet->setCellValue('I8', date('d/m/Y',strtotime("+1 day"))); //课程 $sheet->getStyle('B19:J19')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->setCellValue('B19', 'No.'); $sheet->setCellValue('C19', 'Date'); $sheet->setCellValue('D19', 'Description'); $sheet->setCellValue('F19', 'Unit'); $sheet->setCellValue('G19', 'Session'); $sheet->setCellValue('H19', 'Rate'); $sheet->setCellValue('I19', 'Amount'); $sheet->setCellValue('J19', 'Note'); //值 foreach ($list as $k => $v) { $num = 20 + $k; $sheet->getStyle('D'.$num)->getAlignment()->setWrapText(true); // 自动换行 $sheet->getStyle('H'.$num.':I'.$num)->getNumberFormat()->setFormatCode( '"£"#,##0.00_-'); $sheet->getStyle('B'.$num.':J'.$num)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('H'.$num)->getFont()->setBold(false); $sheet->getStyle('B'.$num)->getFont()->setBold(false); $sheet->mergeCells('D'.$num.':E'.$num); $sheet->setCellValue('B'.$num, $k+1); $sheet->setCellValue('C'.$num, date('Y/m/d',time())); $sheet->setCellValue('D'.$num, '测试'); $sheet->setCellValue('F'.$num, '1'); $sheet->setCellValue('G'.$num, '1'); $sheet->setCellValue('H'.$num, 0); $sheet->setCellValue('I'.$num, '=F'.$num.' * H'.$num.''); $sheet->setCellValue('J'.$num, ''); } $n = count($list)+24; $sheet->setCellValue('H'.$n, 'Total'); $sheet->setCellValue('H'.($n+1), 'VAT'); $sheet->setCellValue('H'.($n+2), 'Sub Total'); $sheet->getStyle('I'.$n)->getNumberFormat()->setFormatCode( '"£"#,##0.00_-'); $sheet->getStyle('I'.($n+1))->getNumberFormat()->setFormatCode( '"£"#,##0.00_-'); $sheet->getStyle('I'.($n+2))->getNumberFormat()->setFormatCode( '"£"#,##0.00_-'); $sheet->setCellValue('I'.$n, '=SUM(I20:I'.(20 + count($list)-1).')'); $sheet->setCellValue('I'.($n+1), '0'); $sheet->setCellValue('I'.($n+2), '=SUM(I20:I'.(20 + count($list)-1).')'); $sheet->setCellValue('I9', '=SUM(I20:I'.(20 + count($list)-1).')'); $n = $n +5; $sheet->setCellValue('B'.$n, 'Payment Method'); $sheet->mergeCells('C'.$n.':D'.$n); $sheet->mergeCells('F'.$n.':G'.$n); $sheet->mergeCells('I'.$n.':J'.$n); $sheet->setCellValue('C'.$n, 'Bnk Transfer'); $sheet->setCellValue('E'.$n, 'Sort code'); $sheet->setCellValue('F'.$n, 'sort_code'); $sheet->setCellValue('H'.$n, 'Account no.'); $sheet->setCellValue('I'.$n, 'account_no'); $sheet->getStyle('C'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('E'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('F'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('I'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $n = $n + 2; $sheet->mergeCells('C'.$n.':E'.$n); $sheet->setCellValue('C'.$n, 'I achknolege that the information above is accurate and true'); $sheet->getStyle('C'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_CENTER); $n = $n + 2; $sheet->mergeCells('B'.$n.':C'.$n); $sheet->setCellValue('B'.$n, 'Teacher Signature'); $sheet->getStyle('B'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_LEFT); $n = $n + 1; $sheet->mergeCells('B'.$n.':E'.$n); $sheet->setCellValue('B'.$n, 'Note: This inovice is in GBP'); $sheet->getStyle('B'.$n)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal(Alignment::HORIZONTAL_LEFT); $writer = new Xlsx($spreadsheet); $writer->save('php://output'); $format = 'Xlsx'; // $format只能为 Xlsx 或 Xls if ($format == 'Xlsx') { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); } elseif ($format == 'Xls') { header('Content-Type: application/vnd.ms-excel'); } $nickname = 'nickname'; $fileName = $inovice_number.$nickname; //直接输出文件 header("Content-Disposition: attachment;filename=" . $fileName . '.' . strtolower($format)); header('Cache-Control: max-age=0'); $objWriter = IOFactory::createWriter($spreadsheet, $format); // ob_start(); $objWriter->save('php://output'); // ob_end_clean(); exit(); } ~~~