'The filename 062014.xlsx is not recognised as an OLE file'

'The filename 062014.xlsx is not recognised as an OLE file'

我正在开发一个处理 Excel 的复杂程序,因此我使用 PHPExcel 从浏览器搜索和编辑 Excel 文件。我的问题出在程序的编辑部分,所以我编写了一个基本程序来编辑现有的 Excel 页面。 PHPExcel 似乎无法将在 Excel 中创建的文件识别为 Excel 文件。这是在我自己的服务器上完成的,我使用 Excel 创建的 Excel 页面。文件名为 062014.xlsx。在 HTML 一侧,我将文本框命名为 C3、D3 和 E3,因此它们的名称很容易与 Excel 单元格对应(php $cell 变量的来源)。我想要做的是获取 html 文本框中的文本,并使用 html 文本框中的数据重写 Excel 中的相应单元格。发布的是我在 html 和 php 中的全部代码,如果有人能告诉我我的程序哪里出错了,我将不胜感激。

<html>
<head>


<form method="POST" action="lilrevisetest.php" id="excelform">

<input type="text" value="foo" name="C3" />
    <input type="text" value="foo" name="D3" />
    <input type="text" value="foo" name="E3" />
<button type="submit">Submit</button>
</form>


</body>
</html>




<body>
 <html>


<?php

include 'PHPExcel/IOFactory.php';
 $n=1;
 $x="C";
 $y=1;


 $file = "062014.xlsx";


  $inputFileType = PHPExcel_IOFactory::identify($file);
 $inputFileType = 'Excel5';
 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
 $objReader->setReadDataOnly(false);

  $objPHPExcel = $objReader->load($file);

  $objWorksheet = $objPHPExcel->getActiveSheet();
  $fileObj = fopen("$file", "rt" );


   $y = 3; 
   $x= "C";

   for($n=1; $n<4; $n++){
    $cell = $x . $y; 

    echo $cell; 
    if (isset($_POST[$cell])){
    $string = ($_POST[$cell]);
       $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);

      $objWorksheet ->setCellValue("$cell","$string");
      $objWriter->save($file);
 }
 echo "<td> <input type ='text' value= '$string' name = '$cell'/></td>";
     $x= ++$x;
}

?>

      </html>
   </body>

您正在尝试使用 Excel5(BIFF 格式)Reader.

加载 xlsx 文件(OfficeOpenXML 格式)
$inputFileType = 'Excel5';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

您应该为要加载的文件类型使用正确的 Reader,否则您会收到错误消息。

您已经使用过

$inputFileType = PHPExcel_IOFactory::identify($file);

识别文件类型和正确的Reader使用,那么你为什么要忽略它并自己手动(错误地)设置它?


另外

另一个问题可能是当您尝试保存时文件已经打开。

您正在使用 PHPExcel 加载程序加载 $file (062014.xlsx),没问题。

出于某种未知原因,您随后执行

$fileObj = fopen("$file", "rt" );

尽管您根本不对 $fileObj 做任何事情,但这样做会使它保持打开状态

当您尝试使用 $objWriter->save($file); 保存时,文件仍保持打开状态,因此保存将失败(与文件名无关,只是文件打开的事实)。

解决方法很简单,删除行

$fileObj = fopen("$file", "rt" );