5

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.

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

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

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

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.

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:

Excel Loads Invalid File

Community
  • 1
  • 1
Machado
  • 741
  • 2
  • 6
  • 19
  • well one way around check for valid file ext .csv or xls seems to be the simplest approach... – MethodMan Jan 17 '12 at 21:20
  • You might see if the _app.Ready property goes to false when loading an invalid file. Just a thought I haven't tried it. – Tod Jan 17 '12 at 21:24
  • @DJ KRAZE, I already check the extension before loading the file. But what if someone changes the file extension and try to load an .exe instead ? – Machado Jan 18 '12 at 11:48
  • @Tod, just checked: _app.Ready remains `true` before and after loading the invalid file. – Machado Jan 18 '12 at 11:48

2 Answers2

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:

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

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

    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.

Machado
  • 741
  • 2
  • 6
  • 19
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.
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Nice! But count sums 1, because TCHAN RAM! Excel loads the invalid file. But it does not look like a spreadsheet, because Excel loads the content of the file oddly. Look the edit, please. – Machado Jan 18 '12 at 11:22