码迷,mamicode.com
首页 > Web开发 > 详细

PHP简单操作Excel

时间:2019-06-30 12:38:56      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:org   pread   off   cti   实例   each   nbsp   class   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

标签:org   pread   off   cti   实例   each   nbsp   class   excel   

原文地址:https://www.cnblogs.com/itwlp/p/11109280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!