winform 使用Clipboard 和windows Word Com組件 把Html 導出到word

首先是把Html複製到剪貼板
然後使用:
private void saveAsWordCopy(string destFileName)
        {


            Microsoft.Office.Interop.Word.Application wordApp = null;       //聲明word應用程序變量
            Microsoft.Office.Interop.Word.Document worddoc = null;

            object Nothing = Missing.Value;           //COM調用時用於佔位
            try
            {
                object path;                      //聲明文件路徑變量

                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; //Word文檔的保存格式
                wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();              //聲明一個wordAPP對象
                worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                worddoc.ActiveWindow.View.SeekView = oWord.WdSeekView.wdSeekPrimaryFooter;
                wordApp.Selection.HeaderFooter.LinkToPrevious = false;
                wordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = oWord.WdParagraphAlignment.wdAlignParagraphCenter;

                wordApp.Selection.HeaderFooter.Range.Text = string.Empty;

                object oNumberAlignment = oWord.WdPageNumberAlignment.wdAlignPageNumberCenter;
                object oFirstPage = true;
                wordApp.Selection.HeaderFooter.PageNumbers.Add(ref oNumberAlignment, ref oFirstPage);
                wordApp.ActiveWindow.View.SeekView = oWord.WdSeekView.wdSeekMainDocument;

                var data = HtmlFData(strHtml);

                MemoryStream sMem = new MemoryStream();

                byte[] byteHtml = Encoding.UTF8.GetBytes(data);

                sMem.Write(byteHtml, 0, byteHtml.Length);
                sMem.Flush();

                var dataObject = new DataObject();
                dataObject.SetData(DataFormats.Html, sMem);

                Clipboard.SetDataObject(dataObject);

                worddoc.Paragraphs.Last.Range.Paste();
                //設置段落段後格式 add by hq 20200527
                worddoc.Paragraphs.SpaceAfter = 0;
                worddoc.Paragraphs.LineUnitAfter = 0;
                Clipboard.Clear();
                sMem.Close();

                path = destFileName;       //設置文件保存路勁
                worddoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                worddoc.Close(ref Nothing, ref Nothing, ref Nothing);  //關閉worddoc文檔對象
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   //關閉wordApp組對象

                wordApp = null;


            }
            catch (Exception ex)
            {
                if (worddoc != null)
                {
                    worddoc.Close(ref Nothing, ref Nothing, ref Nothing);  //關閉worddoc文檔對象
                }
                if (wordApp != null)
                {
                    wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   //關閉wordApp組對象
                }
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章