PHP如何操作Excel之phpspreadsheet教程
今天给大家带来一个php插件 phpspreadsheet
下载 phpoffice/phpspreadsheet,地址:https://packagist.org/packages/phpoffice/phpspreadsheet
composer require phpoffice/phpspreadsheet
如何使用呢?请看下面DEMO
use PhpOfficePhpSpreadsheetSpreadsheet; use PhpOfficePhpSpreadsheetWriterXlsx; //实例化 $xlsx = new PhpOfficePhpSpreadsheetReaderXlsx(); //加载文件 $excel = $xlsx->load("1.xlsx"); //获取活动的excel sheet $sheet = $excel ->getActiveSheet(); //获取sheet列数 $cols = $sheet->getHighestColumn(); //获取行数 $rows = $sheet->getHighestRow(); $data = []; for ($r=2;$r<=$rows;$r++){ //行 $tmp = []; for ($c = 'A'; $c <= $cols; $c++){ //列值 $tmp[]= $active->getCell($c.$r)->getValue(); } $data[] = $tmp; } //写入excel $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); foreach ($data as $key=>$val) { $key++; $sheet->setCellValue("A".$key,$val) ->setCellValue("B".$key,$val]) ...; } $writer = new Xlsx($spreadsheet); $writer->save('2.xlsx');