2011年5月21日 星期六

C#產生Word檔

先加入這幾行
using Microsoft.Office.Interop.Word;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Diagnostics;

然後先這樣:
           Word.ApplicationClass oWord = new Word.ApplicationClass();
            oWord.Visible = true;
            Word.Documents oDocs = oWord.Documents;
            object oFile = "c:\\doc1.doc";

            object oMissing = System.Reflection.Missing.Value;
            Word._Document oDoc = oDocs.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

 處理完之後再這樣:
             oDoc.SaveAs(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing);
            // Quit Word and clean up.
            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oDoc);
            oDoc = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oDocs);
            oDocs = null;
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord);
            oWord = null;

沒有留言:

張貼留言