简体   繁体   English

'文件名062014.xlsx无法识别为OLE文件'

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

I am working on a complex program that deals with Excel, so I am using PHPExcel to search and edit the Excel file from a browser. 我正在处理一个处理Excel的复杂程序,因此我使用PHPExcel从浏览器中搜索和编辑Excel文件。 My problem comes in the editing portion of the program, so I wrote a basic program to edit the existing Excel page. 我的问题出现在程序的编辑部分,所以我编写了一个基本程序来编辑现有的Excel页面。 It seems that PHPExcel does not recognize the file created in Excel as an Excel file. 似乎PHPExcel无法将Excel中创建的文件识别为Excel文件。 This is done on my own server with an Excel page I created with Excel. 这是在我自己的服务器上使用Excel创建的Excel页面完成的。 The filename is 062014.xlsx. 文件名是062014.xlsx。 On the HTML side I named the text boxes C3, D3, and E3, so their names will easily correspond with Excel cells ( where the php $cell variable comes from). 在HTML方面,我将文本框命名为C3,D3和E3,因此它们的名称很容易与Excel单元格(php $ cell变量来自)相对应。 What I want to do is take the text in the html text boxes and rewrite corresponding cells in Excel with the data from the html textboxes. 我想要做的是获取html文本框中的文本,并使用html文本框中的数据重写Excel中的相应单元格。 Posted is my whole code from html and php, if someone can tell me where my program is going wrong, I would greatly appreciate it. 发布是我的整个代码来自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>

You are trying to load an xlsx file (OfficeOpenXML-format) using the Excel5 (BIFF-format) Reader. 您正在尝试使用Excel5 (BIFF格式)Reader加载xlsx文件(OfficeOpenXML格式)。

$inputFileType = 'Excel5';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

You should be using the correct Reader for the file type you're trying to load, otherwise you will get errors. 您应该为正在尝试加载的文件类型使用正确的Reader,否则您将收到错误。

You've already used 你已经习惯了

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

to identify the filetype and the correct Reader to use, so why are you ignoring this and setting it manually (and incorrectly) yourself? 要识别文件类型和正确使用的Reader,那么为什么要忽略这一点并自己手动(并且错误地)设置它?


Additionally 另外

Another problem is likely to be the fact that the file is already open when you try to save it. 另一个问题可能是当您尝试保存文件时文件已经打开。

You're loading $file ( 062014.xlsx ) using the PHPExcel loader, no problem. 你正在使用PHPExcel加载器加载$file062014.xlsx ),没问题。

For some unknown reason, you then execute 出于某种未知原因,您可以执行

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

though you don't do anything with $fileObj at all, but doing this leaves it open 虽然你根本没有对$fileObj做任何事情,但这样做会让它打开

When you try to save using $objWriter->save($file); 当您尝试使用$objWriter->save($file); , the file is still held open, so the save will fail (nothing to do with the filename, simply the fact that the file is open). ,文件仍保持打开状态,因此保存将失败(与文件名无关,只是文件打开的事实)。

The solution is as simple as deleting the line 解决方案就像删除该行一样简单

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM