#1 sandi
编程新手,现在做的一个自己使用的网站程序想使用excel的导入导出功能,需要整合PhpSpreadsheet到speedphp里面,但是不会。程序使用说明中有两句
use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xlsx;放到网站的控制器代码中就会报错。希望高手能指点下,在speedphp中
如何使用PhpSpreadsheet。万分感谢。
2021-03-22 21:42:46
#2 jake
库直接放lib里面,按目录和包名匹配
然后用 new PhpOffice\PhpSpreadsheet\Spreadsheet 这样调用。不需要用use
另外use要php7,你的环境是不是php7?
2021-03-23 13:45:49
#3 sandi
感谢老大的解答,我把phpoffice的vendor整个目录都放到了lib目录。然后在BaseController中做了文件引用“require(APP_DIR.'/protected/lib/vendor/autoload.php');” 在控制器页面中这个使用。 //导出学生
function actionExport() {
new PhpOffice\PhpSpreadsheet\Spreadsheet;
new PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$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->setCellValueByColumnAndRow(5, 2, '分组');
//合并单元格
$worksheet->mergeCells('A1:E1');
$styleArray = [
'font' => [
'bold' => true
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
//设置单元格样式
$worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
$worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);
//$worksheet->getStyle('A2:D2')->getFont()->setSize(14);
$student = new Student();
//读取数据
$sql = "SELECT student_name,student_sex,classes_id,seat_id,student_group FROM `student`";
$stmt = $student->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$len = count($rows);
$j = 0;
for ($i=0; $i < $len; $i++) {
$j = $i + 3;
$worksheet->setCellValueByColumnAndRow(1, $j, $rows[$i]['student_name']);
$worksheet->setCellValueByColumnAndRow(2, $j, $rows[$i]['student_sex']);
$worksheet->setCellValueByColumnAndRow(3, $j, $rows[$i]['classes_id']);
$worksheet->setCellValueByColumnAndRow(4, $j, $rows[$i]['seat_id']);
$worksheet->setCellValueByColumnAndRow(5, $j, $rows[$i]['student_group']);
}
$styleArrayBody = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => '666666'],
],
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$total_rows = $len + 2;
//添加所有边框/居中
$worksheet->getStyle('A1:E'.$total_rows)->applyFromArray($styleArrayBody);
// 下载
$filename = 'student.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}
提示出错:
ERROR: Argument 1 passed to PhpOffice\PhpSpreadsheet\Writer\Xlsx::__construct() must be an instance of PhpOffice\PhpSpreadsheet\Spreadsheet, none given, called in F:\iclass_Alpha\protected\controller\teacher\StudentController.php on line 396 and defined in F:\iclass_Alpha\protected\lib\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx.php on line 112不会解决了,不知何故。
2021-03-23 20:17:30
#4 jake
意要把问题说清楚,最好包括:代码、错误提示、截图等。
2021-03-23 22:00:59
#6 jake
以上的错误提示,意思是在一个StudentController里面,使用PhpSpreadsheet的时候有一个构造函数的参数,你没有给它传进去。
这样的提示,不是引入包的问题,包已经引入了。只是对PhpSpreadsheet的用法不清楚的问题。
2021-03-24 09:35:31