PHPExcel绘图图表导致无法读取的内容和图表被删除

时间:2014-05-15 04:05:00

标签: php charts phpexcel

我使用PHPExcel生成图表所有内容都是正确的但有时当我打开文件时,它显示消息指示不可读的内容,当我点击是按钮进行修复被删除。贝娄是我的代码。

    $workbook = new PHPExcel();
    for($sh = 0; $sh <= 2; $sh++)
    {   
        if($sh == 0)
        {   
            $sheet = $workbook->createSheet($sh); 
            $sheet = $workbook->setActiveSheetIndex($sh);
            $workbook->getActiveSheet()->setTitle($this->lang->line('co_repBased_pos'));
            $sheet_name = $sheet->getTitle();

        }
        elseif($sh == 1)
        {   //$sheet = $workbook->createSheet($sh);
            $sheet = $workbook->setActiveSheetIndex($sh);
                  $workbook->getActiveSheet()->setTitle($this->lang->line('co_repBased_jloc'));  
            $sheet_name = $sheet->getTitle();  
        }
        elseif($sh == 2)
        {   
            $sheet = $workbook->createSheet($sh);
            $sheet = $workbook->setActiveSheetIndex($sh); 
            $workbook->getActiveSheet()->setTitle($this->lang->line('co_total_rep'));  
            $sheet_name = $sheet->getTitle();

        }

        $sheet->getTabColor()->setRGB('CC000#');
        // Set the page setting for easy print option 
        $sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
        $sheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $sheet->getPageSetup()->setFitToPage(TRUE);
        $sheet->getPageSetup()->setFitToWidth(1);
        $sheet->getPageSetup()->setFitToHeight(0);
        // Set the protection of work book and sheet 
        $workbook->getSecurity()->setLockWindows(true);
        $workbook->getSecurity()->setLockStructure(true);
        $workbook->getSecurity()->setWorkbookPassword("pAsSwOrD");
        // Set sheet password protected 
        $sheet->getProtection()->setSheet(TRUE);

        $sheet->getProtection()->setSort(true);   
        $sheet->getProtection()->setInsertRows(true);   
        $sheet->getProtection()->setInsertColumns(true);   
        $sheet->getProtection()->setFormatCells(true); 
        $sheet->getProtection()->setSelectUnlockedCells(false);   
        $sheet->getProtection()->setSelectLockedCells(true);   
        $sheet->getProtection()->setObjects(true);   
        $sheet->getProtection()->setPassword("pAsSwOrD");   
        $styleHeadings = array(
                                'borders' => array(
                                    'allborders' => array(
                                        'style' => PHPExcel_Style_Border::BORDER_THIN,
                                            'color' => array('argb' => '#DBDBB7'),
                                        ),
                                    ),
                                'font' => array(
                                    'bold' => true,
                                    'size' => 20 
                                    ),
                                'fill' => array(
                                    'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                                    'startcolor' => array(
                                        'argb' => 'E9E9E9',
                                    ),
                                    'endcolor' => array(
                                        'argb' => 'E9E9E9',
                                    ),
                                ),
                                'alignment' => array(
                                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                                ),   
                    );
        if($this->mng_auth->get_language()=='en')
        {
            $sheet->setRightToLeft(false); 
        }
        else
        {  
            $sheet->setRightToLeft(true); 
        }
        $start_headRow = $total_rows+1;
        $end_headRow = $total_rows+2;
        // Make the chart area setting 
        $j = 'B';       
        for($k = 0; $k < $course_count; $k++)
        {   
            $m=$k;
            $col = $j;           
            $j = chr(ord($j)+$m);    
        }  
        $total_std = 0;
        foreach($chart_total_stdOfCourse as $row)
        {
            if($row[1] > $total_std)
            {
                $total_std = $row[1];
            }
        }  

        $input_string  = $col;
        $base_value    = 64;
        $decimal_value = 26;
        $column_index  = 0;
        for ($d = 0; $d < strlen($input_string); $d++) 
        {
            // take the ASCII value.
            $char_value = ord($input_string[$d]);
            $char_value -= $base_value;
            $char_value *= pow($decimal_value, (strlen($input_string) - ($d + 1)));
            $column_index += $char_value;
        }
        //create column for present

        $present = PHPExcel_Cell::stringFromColumnIndex($column_index);
        //echo $course_count.'/'.$column_index.'/'.$end_headRow.'/'.$present; exit;
        if($column_index <= 9)
        {
            $present = 'J';
            $column_index = 9;
        }
        // End of chart area setting 
        $sheet->getStyle('A'.$start_headRow.':'.$present.$end_headRow)->applyFromArray($styleHeadings);
        if($sh == 0)
        {
            $sheet->setCellValue('A'.$start_headRow,'ÑÇæÑ æÑÓ åÇ Èå ÇÓÇÓ ÑÊÈå ÔÇÑÏ');
        }
        elseif($sh == 1)
        {
            $sheet->setCellValue('A'.$start_headRow,'ÑÇæÑ æÑÓ åÇ Èå ÇÓÇÓ ãÍá æÙíÝå ÔÇÑÏ');
        }
        elseif($sh == 2)
        {
            $sheet->setCellValue('A'.$start_headRow,'ÑÇæÑ æÑÓ åÇ Èå ÇÓÇÓ ãÌãæÚå ÔÇÑÏÇä');
        }
        $sheet->getStyle('A'.$start_headRow.':'.$present.$end_headRow)->getAlignment()->setWrapText(true);
        // Set mergeCells  start from 0 
        $sheet->mergeCellsByColumnAndRow(0,$start_headRow,$column_index,$end_headRow);
        if($sh == 0)
        {
            $sheet->fromArray(  
               $course_rep,
               NULL,
               'B'.$startRow_num 
            );
        }
        elseif($sh == 1)
        {
            $sheet->fromArray(  
               $course_jobLocBasedrep,
               NULL,
               'B'.$startRow_num  
            );

        }
        else
        {
            $sheet->fromArray(  
                   $chart_total_stdOfCourse,
                   NULL,
                   'B'.$startRow_num  
                );
        }
        if($sh <= 1)
        {
            $labels = array(
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$C$'.$startRow_num, null, 1),
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$D$'.$startRow_num, null, 1), 
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$E$'.$startRow_num, null, 1), 
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$F$'.$startRow_num, null, 1), 
            );

            // find the end row number of data source because we write from A2 so A3 is the start of main data 

            $categories = array(
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$B$'.$category_start.':$B$'.$total_rows, null, 4),   
            );
            $values = array(
              new PHPExcel_Chart_DataSeriesValues('Number', "'".$sheet_name."'".'!$C$'.$category_start.':$C$'.$total_rows, null, 4),
              new PHPExcel_Chart_DataSeriesValues('Number', "'".$sheet_name."'".'!$D$'.$category_start.':$D$'.$total_rows, null, 4),  
              new PHPExcel_Chart_DataSeriesValues('Number', "'".$sheet_name."'".'!$E$'.$category_start.':$E$'.$total_rows, null, 4),  
              new PHPExcel_Chart_DataSeriesValues('Number', "'".$sheet_name."'".'!$F$'.$category_start.':$F$'.$total_rows, null, 4),  
            );
            $series = new PHPExcel_Chart_DataSeries(
              PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,    // plotType
              PHPExcel_Chart_DataSeries::GROUPING_STACKED,    // plotGrouping
              array(0,1,2,3),                                 // plotOrder
              $labels,                                        // plotLabel
              $categories,                                    // plotCategory
              $values                                         // plotValues
            );
        }
        elseif($sh == 2)
        {    

            // Total student chart 
            $labels = array(
              //new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$Q$'.$startRow_num, null, 1),
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$C$'.$startRow_num, null, 1)
            );

            // find the end row number of data source because we write from A2 so A3 is the start of main data 

            $categories = array(
              new PHPExcel_Chart_DataSeriesValues('String', "'".$sheet_name."'".'!$B$'.$category_start.':$B$'.$total_rows, null, 1)   
            );
            $values = array(
              new PHPExcel_Chart_DataSeriesValues('Number', "'".$sheet_name."'".'!$C$'.$category_start.':$C$'.$total_rows, null, 1)
              //new PHPExcel_Chart_DataSeriesValues('Number', $sheet_name.'!$B$'.$category_start.':$B$'.$total_rows, null, 4),  
            ); 
            $series = new PHPExcel_Chart_DataSeries(
              PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,    // plotType
              PHPExcel_Chart_DataSeries::GROUPING_STACKED,    // plotGrouping
              array(0),                                       // plotOrder
              $labels,                                        // plotLabel
              $categories,                                    // plotCategory
              $values                                         // plotValues
            );  
        }                                                                          
        $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);        
        $layout1 = new PHPExcel_Chart_Layout();    // Create object of chart layout to set data label 
        $layout1->setShowVal(TRUE);                   
        $layout1->setManual3dAlign(true);
        $layout1->setXRotation(20);
        $layout1->setYRotation(20);
        $layout1->setPerspective(15);                                              
        $layout1->setRightAngleAxes(TRUE);                                              
        //$layout1->setShowPercent(TRUE);
        //$layout1->setShowCatName(TRUE);
        //$layout1->setShowSerName(TRUE);
        //$layout1->setShowBubbleSize(TRUE);
        //$layout1->setShowLeaderLines(TRUE);
        //$layout1->setShowLegendKey(TRUE);
        //$image = base_url().'image/app_req.jpeg';
        //$header1 = new PHPExcel_Worksheet();   
        //$header1->setHeaderFooter(PHPExcel_Worksheet_HeaderFooter::addImage($image,'A12'));
        $plotarea = new PHPExcel_Chart_PlotArea($layout1, array($series));
        $title_name = $this->lang->line('co_report').''.$this->lang->line('co_reportFrom').$rep_sdate.''.$this->lang->line('co_reportto').''.$rep_edate;
        //echo $title_name; exit;
        $title    = new PHPExcel_Chart_Title($title_name, null);  
        //$title = new PHPExcel_Chart_Title(new PHPExcel_RichText(new PHPExcel_Cell('A','1','Ttile Chart',PHPExcel_Cell_DataType::TYPE_STRING,$sheet)));
        $legend   = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
        $xTitle   = new PHPExcel_Chart_Title($this->lang->line('course_names'));
        $yTitle   = new PHPExcel_Chart_Title($this->lang->line('no_std'));
        $chart    = new PHPExcel_Chart(
          'chart1',                                       // name
          $title,                                         // title
          $legend,                                        // legend 
          $plotarea,                                      // plotArea
          true,                                           // plotVisibleOnly
          0,                                              // displayBlanksAs
          $xTitle,                                        // xAxisLabel
          $yTitle                                         // yAxisLabel
        );   
        $start_ofChart = $end_headRow+2;                
        $chart->setTopLeftPosition('A'.$start_ofChart);
        if($total_std < 20)
        {                           
            $total_std+= 20;
            $present++; 
            //echo $total_std; exit; 
            $chart->setBottomRightPosition((string)$present."$total_std");
        }                                         
        $sheet->addChart($chart); 
    }    
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="training report.xlsx"');
    header('Cache-Control: max-age=0'); 
    $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
    $writer->setIncludeCharts(TRUE);
    ob_start();
    ob_end_clean(); 
    $writer->save('php://output');

但是,当我打开文件时,它会显示消息指示不可读的内容,当我单击是按钮进行修复时,图表正在删除。所以,任何帮助解决这个问题谢谢。

0 个答案:

没有答案