Dan Bater
Dan Bater

Reputation: 323

C# Create Excel (Done) then show/open it? Help

I can create an excel and save it, however I would prefer to show the document! Any help would be great!! Thanks

Excel.Application   xlApp;
Excel.Workbook      xlWorkBook;
Excel.Worksheet     xlWorkSheet;

object misValue = System.Reflection.Missing.Value;

xlApp       = new Excel.ApplicationClass();
xlWorkBook  = xlApp.Workbooks.Add(misValue);

xlWorkSheet         = (Excel.Worksheet)xlWorkBook.Sheets["Sheet1"];
xlWorkSheet.Name    = "Exported Data";

xlWorkSheet.Cells[1, 1] = "Demo";
xlWorkSheet             = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkBook.SaveAs("Filename.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

xlWorkSheet = null;
xlWorkBook  = null;
xlApp       = null;
GC.Collect();

Upvotes: 3

Views: 15120

Answers (2)

denisenkom
denisenkom

Reputation: 414

Insert this line xlApp.Visible = true;, and don't call Close() and Quit() methods

Upvotes: 7

Adriaan Stander
Adriaan Stander

Reputation: 166396

You can always display the xlApp using

xlApp.Visible = true;

Upvotes: 15

Related Questions