PHPExcel读取excel文件并保存成html文件

public function index()        //该方法只用于php5
    {
        $els = new PHPExcel();
        $filename = FCPATH . 'xls/aaa.xls';
        $obj = new \PHPExcel_Reader_Excel5();
        $nn = $obj->load($filename);
        $htmlobj = new \PHPExcel_Writer_HTML($nn);

        $htmlobj->save(FCPATH . 'xls/bbb.html');
    }

php7已经不支持phpexcel,用PhpSpreadsheet替换

一、通过composer安装PhpSpreadsheet

composer require phpoffice/phpspreadsheet
public function index()
    {
        // 读取excel文件
        $old = FCPATH . 'xls/aaa.xlsx';
        $spreadsheet = IOFactory::load($old);
        $worksheet = $spreadsheet->getActiveSheet();

        // 修改
        $worksheet->getCell('A1')->setValue('Hello World');
        $worksheet->getCell('B1')->setValue('Tome and Jerry');

        // 修改完成后保存成excel
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $new = FCPATH . 'xls/new_aaa.xlsx';
        $writer->save($new);

      
     // 修改完成后保存成html
        $writer = IOFactory::createWriter($spreadsheet, 'Html');  //Html注意大小写
        $new = FCPATH . 'xls/new_aaa.html';
        $writer->save($new);

    }

 

posted @ 2024-01-12 20:32  哆啦啊梦  阅读(28)  评论(0编辑  收藏  举报