微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在一个工作表中合并 phpSpreadsheet 块而不会丢失单元格格式

如何解决在一个工作表中合并 phpSpreadsheet 块而不会丢失单元格格式

我有一个现有的 Excel 模板,我想使用 PHPSpreadsheet 对其进行编辑。我的模板有点大,所以我以前把它当作块来读,但我不知道如何将所有块合并到一个电子表格中,或者至少将所有块内容写在一个工作表中

$inputFileType = 'Xlsx';
$inputFileName = 'template.xlsx';

$reader = PHPOffice\PHPSpreadsheet\IOFactory::createReader($inputFileType);
$chunkSize = 100;
$chunkFilter = new ChunkReadFilter();
for ($startRow = 1; $startRow <= 1000; $startRow += $chunkSize) {

     //Tell the Read Filter which rows we want this iteration  
    $chunkFilter->setRows($startRow,$chunkSize);
    //Tell the Reader that we want to use the Read Filter 
    $reader->setReadFilter($chunkFilter);
    //Load only the rows that match our filter  
    $spreadsheet = $reader->load($inputFileName);
    //HERE I WANT TO APPEND THE WORKSHEET TO THE EXISTING ONE OF THE PREV. CHUNK (OR combine Chunks)
    $worksheet = $spreadsheet->getActiveSheet();
}

// ONCE I'M ABLE TO GET ALL TEMPLATE SHEET I WILL DO PROCESSING HERE

$writer = PHPOffice\PHPSpreadsheet\IOFactory::createWriter($spreadsheet,'Xlsx');
$writer->save('report'. date("Md_Y") .'.xlsx');

我能够组合值,但我丢失了单元格格式。有谁知道如何保持单元格格式?

    $inputFileType = 'Xlsx';
    $inputFileName = 'template.xlsx';
    $spreadsheet = new PHPOffice\PHPSpreadsheet\Spreadsheet();
    
    $reader = PHPOffice\PHPSpreadsheet\IOFactory::createReader($inputFileType);
    $chunkSize = 100;
    $chunkFilter = new ChunkReadFilter();
    for ($startRow = 1; $startRow <= 100; $startRow += $chunkSize) {

         //Tell the Read Filter which rows we want this iteration  
        $chunkFilter->setRows($startRow,$chunkSize);
        //Tell the Reader that we want to use the Read Filter 
        $reader->setReadFilter($chunkFilter);
        //Load only the rows that match our filter  
        $sp = $reader->load($inputFileName);
        //Do some processing here
        $realDatas = $sp->getActiveSheet()->toArray(null,true,false);;
        
        
            
        foreach ($realDatas as $rowIndex => $row) {    
            foreach ($row as $colIndex => $col) {  
                $spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($colIndex+1,$rowIndex+1,$col);    
            }    
        } 
    }

    $worksheet = $spreadsheet->getActiveSheet();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。