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

走远了吗. 提交于 2021-02-18 21:32:27

问题


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. My problem comes in the editing portion of the program, so I wrote a basic program to edit the existing Excel page. It seems that PHPExcel does not recognize the file created in Excel as an Excel file. This is done on my own server with an Excel page I created with Excel. The filename is 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). 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. 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>
<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>

回答1:


You are trying to load an xlsx file (OfficeOpenXML-format) using the Excel5 (BIFF-format) Reader.

$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.

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?


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.

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

When you try to save using $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" );


来源:https://stackoverflow.com/questions/28688652/the-filename-062014-xlsx-is-not-recognised-as-an-ole-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!