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

时间:2021-06-30 07:50:53

标签: php excel phpexcel phpspreadsheet phpoffice-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, true, false);;
        
        
            
        foreach ($realDatas as $rowIndex => $row) {    
            foreach ($row as $colIndex => $col) {  
                $spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($colIndex+1, $rowIndex+1, $col);    
            }    
        } 
    }

    $worksheet = $spreadsheet->getActiveSheet();

0 个答案:

没有答案