PHPExcel:如何检查XLS文件是否有效? [英] PHPExcel: How to check whether a XLS file is valid or not?

查看:421
本文介绍了PHPExcel:如何检查XLS文件是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPExcel 1.7.8读取由随机用户上传的.xls文件.所有人都可以使用有效的.xls文件正常工作,但是现在我想对无效文件进行一些测试,以检查程序是否显示正确的错误消息.

I'm using PHPExcel 1.7.8 to read .xls files, uploaded by a radom user. All is working properly with a valid .xls file, but now I wanted to make some tests with invalid files to check if the program displays good error messages.

所以我拿了一个.csv文件,并用.xls重命名了它(不进行任何转换,只是更改了名称),最后只是为了检查.... 破碎的! :)

So I took a .csv file, and renamed it with .xls (without converting anything, just changing the name) to the end, just to check...
Broken! :)

DOM ELEMENT: HTML
DOM ELEMENT: BODY
DOM ELEMENT: P
START OF PARAGRAPH: 
END OF PARAGRAPH:
FLUSH CELL: A1 => block,date,hour...

array
  1 =>
    array
      'A' => string  'block,date,hour...' (length=2777)

{"step":"error","errors":[],"warnings":[]}

就像您看到的那样,显示了一条错误消息,我没有要求,然后是我通常编写的JSON.

Like you can see, there's an error message displaying, I didn't ask for that, and then the JSON that I usually write.

它发生在这一行:

<?php
echo "Loading file\n";
try {
    if (!($objPHPExcel = PHPExcel_IOFactory::load('path'))) {
        echo "Failed\n";
        return;
        // ...
    }
} catch(Exception $e) {
    echo 'Exception !';
}
echo "Done\n";

此代码显示:

Loading file
/!\ ERROR MESSAGE ABOVE /!\
Done

我的问题是,在尝试解析文件之前,PHPExcel或其他方法可以检查文件是否为有效的XLS文件吗?

My question is, is there a way with PHPExcel or anything else to check whether a file is a valid XLS file before I try to parse it?

谢谢.

推荐答案

PHPExcel中的每个阅读器都有一个canRead()方法,该方法可以验证传递给读取的文件的格式是否适合该阅读器-该方法返回一个简单的布尔值是或否.从对PHPExcel_Reader_Excel5类的canRead()方法的调用返回True,将确认该读取器可以读取该文件,而不管文件扩展名如何.

Each reader in PHPExcel has a canRead() method that validates the file passed in to the read is of the appropriate format for that reader - the method returns a simple boolean True or False. A return of True from a call to the canRead() method of the PHPExcel_Reader_Excel5 class will confirm that the file can be read by that reader, irrespective of the file extension.

IOFactory identity()方法使用此调用,依次针对每种受支持的格式对Readers进行测试,直到从canRead()调用获得真正的返回值为止. IOFactory的load()方法又使用identify()来确定应将哪个Reader用于指定的文件.

The IOFactory identify() method uses this call, testing against the Readers for each supported format in turn until it gets a true return from the canRead() call. The IOFactory load() method, in it's turn, uses identify() to determine which Reader should be used for the specified file.

当您想为阅读器设置其他参数时,验证文件类型的能力(不取决于文件扩展名,这常常会引起误解)特别有用.

The ability to verify a filetype (without depending on the file extension which can often be misleading) is particularly useful when you want to set additional arguments for the reader.

identify()/load()的后备效果不太令人满意:如果canRead()对于所有其他Reader返回false,则该文件将被视为CSV.

The fallback from identify()/load() is slightly less satisfactory: if canRead() returns false for all other Readers, then the file is treated as a CSV.

这篇关于PHPExcel:如何检查XLS文件是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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