检测.csv文件是否实际上是.xls(Excel)文件

时间:2012-08-27 17:08:29

标签: csv binary-data xls

我想检测用户是否意外上传了标记为.csv的Excel文件。 xls文件是否有标准的二进制占用空间可以实现这一目标?

1 个答案:

答案 0 :(得分:2)

您可以在python中读取excel文件:

http://scienceoss.com/read-excel-files-from-python/

您可以在Perl中阅读excel文件:

http://www.thegeekstuff.com/2011/12/perl-and-excel/

How can I read Excel files in Perl?

Unix / Linux实用程序file可以识别excel和大量其他文件。

示例输出:

file ~/Download/*xls

/home/paul/Downloads/REDACTED1.xls:          Composite Document File V2 Document, Little Endian, Os: Windows, Version 5.1, Code page: 1252, Author: Someones Name, Last Saved By: Somebody Else, Name of Creating Application: Microsoft Excel, Create Time/Date: Wed Jan 27 00:39:46 2010, Last Saved Time/Date: Sun Feb 28 13:55:47 2010, Security: 0

/home/paul/Downloads/REDACTED2.xls: Composite Document File V2 Document, Little Endian, Os: Windows, Version 1.0, Code page: -535, Author: Paul , Last Saved By: Paul , Revision Number: 3, Total Editing Time: 18:09, Create Time/Date: Wed Oct 26 23:45:51 2011, Last Saved Time/Date: Thu Oct 27 00:34:42 2011

您可以简单地构建一个调用file的库并返回结果。

要了解file如何做,源代码可用,file实用程序有自己的配置文件,甚至还有魔术字节和字符串信息的配置目录。

apt-get source file

./ file-5.11 / magic / MagDir是一个充满魔术字节和字符串的目录,以多种格式查找,但在我自己的excel文件扫描中看到的“复合文档文件”未在此处声明。这个目录确实有Mac上的Excel,Word和一些旧的msdos格式的定义文件。

cd ./file-5.11; grep 'Composite Document File' */*

的产率:

src/cdf.c: * Parse Composite Document Files, the format used in Microsoft Office
src/cdf.c: * N.B. This is the "Composite Document File" format, and not the
src/cdf.h: * Parse Composite Document Files, the format used in Microsoft Office
src/cdf.h: * N.B. This is the "Composite Document File" format, and not the
src/readcdf.c:                if (file_printf(ms, "Composite Document File V2 Document")
src/readcdf.c:          if (file_printf(ms, "Composite Document File V2 Document")

我建议您进行调查,以确定file实用程序如何检测某些Microsoft Excel格式。