PHP简单操作Excel
Posted itwlp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP简单操作Excel相关的知识,希望对你有一定的参考价值。
1.下载 phpoffice/phpspreadsheet,地址:https://packagist.org/packages/phpoffice/phpspreadsheet
composer require phpoffice/phpspreadsheet
2.使用:
2.1读取文件:
use PhpOffice\\PhpSpreadsheet\\Spreadsheet; use PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx; //实例化 $xlsx = new \\PhpOffice\\PhpSpreadsheet\\Reader\\Xlsx(); //加载文件 $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‘);
以上是关于PHP简单操作Excel的主要内容,如果未能解决你的问题,请参考以下文章