0

I have a C# application that exports data to Excel. I have Excel 2010 and VS 2012 on my machine and am using .Net 4.5.

I quietly export in the background with Excel hidden from the user and then save it and launch it using Process.Start, but when I do this, Excel tells me that it can't open the file because the file format or file extension is not valid.

Here is my code:

xlApp = new Excel.Application();
Excel.Workbook myWBk;
myWBk = xlApp.Workbooks.Add(1);

// Some code to dump data to Excel

string sFilename = Environment.CurrentDirectory + @"\LHGR.xlsx";
myWBk.SaveAs(sFilename, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
myWBk.Close();
Process.Start(sFilename);

I have tried using the ".xls" extension, but then it saves it as an Excel 97-2003 format (which I can open) but it warns me that it is limiting the columns to 256 - I have more than that.

Any thoughts?

John
  • 195
  • 1
  • 3
  • 17
  • 1
    Don't use the Excel interop for stuff like this, use the [Open XML SDK for Office](https://msdn.microsoft.com/en-us/library/office/bb448854.aspx) instead. – Scott Chamberlain Nov 08 '16 at 13:57
  • As you can see in the duplicate, you're using the wrong `XlFileFormat`. – Charles Mager Nov 08 '16 at 13:58
  • `WB.SaveAs(fileName, (int)Excel.XlFileFormat.xlOpenXMLWorkbook, AccessMode: Excel.XlSaveAsAccessMode.xlNoChange);` works for me. – Axel Kemper Nov 08 '16 at 14:00
  • In addition to what @ScottChamberlain advises, you may also want to look at the EPPlus Open Source project that takes a lot of the problems away for dealing with Excel. – Steve Nov 08 '16 at 14:00
  • Thank you guys. @Axel - your parameters worked perfectly. – John Nov 08 '16 at 14:15

0 Answers0