PHPExcel逻辑序列错误

时间:2014-06-18 23:39:00

标签: php codeigniter foreach phpexcel

我使用的是PHPExcel 1.8.0版本,以及CodeIgniter,我正在尝试的是克隆并填充3个不同的表格:

 public function generateReport($obra, $todosEnsayes, $pNombre) {



    $this->load->library('PHPExcel/PHPExcel');


    $objPHPexcel = new PHPExcel();


    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objPHPexcel = $objReader->load('application/plantillas_excel/reporte_grafico2.xlsx');

    $objPHPexcel->getProperties()
                      ->setCreator('New system'                )
                      ->setLastModifiedBy('NewSystem'         )
                      ->setTitle('NewSystem'  )
                      ->setSubject('NewSystem')
                      ->setDescription('NewSystem')
                      ->setKeywords("Excel Office 2007 openxml php"    )
                      ->setCategory("NewSystem"       );  



    $hoja = 0;
    $inicial = 18;
    $arrayNumero = array();
    $arrayValor = array();
    $arrayNombre = array();
    $arrayValor2 = array();
    $arrayDesv = array();

    $tramo = 'TRAMO: KM '.$obra->km_inicial.'+'.$obra->mt_inicial.' - '.$obra->km_final  .'+'.$obra->mt_final;

    foreach($todosEnsayes as $key=>$ensaye) { 

          if (($key % 20) == 0)
          {
            $newSheet = clone $objPHPexcel->getSheetByName("PLANTILLA1");
            $newSheet->setTitle('GRAF SYC-LL');
            $objPHPexcel->setActiveSheetIndex($hoja);
            $objWorksheet = $objPHPexcel->getActiveSheet();
            $objWorksheet
            ->setCellValue('A6', "Obra:".$obra->objeto)
            ->setCellValue('A7', "Tramo:".$tramo);
            $objWorksheet
            ->setCellValue('A'.$inicial, $key)
            ->setCellValue('B'.$inicial, '35')
            ->setCellValue('C'.$inicial, 'SYC 2A CPA PRUEBA')
            ->setCellValue('D'.$inicial, '35')
            ->setCellValue('E'.$inicial, '1.72'); 
            array_push($arrayNumero, $key);
            array_push($arrayValor, '35');
            array_push($arrayNombre, 'SYC 2A CPA PRUEBA');
            array_push($arrayValor2, '35');
            array_push($arrayDesv, '1.72');



            $this->generarImagen($arrayNumero,$arrayValor,$arrayNombre,$arrayValor2,$arrayDesv);

            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setWorksheet($objWorksheet);
            $objDrawing->setName("name");
            $objDrawing->setDescription("Description");
            $objDrawing->setPath('imagefile.png');
            $objDrawing->setCoordinates('I8');
            $objDrawing->setOffsetX(1);
            $objDrawing->setOffsetY(5);


            $arrayNumero = array();
            $arrayValor = array();
            $arrayNombre = array();
            $arrayValor2 = array();
            $arrayDesv = array();
            $objPHPexcel->addSheet($newSheet, $hoja);

            $inicial = 18;
          }
          else
          {
            $newSheet = clone $objPHPexcel->getSheetByName("PLANTILLA1");
            //$newSheet->setTitle('GRAF SYC-LL');
            //$objPHPexcel->addSheet($newSheet, $hoja);
            $objPHPexcel->setActiveSheetIndex($hoja);
            $objWorksheet = $objPHPexcel->getActiveSheet();
            $objWorksheet
            ->setCellValue('A6', "Obra:".$obra->objeto)
            ->setCellValue('A7', "Tramo:".$tramo);
            $objWorksheet
            ->setCellValue('A'.$inicial, $key)
            ->setCellValue('B'.$inicial, '35')
            ->setCellValue('C'.$inicial, 'SYC 2A CPA PRUEBA')
            ->setCellValue('D'.$inicial, '35')
            ->setCellValue('E'.$inicial, '1.72'); 

            array_push($arrayNumero, $key);
            array_push($arrayValor, '35');
            array_push($arrayNombre, 'SYC 2A CPA PRUEBA');
            array_push($arrayValor2, '35');
            array_push($arrayDesv, '1.72');

            $inicial++;
          }

    }

    $this->generarImagen($arrayNumero,$arrayValor,$arrayNombre,$arrayValor2,$arrayDesv);

            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setWorksheet($objWorksheet);
            $objDrawing->setName("name");
            $objDrawing->setDescription("Description");
            $objDrawing->setPath('imagefile.png');
            $objDrawing->setCoordinates('I8');
            $objDrawing->setOffsetX(1);
            $objDrawing->setOffsetY(5);


            $arrayNumero = array();
            $arrayValor = array();
            $arrayNombre = array();
            $arrayValor2 = array();
            $arrayDesv = array();

    $hoja++;

    foreach($todosEnsayes as $key=>$ensaye) { //SE RECORREN LOS ENSAYES

          if (($key % 20) == 0)
          {
            $newSheet = clone $objPHPexcel->getSheetByName("PLANTILLA2");
            $newSheet->setTitle('GRAF SYC-CBR');
            $objPHPexcel->setActiveSheetIndex($hoja);
            $objWorksheet = $objPHPexcel->getActiveSheet();
            $objWorksheet
            ->setCellValue('A6', "Obra:".$obra->objeto)
            ->setCellValue('A7', "Tramo:".$tramo);
            $objWorksheet
            ->setCellValue('A'.$inicial, $key)
            ->setCellValue('B'.$inicial, '40')
            ->setCellValue('C'.$inicial, 'SYC 2A CPA PRUEBA')
            ->setCellValue('D'.$inicial, '40')
            ->setCellValue('E'.$inicial, '1.50');

            array_push($arrayNumero, $key);
            array_push($arrayValor, '35');
            array_push($arrayNombre, 'SYC 2A CPA PRUEBA');
            array_push($arrayValor2, '35');
            array_push($arrayDesv, '1.72');



            $this->generarImagen($arrayNumero,$arrayValor,$arrayNombre,$arrayValor2,$arrayDesv);

            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setWorksheet($objWorksheet);
            $objDrawing->setName("name");
            $objDrawing->setDescription("Description");
            $objDrawing->setPath('imagefile.png');
            $objDrawing->setCoordinates('I8');
            $objDrawing->setOffsetX(1);
            $objDrawing->setOffsetY(5);


            $arrayNumero = array();
            $arrayValor = array();
            $arrayNombre = array();
            $arrayValor2 = array();
            $arrayDesv = array(); 

            $objPHPexcel->addSheet($newSheet, $hoja);

            $inicial = 18;
          }
          else
          {
            $newSheet = clone $objPHPexcel->getSheetByName("PLANTILLA2");
            //$newSheet->setTitle('GRAF SYC-LL');
            //$objPHPexcel->addSheet($newSheet, $hoja);
            $objPHPexcel->setActiveSheetIndex($hoja);
            $objWorksheet = $objPHPexcel->getActiveSheet();
            $objWorksheet
            ->setCellValue('A6', "Obra:".$obra->objeto)
            ->setCellValue('A7', "Tramo:".$tramo);
            $objWorksheet
            ->setCellValue('A'.$inicial, $key)
            ->setCellValue('B'.$inicial, '40')
            ->setCellValue('C'.$inicial, 'SYC 2A CPA PRUEBA')
            ->setCellValue('D'.$inicial, '40')
            ->setCellValue('E'.$inicial, '1.50'); 

            array_push($arrayNumero, $key);
            array_push($arrayValor, '35');
            array_push($arrayNombre, 'SYC 2A CPA PRUEBA');
            array_push($arrayValor2, '35');
            array_push($arrayDesv, '1.72');

            $inicial++;
          }

    }

    $this->generarImagen($arrayNumero,$arrayValor,$arrayNombre,$arrayValor2,$arrayDesv);

            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setWorksheet($objWorksheet);
            $objDrawing->setName("name");
            $objDrawing->setDescription("Description");
            $objDrawing->setPath('imagefile.png');
            $objDrawing->setCoordinates('I8');
            $objDrawing->setOffsetX(1);
            $objDrawing->setOffsetY(5);


            $arrayNumero = array();
            $arrayValor = array();
            $arrayNombre = array();
            $arrayValor2 = array();
            $arrayDesv = array();

    $hoja++;

    foreach($todosEnsayes as $key=>$ensaye) { //SE RECORREN LOS ENSAYES

          if (($key % 20) == 0)
          {
            $newSheet = clone $objPHPexcel->getSheetByName("PLANTILLA3");
            $newSheet->setTitle('GRAF SYC-EXP');
            $objPHPexcel->setActiveSheetIndex($hoja);
            $objWorksheet = $objPHPexcel->getActiveSheet();
            $objWorksheet
            ->setCellValue('A6', "Obra:".$obra->objeto)
            ->setCellValue('A7', "Tramo:".$tramo);
            $objWorksheet
            ->setCellValue('A'.$inicial, $key)
            ->setCellValue('B'.$inicial, '35')
            ->setCellValue('C'.$inicial, 'SYC 2A CPA PRUEBA')
            ->setCellValue('D'.$inicial, '35')
            ->setCellValue('E'.$inicial, '1.72'); 

            $objPHPexcel->addSheet($newSheet, $hoja);

            array_push($arrayNumero, $key);
            array_push($arrayValor, '35');
            array_push($arrayNombre, 'SYC 2A CPA PRUEBA');
            array_push($arrayValor2, '35');
            array_push($arrayDesv, '1.72');



            $this->generarImagen($arrayNumero,$arrayValor,$arrayNombre,$arrayValor2,$arrayDesv);

            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setWorksheet($objWorksheet);
            $objDrawing->setName("name");
            $objDrawing->setDescription("Description");
            $objDrawing->setPath('imagefile.png');
            $objDrawing->setCoordinates('I8');
            $objDrawing->setOffsetX(1);
            $objDrawing->setOffsetY(5);


            $arrayNumero = array();
            $arrayValor = array();
            $arrayNombre = array();
            $arrayValor2 = array();
            $arrayDesv = array();

            $inicial = 18;
          }
          else
          {
            $newSheet = clone $objPHPexcel->getSheetByName("PLANTILLA3");
            //$newSheet->setTitle('GRAF SYC-LL');
            //$objPHPexcel->addSheet($newSheet, $hoja);
            $objPHPexcel->setActiveSheetIndex($hoja);
            $objWorksheet = $objPHPexcel->getActiveSheet();
            $objWorksheet
            ->setCellValue('A6', "Obra:".$obra->objeto)
            ->setCellValue('A7', "Tramo:".$tramo);
            $objWorksheet
            ->setCellValue('A'.$inicial, $key)
            ->setCellValue('B'.$inicial, '35')
            ->setCellValue('C'.$inicial, 'SYC 2A CPA PRUEBA')
            ->setCellValue('D'.$inicial, '35')
            ->setCellValue('E'.$inicial, '1.72'); 

            array_push($arrayNumero, $key);
            array_push($arrayValor, '35');
            array_push($arrayNombre, 'SYC 2A CPA PRUEBA');
            array_push($arrayValor2, '35');
            array_push($arrayDesv, '1.72');

            $inicial++;
          }

    }

    $this->generarImagen($arrayNumero,$arrayValor,$arrayNombre,$arrayValor2,$arrayDesv);

            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setWorksheet($objWorksheet);
            $objDrawing->setName("name");
            $objDrawing->setDescription("Description");
            $objDrawing->setPath('imagefile.png');
            $objDrawing->setCoordinates('I8');
            $objDrawing->setOffsetX(1);
            $objDrawing->setOffsetY(5);


            $arrayNumero = array();
            $arrayValor = array();
            $arrayNombre = array();
            $arrayValor2 = array();
            $arrayDesv = array();

    //$objPHPexcel->removeSheetByIndex(0); /* SE ELIMINA LA PLANTILLA */
    //$objPHPexcel->setActiveSheetIndex(0);


    $objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Reporte.xls"'); 
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');



 }

我在excel文件中的3种工作表上得到了结果。这段代码的问题在于它会弹出一个" 0"在文档的某些部分,特别是在单元格11中,除了第三个foreach之外,其他foreach迭代的值都具有所有正确的值。

enter image description here

enter image description here

每个foreach填充一种类型的表格,所以我发现第一个foreach在尚未达到该代码时混合第二个foreach的值时很奇怪。

编辑:我已经重新创建了序列,我似乎在第一次进入第一个" foreach"它也进入第一个"如果"子句并打印一个值,即使($ key)中的第一个值为0:

if (($key % 20) == 0)
 {
 }

2 个答案:

答案 0 :(得分:0)

为每个克隆的$ newsheet使用一个新的变量名,可能是克隆通过引用从前一个克隆到$ newssheet留下了一些残留信息。

答案 1 :(得分:0)

看起来我正在寻找的条件不是

if (($key % 20) == 0)
 {
 }

我用这种方式用计数器替换它,因为它总是在值为0时输入。

counter = 0;
    if ($counter == 20)
     {
     }
    //Do stuff
    //Clear counter
    $counter = 0;