如何验证文件是否是有效的Excel电子表格?

时间:2022-09-01 18:37:21

I already did some search here on SO, on this, this, this and more, but no clue, so I'm going to have to ask you guys.

我已经在这里做了一些搜索,关于这个,这个,这个以及更多,但没有任何线索,所以我将不得不问你们。

I'm trying to open an Excel file via Interop, and I have Office 2007 installed, so I'm using Microsoft.Office.Interop.Excel version 14.0.

我正在尝试通过Interop打开Excel文件,并且我安装了Office 2007,所以我使用的是Microsoft.Office.Interop.Excel版本14.0。

When I try to open a valid xls file, everything runs fine.

当我尝试打开一个有效的xls文件时,一切运行正常。

But if I try to open an invalid file (like a bitmap, or an executable) via Interop, Excel opens it without complain or warning.

但是,如果我尝试通过Interop打开无效文件(如位图或可执行文件),Excel会在没有抱怨或警告的情况下打开它。

How can I detect Excel has an invalid workbook file loaded, without blocking with an alert ?

如何检测Excel是否加载了无效的工作簿文件,而没有阻止警报?

I'm trying to write a unit test of my Excel object reader. One of the cases is to try to open an invalid file.

我正在尝试编写Excel对象阅读器的单元测试。其中一种情况是尝试打开无效文件。

Some code to help: Application Creation:

一些代码可以帮助:应用程序创建:

    _app = new Application()
        {
            Visible = false,
            DisplayAlerts = false,     // <-- I don't want to remove this.
            AskToUpdateLinks = false,
        };

Workbook opening:

工作簿开放:

   _workbooks.Open(filename);
   _workbook = _workbooks.get_Item(1); // 1-based.
   _worksheets = _workbook.Sheets;

EDIT: Just to add 1 more information: Excel loads the invalid file. If DisplayAlerts is set to true, it complains (open a dialog) informing the workbook seems to be corrupted, but if DisplayAlerts is set to false, it loads the file just as below:

编辑:只是添加1个更多信息:Excel加载无效文件。如果DisplayAlerts设置为true,它会抱怨(打开一个对话框)通知工作簿似乎已损坏,但如果DisplayAlerts设置为false,则加载文件如下:

如何验证文件是否是有效的Excel电子表格?

2 个解决方案

#1


8  

I just ended using another approach: Excel workbooks have a property called FileFormat. When Excel opens an invalid file, it sets the file format to one of the enumerator values of text:

我刚刚结束使用另一种方法:Excel工作簿有一个名为FileFormat的属性。当Excel打开无效文件时,它会将文件格式设置为文本的枚举器值之一:

    XlFileFormat.xlTextMac
    XlFileFormat.xlTextMSDOS
    XlFileFormat.xlTextPrinter
    XlFileFormat.xlTextWindows

And inside this enumeration Excel has the valid files (valid for my purpose):

在这个枚举中,Excel有有效的文件(对我有用):

    XlFileFormat.xlExcel12
    XlFileFormat.xlExcel7
    XlFileFormat.xlExcel8
    XlFileFormat.xlExcel9795

So I ended using a simple "or" to filter the kind of file I'd like to import:

所以我最后使用一个简单的“或”来过滤我要导入的文件类型:

    bool validFile = ( f == XlFileFormat.xlExcel12   ) 
                  || ( f == XlFileFormat.xlExcel7    ) 
                  || ( f == XlFileFormat.xlExcel8    ) 
                  || ( f == XlFileFormat.xlExcel9795 );

And now it works. Thanks for your help guys, you set me in the right direction.

现在它有效。谢谢你的帮助,你让我朝着正确的方向前进。

#2


2  

You can check the workbook count before and after loading the file. If both counts are the same, the file failed to load.

您可以在加载文件之前和之后检查工作簿计数。如果两个计数相同,则无法加载文件。

int countBefore = _workbooks.get_Count();
_workbooks.Open(filename);
int countAfter = _workbooks.get_Count();
if (countBefore == countAfter) {
    // The file failed to load.
}

#1


8  

I just ended using another approach: Excel workbooks have a property called FileFormat. When Excel opens an invalid file, it sets the file format to one of the enumerator values of text:

我刚刚结束使用另一种方法:Excel工作簿有一个名为FileFormat的属性。当Excel打开无效文件时,它会将文件格式设置为文本的枚举器值之一:

    XlFileFormat.xlTextMac
    XlFileFormat.xlTextMSDOS
    XlFileFormat.xlTextPrinter
    XlFileFormat.xlTextWindows

And inside this enumeration Excel has the valid files (valid for my purpose):

在这个枚举中,Excel有有效的文件(对我有用):

    XlFileFormat.xlExcel12
    XlFileFormat.xlExcel7
    XlFileFormat.xlExcel8
    XlFileFormat.xlExcel9795

So I ended using a simple "or" to filter the kind of file I'd like to import:

所以我最后使用一个简单的“或”来过滤我要导入的文件类型:

    bool validFile = ( f == XlFileFormat.xlExcel12   ) 
                  || ( f == XlFileFormat.xlExcel7    ) 
                  || ( f == XlFileFormat.xlExcel8    ) 
                  || ( f == XlFileFormat.xlExcel9795 );

And now it works. Thanks for your help guys, you set me in the right direction.

现在它有效。谢谢你的帮助,你让我朝着正确的方向前进。

#2


2  

You can check the workbook count before and after loading the file. If both counts are the same, the file failed to load.

您可以在加载文件之前和之后检查工作簿计数。如果两个计数相同,则无法加载文件。

int countBefore = _workbooks.get_Count();
_workbooks.Open(filename);
int countAfter = _workbooks.get_Count();
if (countBefore == countAfter) {
    // The file failed to load.
}