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 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件