addExternalSheet()错误"工作表不存在"同时使用克隆PHP Excel

时间:2018-02-19 10:14:11

标签: php clone phpexcel

在PHP Excel中使用addExternalSheet()时,addSheet()使用clone时遇到此问题。

这是我的代码:

<?php
$objPHPExcel = PHPExcel_IOFactory::load(APPPATH . 'blank_payroll_copy.xlsx');
$objPHPExcel2 = PHPExcel_IOFactory::load(APPPATH . 'blank_payroll_copy.xlsx');

// Now we get the provinces
$base = 8;
$base = 8;

foreach($provinces as $key) {
    $get_provinces_record = $this->Dry_m->multiple_join_provinces($key);
    $cprovinces = count($get_provinces_record);

    // this line is very important if you want to clone an external worksheet
    $clonedSheet = clone $objPHPExcel2->getActiveSheet();

    // $this->printr($get_provinces_record);
    foreach($get_provinces_record as $r => $datarew) {
        $get_monthly_salary = $this->Dry_m->get_specific_column('salary_grade', 'step_' . $datarew['step'], 'salary_grade', $datarew['salary_grade']);
        $monthly_salary = $get_monthly_salary['step_' . $datarew['step']];
        $row = $base + $r;

        // Clone worksheet
        $clonedSheet->setCellValue('A' . $row, $r + 1)->setCellValue('B' . $row, $datarew['LAST_M'] . ', ' . $datarew['FIRST_M'] . ' ' . $datarew['MIDDLE_M'])->setCellValue('C' . $row, $datarew['POSITION_M'])->setCellValue('D' . $row, $monthly_salary)->setCellValue('E' . $row, $datarew['bir_tax']);

        // this will make the rows mych nicer in  styling
        $objPHPExcel2->getActiveSheet()->getRowDimension('A8')->setRowHeight(-1);
        $objPHPExcel2->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true);

        // $sheetcloned[] = $r[$datarew];
        // $objPHPExcel->setActiveSheetIndex(0);

        /*
            $alphabets = array('D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
            foreach ($alphabets as $keyc => $alps) {
            $objPHPExcel2->getActiveSheet()->setCellValue($alps.$cprovinces,'=SUM('.$alps.$base.':'.$alps.$objPHPExcel->setActiveSheetIndex(0)->getHighestRow().')');
            }
        */
        $objPHPExcel2->getActiveSheet()->setCellValue('D35', '=SUM(D8:D44)');

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        // $objPHPExcel->setActiveSheetIndex(0);

    }

    // $this->printr($sheetcloned);
    // Rename cloned worksheet

    $clonedSheet->setTitle($key);
    $objPHPExcel->addExternalSheet($clonedSheet);
}
?>

正如你在底部看到的那样:

$objPHPExcel->addExternalSheet($clonedSheet);

它返回错误:

  

工作表不存在

但是当我用addSheet替换它时,它可以正常工作。

谁能告诉我我做错了什么?

我已经包含了要加载的文件供您查看。

1 个答案:

答案 0 :(得分:0)

首先添加外部工作表,然后更改标题。

volatile