在一个工作表中合并 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 (将#修改为@)