致命错误:'打破'不在'循环'或者'切换' Function.php中的上下文

时间:2016-03-10 14:02:07

标签: php phpexcel

我正在尝试使用PHPExcel,并且在执行脚本时输出中出现错误:

  

致命错误:'打破'不在'循环'或者'切换'第581行/opt/lampp/htdocs/Xlsphp/test/Classes/PHPExcel/Calculation/Functions.php中的上下文

我不知道我在PHP脚本中做错了什么。似乎一切都是正确的。

有人知道如何解决它吗?

这是我的PHP脚本:

<?php
require_once 'Classes/PHPExcel.php';
require_once 'config.php';

$sql = 'SELECT * FROM tablevalues';
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$fileName = 'test.xls';

// initialise excel column name
// currently limited to queries with less than 27 columns
$columnArray = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

// Instantiate a new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 1;
// fetch result set column information
$finfo = mysqli_fetch_fields($result);
// initialise columnlenght counter
$columnlenght = 0;
foreach ($finfo as $val) {
    // set column header values
    $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$columnlenght++] . $rowCount, $val->name);
}
// make the column headers bold
$objPHPExcel->getActiveSheet()->getStyle($columnArray[0]."1:".$columnArray[$columnlenght]."1")->getFont()->setBold(true);

$rowCount++;
// Iterate through each result from the SQL query in turn
// We fetch each database result row into $row in turn

while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    for ($i = 0; $i < $columnlenght; $i++) {
        $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$i] . $rowCount, $row[$i]);
    }
    $rowCount++;
}
// set header information to force download
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
// Write the Excel file to filename some_excel_file.xlsx in the current directory
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
// Write the Excel file to filename some_excel_file.xlsx in the current directory
$objWriter->save('php://output');

mysqli_close($conn);
?>

2 个答案:

答案 0 :(得分:19)

只需删除&#34;休息;&#34; functions.php文件中的语句。 因为break是在return语句之后,所以它会给出致命的错误。

答案 1 :(得分:1)

解决第三方开源库不兼容问题的正确方法是:

  1. 检查是否已发布您遇到问题的库的更新版本
  2. 向库的维护者提交一个错误,如果你修复了一个包含你的修补程序或git pull请求的diff文件
  3. 在您的情况下,只需从github下载PHPExcel,解包并覆盖旧库。