安装扩展
composer require phpoffice/phpspreadsheet
引入扩展
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
导入到php,xls 和xlsx都可以
下面是简单示例,有很多可以优化的地方和格式导入
$inputFileName='D:\\1111.xlsx';
dump($inputFileName);
$spreadsheet =IOFactory::load($inputFileName);
$sheetData = $spreadsheet->getSheet(0)->toArray();
dump($sheetData);
导出 到服务器
$objPHPExcel = new Spreadsheet();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', '标题')
->setCellValue('C1', '描述')
->setCellValue('D1', '图片');
$list=ArticleModel::limit(5)->select()->toArray();
$i=2;
foreach($list as $k=>$v){
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.($i+$k), $v['id'])
->setCellValue('B'.($i+$k), $v['title'])
->setCellValue('C'.($i+$k), $v['desc'])
->setCellValue('D'.($i+$k), $v['pic']);
}
$writer = IOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('/4545.xlsx');