copy style and data in PHPExcel

后端 未结 3 1654
长情又很酷
长情又很酷 2021-02-03 14:48

I would like to copy all the data and the style of a certain range to other cells. for example I want to copy from A4: I15 and then paste exactly want I copied the contents and

3条回答
  •  -上瘾入骨i
    2021-02-03 15:22

    I found a solution without additional code:

    setReadDataOnly(false);
    $spreadsheet = $reader->load('old_file.xlsx');
    
    $worksheet = $spreadsheet->getActiveSheet();
    
    /** Iterate needed cells in $worksheet and change their values **/
    
    $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('new_file.xlsx');
    

    The important part is to set option $reader->setReadDataOnly(false) (which is set by default) because if option is set to true then only data without styles are read.

提交回复
热议问题