• 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组对象
                    }
                }
            }
  • 相关阅读:
    第五周总结
    第四周总结
    第三周总结
    第二周总结
    第一周总结
    暑假学习进度八
    使用nmtui文本框方式修改IP
    Linux 忘记密码配置
    关于公网IP和内网IP
    常见API编写方式(三种)
  • 原文地址:https://www.cnblogs.com/HelloQLQ/p/16289371.html
Copyright © 2020-2023  润新知