'文件名062014.xlsx无法识别为OLE文件' [英] 'The filename 062014.xlsx is not recognised as an OLE file'

查看:65
本文介绍了'文件名062014.xlsx无法识别为OLE文件'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

推荐答案

您正尝试使用 Excel5 (BIFF-格式)阅读器.

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.

您已经使用过

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

另外

另一个问题很可能是当您尝试保存文件时文件已经打开的事实.

Another problem is likely to be the fact that the file is already open when you try to save it.

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

You're loading $file (062014.xlsx) using the PHPExcel loader, no problem.

出于某些未知原因,然后执行

For some unknown reason, you then execute

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

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

though you don't do anything with $fileObj at all, but doing this leaves it open

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

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" );

这篇关于'文件名062014.xlsx无法识别为OLE文件'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆