duanshaiduhao2471 2012-10-31 03:25
浏览 91
已采纳

PHPExcel从定义的工作表生成数据到数组

I want to generate an array from my xlsx file first, before I insert it to Mysql, so I am using phpexcel because this one is good enough (I know it from every website's review). But there is a problem when I am trying to making an array from the xlsx defined sheet, here is my script :

<?php

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('Europe/London');

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Automatic Importer</title>

</head>
<body>

<h1>PHPExcel Reader Example #07</h1>
<h2>Simple File Reader Loading a Single Named WorkSheet</h2>
<?php

/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';


//$inputFileType = 'Excel5';
    $inputFileType = 'Excel2007';
//  $inputFileType = 'Excel2003XML';
//  $inputFileType = 'OOCalc';
//  $inputFileType = 'Gnumeric';
$inputFileName = 'file.xlsx';
$sheetname = 'My Sheet 4';

echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);


echo '<hr />';

echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames();
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);


?>
<body>
</html>

and this is the error says :

Fatal error: Call to a member function cellExists() on a non-object in C:\xampp\htdocs\chi\import\Classes\PHPExcel\Calculation.php on line 3241

Can someone tell where is the error is? My OOP is not good, thanks

  • 写回答

2条回答 默认 最新

  • du1913 2012-10-31 07:31
    关注

    The problem is something in one of the spreadsheet formulae that PHPExcel is having problems evaluating, when you call toArray() with the 3rd argument as TRUE telling the method to evaluate all the formulae in the worksheet. I did add a fix to this last night where a named range containing a reference to a non-existent cell is used in a formula... a bug that would trigger this message; but in this case (with the error at that particular line number) it looks as though it could be a formula that references a non-existent worksheet. The fact that you're only loading a single named worksheet makes this quite possible.

    If possible, can you "walk the spreadsheet", using getCalculatedValue() on each cell to find which cell contains the formula that is triggering the error; and then see what that formula is?

    Alternatively, load the entire workbook rather than just the one individual worksheet, and see if the error still occurs then.

    I'll look at patching the calculation engine code to prevent this error; but it does mean that the formula can't be correctly evaluated if a sheet that it references isn't accessible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里