2009-11-17 52 views
2

我可以創建一個excel並保存它,但是我寧願顯示該文檔!任何幫助將是偉大的!由於C#創建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(); 

回答

9

可以使用

xlApp.Visible = true; 
+0

大,謝謝! –

+0

這個文件在哪裏暫時保存?我們是否需要刪除稍後創建的文件? – devan

7

插入該行xlApp.Visible = true;始終顯示xlApp,並且不叫Close()Quit()方法

+0

非常感謝您! –