未捕获的错误:调用未定义的方法PHPExcel :: sheetNameExists()

时间:2016-11-10 18:17:17

标签: php excel

当我尝试生成一个excel文档时,我收到此错误,任何人都知道这个错误来自哪里?

  

致命错误:未捕获错误:调用未定义的方法   PHPExcel :: sheetNameExists()in   /public_html/docs/PHP/PHPExcel/Worksheet.php:835 Stack   追踪:#0   /public_html/docs/PHP/PHPExcel/Worksheet.php(337):   PHPExcel_Worksheet-> setTitle('Worksheet',false)#1   /public_html/docs/PHP/PHPExcel.php(108):   PHPExcel_Worksheet-> __ construct(Object(PHPExcel))#2   /public_html/docs/SendDocument.php(33):   PHPExcel-> __ construct()#3 {main}引入   835行/public_html/docs/PHP/PHPExcel/Worksheet.php

<?php
//Excel Data
    $Spo = $_SESSION["spo"];
    $Cont = $_SESSION["contractNum"];
    $Site = $_SESSION["siteMan"];
    $Job = $_SESSION["jtd"];
    $ObBrief = $_SESSION["ob_Des"];
    $ObAgreed = $_SESSION["ob_Act"];
    $ObDate = $_SESSION["date1"];
    $GoBrief = $_SESSION["good_Des"];
    $GoAgreed = $_SESSION["good_Act"];
    $GoDate = $_SESSION["date2"];
    $FeBrief = $_SESSION["fe_Des"];
    $FeAgreed = $_SESSION["fe_Act"];
    $FeDate = $_SESSION["date3"];
    $Q1 = '';
    $Q2 = '';
    $Q3 = '';
    $Q4 = '';
    $Q5 = '';
    $Q6 = '';
    $Q7 = '';
    $Q8 = '';
    $Q9 = '';
    $Q10 = '';
    $Add = $_SESSION["addCom"];
    $Name = $_SESSION["name"];
    $Title = $_SESSION["title"];
    $Date = $_SESSION["date4"]; 

    Include_once 'PHP/PHPExcel.php';

    $objPHPExcel = new PHPExcel();
    $activeSheet = $objPHPExcel->getActiveSheet();
    $objPHPExcel->getActiveSheet()->setTitle('Senior Managers Site Tour');
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);

    $styleArray = array(
        'borders' => array(
            'allborders' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
                )
            )
        );  

//Set Up
    $activeSheet->setCellValue('E1','Senior Managers Site Tour');
    $objPHPExcel->getActiveSheet()->getStyle('A1:N70')->applyFromArray($styleArray);
    $objPHPExcel->getActiveSheet()->mergeCells('A1:A70');
    $objPHPExcel->getActiveSheet()->mergeCells('N1:N70');
    $objPHPExcel->getActiveSheet()->getStyle('B6:M68')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);

    $titleArray = array(
        'font'  => array(
            'bold'  => true,
            'size'  => 32,
        ));

    $styleTitleAlign = array(
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

//Title
    $objPHPExcel->getActiveSheet()->mergeCells('B1:D4');
    $objPHPExcel->getActiveSheet()->mergeCells('E1:M4');
    $objPHPExcel->getActiveSheet()->mergeCells('B5:M5');

    $objPHPExcel->getActiveSheet()->getStyle('E1')->applyFromArray($titleArray);
    $objPHPExcel->getActiveSheet()->getStyle('E1')->applyFromArray($styleTitleAlign);

//Basic Information
    $objPHPExcel->getActiveSheet()->mergeCells('B6:D6');
    $objPHPExcel->getActiveSheet()->mergeCells('E6:G6');
    $objPHPExcel->getActiveSheet()->mergeCells('H6:J6');
    $objPHPExcel->getActiveSheet()->mergeCells('K6:M6');
    $objPHPExcel->getActiveSheet()->mergeCells('B7:D7');
    $objPHPExcel->getActiveSheet()->mergeCells('E7:M7');
    $objPHPExcel->getActiveSheet()->mergeCells('B8:D8');
    $objPHPExcel->getActiveSheet()->mergeCells('E8:M8');
    $objPHPExcel->getActiveSheet()->mergeCells('B9:M10');

    $activeSheet->setCellValue('B6','Site / Project / Object:');
    $activeSheet->setCellValue('E6', $Spo);
    $activeSheet->setCellValue('H6','Contract Number:');
    $activeSheet->setCellValue('K6',$Cont);
    $activeSheet->setCellValue('B7','Site Manager:');
    $activeSheet->setCellValue('E7',$Site);
    $activeSheet->setCellValue('B8','Job / Task Description:');
    $activeSheet->setCellValue('E8',$Job);

//Observation
    $objPHPExcel->getActiveSheet()->mergeCells('B11:M11');
    $objPHPExcel->getActiveSheet()->mergeCells('B12:E12');
    $objPHPExcel->getActiveSheet()->mergeCells('F12:I12');
    $objPHPExcel->getActiveSheet()->mergeCells('J12:M12');
    $objPHPExcel->getActiveSheet()->mergeCells('B13:E17');
    $objPHPExcel->getActiveSheet()->mergeCells('F13:I17');
    $objPHPExcel->getActiveSheet()->mergeCells('J13:M17');
    $objPHPExcel->getActiveSheet()->mergeCells('B18:M19');  

    $activeSheet->setCellValue('B11','OBSERVATION (areas where improvement can be made).');
    $activeSheet->setCellValue('B12','Brief Description:');
    $activeSheet->setCellValue('B13',$ObBrief);
    $activeSheet->setCellValue('F12','Agreed Action:');
    $activeSheet->setCellValue('F13',$ObAgreed);
    $activeSheet->setCellValue('J12','Close Date:');
    $activeSheet->setCellValue('J13',$ObDate);

    $objPHPExcel->getActiveSheet()->getStyle('B11')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

//Good Practices
    $objPHPExcel->getActiveSheet()->mergeCells('B20:M20');
    $objPHPExcel->getActiveSheet()->mergeCells('B21:E21');
    $objPHPExcel->getActiveSheet()->mergeCells('F21:I21');
    $objPHPExcel->getActiveSheet()->mergeCells('J21:M21');
    $objPHPExcel->getActiveSheet()->mergeCells('B22:E26');
    $objPHPExcel->getActiveSheet()->mergeCells('F22:I26');
    $objPHPExcel->getActiveSheet()->mergeCells('J22:M26');
    $objPHPExcel->getActiveSheet()->mergeCells('B27:M28');

    $activeSheet->setCellValue('B20','GOOD PRACTICES (Commendable acts and actions, Improvements, Innovations etc).');
    $activeSheet->setCellValue('B21','Brief Description:');
    $activeSheet->setCellValue('B22',$GoBrief);
    $activeSheet->setCellValue('F21','Agreed Action:');
    $activeSheet->setCellValue('F22',$GoAgreed);
    $activeSheet->setCellValue('J21','Close Date:');
    $activeSheet->setCellValue('J22',$GoDate);

    $objPHPExcel->getActiveSheet()->getStyle('B20')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

//Feedback Given & Received
    $objPHPExcel->getActiveSheet()->mergeCells('B29:M29');
    $objPHPExcel->getActiveSheet()->mergeCells('B30:E30');
    $objPHPExcel->getActiveSheet()->mergeCells('F30:I30');
    $objPHPExcel->getActiveSheet()->mergeCells('J30:M30');
    $objPHPExcel->getActiveSheet()->mergeCells('B31:E35');
    $objPHPExcel->getActiveSheet()->mergeCells('F31:I35');
    $objPHPExcel->getActiveSheet()->mergeCells('J31:M35');
    $objPHPExcel->getActiveSheet()->mergeCells('B36:M37');

    $activeSheet->setCellValue('B29','FEEDBACK GIVEN & RECEIVED');
    $activeSheet->setCellValue('B30','Brief Description:');
    $activeSheet->setCellValue('B31',$FeBrief);
    $activeSheet->setCellValue('F30','Agreed Action:');
    $activeSheet->setCellValue('F31',$FeAgreed);
    $activeSheet->setCellValue('J30','Close Date:');
    $activeSheet->setCellValue('J31',$FeDate);

    $objPHPExcel->getActiveSheet()->getStyle('B29')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

//General Impression
    $objPHPExcel->getActiveSheet()->mergeCells('B38:M38');
    $objPHPExcel->getActiveSheet()->mergeCells('B39:G39');
    $objPHPExcel->getActiveSheet()->mergeCells('H39:M39');
    $objPHPExcel->getActiveSheet()->mergeCells('B40:G40');
    $objPHPExcel->getActiveSheet()->mergeCells('H40:M40');
    $objPHPExcel->getActiveSheet()->mergeCells('B41:M41');
    $objPHPExcel->getActiveSheet()->mergeCells('B42:K42');
    $objPHPExcel->getActiveSheet()->mergeCells('L42:M42');
    $objPHPExcel->getActiveSheet()->mergeCells('B43:K43');
    $objPHPExcel->getActiveSheet()->mergeCells('L43:M43');
    $objPHPExcel->getActiveSheet()->mergeCells('B44:K44');
    $objPHPExcel->getActiveSheet()->mergeCells('L44:M44');
    $objPHPExcel->getActiveSheet()->mergeCells('B45:K45');
    $objPHPExcel->getActiveSheet()->mergeCells('L45:M45');
    $objPHPExcel->getActiveSheet()->mergeCells('B46:K46');
    $objPHPExcel->getActiveSheet()->mergeCells('L46:M46');
    $objPHPExcel->getActiveSheet()->mergeCells('B47:M47');

    $objPHPExcel->getActiveSheet()->mergeCells('B48:K48');
    $objPHPExcel->getActiveSheet()->mergeCells('L48:M48');
    $objPHPExcel->getActiveSheet()->mergeCells('B49:K49');
    $objPHPExcel->getActiveSheet()->mergeCells('L49:M49');
    $objPHPExcel->getActiveSheet()->mergeCells('B50:K50');
    $objPHPExcel->getActiveSheet()->mergeCells('L50:M50');
    $objPHPExcel->getActiveSheet()->mergeCells('B51:K51');
    $objPHPExcel->getActiveSheet()->mergeCells('L51:M51');
    $objPHPExcel->getActiveSheet()->mergeCells('B52:K52');
    $objPHPExcel->getActiveSheet()->mergeCells('L52:M52');
    $objPHPExcel->getActiveSheet()->mergeCells('B53:M54');

    $activeSheet->setCellValue('B38','GENERAL IMPRESSION');
    $activeSheet->setCellValue('B39','1 = No evidence / poor / no understanding');
    $activeSheet->setCellValue('H39','2 = Some evidence / understanding / not consistent');
    $activeSheet->setCellValue('B40','3 = Requirements in place / basic unerstanding');
    $activeSheet->setCellValue('H40','4 = Detailed understanding / more than minimum standards');

    $objPHPExcel->getActiveSheet()->getStyle('B38')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

    $activeSheet->setCellValue('B41','CULTURE');
    $activeSheet->setCellValue('B42','Is the CDP obvious - You Said / We Did Boards; Feedback Stations; Posters?:');
    $activeSheet->setCellValue('L42',$Q1);
    $activeSheet->setCellValue('B43','Are there any Speak Up Coaches on this site?');
    $activeSheet->setCellValue('L43',$Q2);
    $activeSheet->setCellValue('B44','Are Focus Leader meetings happening?:');
    $activeSheet->setCellValue('L44',$Q3);
    $activeSheet->setCellValue('B45','Mention the Mental Tools, do people understand them?:');
    $activeSheet->setCellValue('L45',$Q4);
    $activeSheet->setCellValue('B46','Ask what "Never Harm" means to the teams:');
    $activeSheet->setCellValue('L46',$Q5);

    $objPHPExcel->getActiveSheet()->getStyle('B41')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

    $activeSheet->setCellValue('B47','SYSTEM');
    $activeSheet->setCellValue('B48','Are first impressions good, site signage, induction etc?:');
    $activeSheet->setCellValue('L48',$Q6);
    $activeSheet->setCellValue('B49','Is the site tidy, well laid out etc?:');
    $activeSheet->setCellValue('L49',$Q7);
    $activeSheet->setCellValue('B50','Are morning briefings / daily risk assessment carried out?:');
    $activeSheet->setCellValue('L50',$Q8);
    $activeSheet->setCellValue('B51','Have all the team been briefed & signed onto the RAMS?:');
    $activeSheet->setCellValue('L51',$Q9);
    $activeSheet->setCellValue('B52','Ask what they would do if there was a change in the work activity i.e. not in RAMS?:');
    $activeSheet->setCellValue('L52',$Q10);

    $objPHPExcel->getActiveSheet()->getStyle('B47')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

//Comments
    $objPHPExcel->getActiveSheet()->mergeCells('B55:M55');
    $objPHPExcel->getActiveSheet()->mergeCells('B56:M63');
    $objPHPExcel->getActiveSheet()->mergeCells('B64:M65');

    $activeSheet->setCellValue('B55','Additional Comments:');
    $activeSheet->setCellValue('B56',$Add);

    $objPHPExcel->getActiveSheet()->getStyle('B55')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

//Completed By
    $objPHPExcel->getActiveSheet()->mergeCells('B66:M66');
    $objPHPExcel->getActiveSheet()->mergeCells('B67:C67');
    $objPHPExcel->getActiveSheet()->mergeCells('D67:G67');
    $objPHPExcel->getActiveSheet()->mergeCells('H67:I67');
    $objPHPExcel->getActiveSheet()->mergeCells('J67:M67');
    $objPHPExcel->getActiveSheet()->mergeCells('B68:C68');
    $objPHPExcel->getActiveSheet()->mergeCells('D68:G68');
    $objPHPExcel->getActiveSheet()->mergeCells('H68:M68');
    $objPHPExcel->getActiveSheet()->mergeCells('B69:M70');

    $activeSheet->setCellValue('B66','COMPLETED BY');
    $activeSheet->setCellValue('B67','Name:');
    $activeSheet->setCellValue('D67',$Name);
    $activeSheet->setCellValue('H67','Title:');
    $activeSheet->setCellValue('J67',$Title);
    $activeSheet->setCellValue('B68','Date:');
    $activeSheet->setCellValue('D68',$Date);

    $objPHPExcel->getActiveSheet()->getStyle('B66')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('D6D6D6');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
    $objWriter->save('Senior Managers Site Tour.xls');
?>

1 个答案:

答案 0 :(得分:0)

似乎使用了错误的PHPExcel版本,您需要重新安装库

有关详细信息,请参阅[此答案]。

研究

通过PHPExcel repository on GitHub进行代码跟进后,可以进行以下观察:

实例化PHPExcel时,它的构造函数使用一个Worksheet构建工作表数组。

// Initialise worksheet collection and add one worksheet
$this->workSheetCollection = array();
$this->workSheetCollection[] = new PHPExcel_Worksheet($this);

请注意,Worksheet在实例化时会将PHPExcel实例添加到其构造函数中。此对象已分配给Worksheet属性parent

public function __construct(PHPExcel $pParent = null, $pTitle = 'Worksheet')
{
    // Set parent and title
    $this->parent = $pParent;
    ...

现在,我们发现从setTitle

调用了Worksheet方法
$objPHPExcel->getActiveSheet()->setTitle('Senior Managers Site Tour');

setTitle内,有以下代码:

if ($this->parent) {
    // Is there already such sheet name?
    if ($this->parent->sheetNameExists($pValue)) {
        ...

此处抛出异常。

我们可以清楚地看到PHPExcel代码中存在的方法,我们可以看到父类被正确识别为此类的成员。所以唯一的结论就是使用错误版本的PHPExcel.php 。仅在v。1.8

中添加了缺失的功能