系统相关
首页 > 系统相关> > winform 使用Clipboard 和windows Word Com组件 把Html 导出到word

winform 使用Clipboard 和windows Word Com组件 把Html 导出到word

作者:互联网

首先是把Html复制到剪贴板 见:https://www.cnblogs.com/HelloQLQ/p/16289343.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组对象
                }
            }
        }

 

标签:sMem,Word,windows,worddoc,wordApp,Html,Nothing,ref
来源: https://www.cnblogs.com/HelloQLQ/p/16289371.html