Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.97 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to verify if a file is a valid excel spreadsheet?
  2. _app = new Application()
  3.         {
  4.             Visible = false,
  5.             DisplayAlerts = false,     // <-- I don't want to remove this.
  6.             AskToUpdateLinks = false,
  7.         };
  8.        
  9. _workbooks.Open(filename);
  10.    _workbook = _workbooks.get_Item(1); // 1-based.
  11.    _worksheets = _workbook.Sheets;
  12.        
  13. XlFileFormat.xlTextMac
  14.     XlFileFormat.xlTextMSDOS
  15.     XlFileFormat.xlTextPrinter
  16.     XlFileFormat.xlTextWindows
  17.        
  18. XlFileFormat.xlExcel12
  19.     XlFileFormat.xlExcel7
  20.     XlFileFormat.xlExcel8
  21.     XlFileFormat.xlExcel9795
  22.        
  23. bool validFile = ( f == XlFileFormat.xlExcel12   )
  24.                   || ( f == XlFileFormat.xlExcel7    )
  25.                   || ( f == XlFileFormat.xlExcel8    )
  26.                   || ( f == XlFileFormat.xlExcel9795 );
  27.        
  28. int countBefore = _workbooks.get_Count();
  29. _workbooks.Open(filename);
  30. int countAfter = _workbooks.get_Count();
  31. if (countBefore == countAfter) {
  32.     // The file failed to load.
  33. }