C#创建Excel(完成)然后显示/打开它?救命

Dan*_*ter 3 c# excel

我可以创建一个excel并保存它,但我更愿意显示该文档!任何帮助都会很棒!! 谢谢

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();
Run Code Online (Sandbox Code Playgroud)

Adr*_*der 14

您始终可以使用显示xlApp

xlApp.Visible = true;
Run Code Online (Sandbox Code Playgroud)


den*_*kom 7

插入此行xlApp.Visible = true;,不要调用Close()Quit()方法