HTML2Word

目前转换html与word有很多方案,这里只说明windows下的。

  1. 使用HtmlToOpenXml库,直接转换,问题是只支持基础的,不支持CSS(效果一般)
  2. 使用Xceed收费版,insertContent, 自带RTF/HTML转换功能(没用过)
  3. 使用Office自带的打开和另存为功能,间接转换。(效果最接近)

参考代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xceed.Words.NET;
using Xceed.Document.NET;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using HtmlToOpenXml;
using MSWord = Microsoft.Office.Interop.Word;namespace docxdemo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){// 将一个文档合并到另外一个里面using (var document = DocX.Load("file1.docx")){// goto end of documentfor(var i = 0; i < 3; i++) { using (var document2 = DocX.Load("file2.docx")){// Insert a document at the end of another document.// When true, document is added at the end. When false, document is added at beginning.document.InsertDocument(document2, true, false, MergingMode.Local);}}// Save the changes to the documentdocument.SaveAs("demo.docx");System.Diagnostics.Process.Start("demo.docx");}}private void button2_Click(object sender, EventArgs e){// 通过第三方库将html转换为wordconst string filename = "file2.docx";string html = textBox1.Text;if (File.Exists(filename)) File.Delete(filename);using (MemoryStream generatedDocument = new MemoryStream()){using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)){MainDocumentPart mainPart = package.MainDocumentPart;if (mainPart == null){mainPart = package.AddMainDocumentPart();new DocumentFormat.OpenXml.Wordprocessing.Document(new Body()).Save(mainPart);}HtmlConverter converter = new HtmlConverter(mainPart);converter.ParseHtml(html);mainPart.Document.Save();}File.WriteAllBytes(filename, generatedDocument.ToArray());}System.Diagnostics.Process.Start(filename);}private void button3_Click(object sender, EventArgs e){// 通过office自带的功能将html转换为wordstring source = "demo.htm";string target = "./msdemo.docx";using (var ht = File.Open(source, FileMode.OpenOrCreate)){var txt = $"<html>{ textBox1.Text}</html>";var buff = Encoding.UTF8.GetBytes(txt);ht.Write(buff, 0, buff.Length);ht.Close();}MSWord._Application newApp = new MSWord.Application();//try//{//    this.app = (Application)Microsoft.VisualBasic.Interaction.GetObject(null, "Word.Application");//}//catch//{//    this.app = new Application();//}MSWord.Documents d = newApp.Documents;object Unknown = Type.Missing;object format = MSWord.WdOpenFormat.wdOpenFormatWebPages;  //Microsoft.Office.Interop.Word.WdOpenFormatobject enc = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; //msoEncodingAutoDetect;object src = Path.GetFullPath("./demo.htm");MSWord.Document od = d.Open(ref src, false,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref format, ref enc,ref Unknown, ref Unknown, ref Unknown, ref Unknown);object targ = Path.GetFullPath("./msdemo.docx");object format2 = MSWord.WdSaveFormat.wdFormatDocumentDefault;od.SaveAs2(targ, ref format2,//newApp.ActiveDocument.SaveAs(targ, ref format2,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown);od.Close(MSWord.WdSaveOptions.wdDoNotSaveChanges);newApp.Quit(MSWord.WdSaveOptions.wdDoNotSaveChanges);System.Diagnostics.Process.Start(targ.ToString());}}
}

HTML转为office WORD方案相关推荐

  1. 研究人员发现Office Word 0Day攻击 这个漏洞绕过了word宏安全设置 绿盟科技、McAfee及FireEye发出警告...

    这次的0Day漏洞确实很厉害,以往攻击者诱使用户点击Word文档,由于其中包含了恶意脚本,大多数需要用户启用了宏.但这次的漏洞不是,受害者无需启用宏,也会中招,而且漏洞覆盖Windows所有版本(包括 ...

  2. Office Word 不显示 Citavi 插件,如何修复?

    Word 没有 Citavi 插件,没有一个唯一的解决方案,问题可能是多种多样的,一般需要进行排查解决问题.但是一般分为两大类: 1.安装 Citavi 过程中可以看到 Word 插件选项,但是安装成 ...

  3. mac上设置ms office word初始窗口大小

    mac上设置ms office word初始窗口大小 在Mac上打开word,总是那么小,还需要用鼠标拉大,十分麻烦.但是,Mac上又没有设置初始窗口大小的功能,同样ms word也没有该设置. 初始 ...

  4. smartfroms word编辑_请教Microsoft Office Word 2007 SmartArt图形文本如何直接在里面添加字,修改字。如图。...

    展开全部 观型外观 Office PowerPoint 2007 具有一个称为 Microsoft Office Fluent 用户界面 的全新直观用户界面:与早期版本的 PowerPoint 相比, ...

  5. Microsoft office Word 批注相关问题解决

    Microsoft office word 批注相关问题解决 目录 Microsoft office word 批注相关问题解决 1.增添并显示批注 2.批注显示及取消操作 3.更改批注者姓名 4.将 ...

  6. Win8 x64 + Office Word 2013 x64 无法自动加载 Endnote X6 的解决方案

    此篇技术解决贴之前发布在PLOB上,阅读7700+,原文链接 https://www.plob.org/article/5932.html .最近新开了自己的CSDN博客,重发自己的原创作品. 问题描 ...

  7. 使用ABAP编程实现对微软Office Word文档的操作

    SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...

  8. Office word 2007不能另存为pdf格式的解决方法

    Office word 2007不能另存为pdf格式的解决方法 参考文章: (1)Office word 2007不能另存为pdf格式的解决方法 (2)https://www.cnblogs.com/ ...

  9. office(Word、Excel、PPT等图标异常和桌面无新建解决方案)

    office(Word.Excel.PPT等图标异常和桌面无新建解决方案) 参考文章: (1)office(Word.Excel.PPT等图标异常和桌面无新建解决方案) (2)https://www. ...

最新文章

  1. Line上半年扭亏为盈 用户及营收遇瓶颈
  2. 金华杭州计算机学校录取分数线,2017年浙江金华各地中考录取分数线
  3. 一个软件公司需要多少前端_制作一个小程序商城需要多少钱?开发小程序公司哪家强?...
  4. 人形AI捉迷藏惊煞网友:飞檐走壁纯靠自学,表情丰富还会合作,姚班学霸吴翼参与...
  5. centos 7.9 scp命令 实现远程拷贝文件
  6. 【机器学习】数据挖掘算法——关联规则(三),FP-growth算法
  7. 1.9 编程基础之顺序查找 01 查找特定的值
  8. IntelliJ IDEA版本:Ultimate、Community、EAP版本的区别
  9. Windows Phone
  10. iphone pageController 的实现
  11. 分布式与集群(一):我眼中的分布式与集群
  12. mysql s.`name`_MySQL的基础操作命令
  13. python60行绘图程序_不敢相信,60行python代码就写出了贪吃蛇游戏
  14. 一起谈谈 Java 9 的新特性
  15. 【设计模式】简单工厂模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )
  16. idea 中部署 tomcat
  17. Biotin-PEG8-amine,2183447-27-8具有末端伯胺基的生物素化试剂
  18. (3/300)分部积分法
  19. LSF---【bsub命令】
  20. 关于texlive2021安装,一直卡在安装界面怎么回事?

热门文章

  1. 深圳软件测试培训:测试中常见移动端基础知识
  2. Tomcat 打印日志让你事半功倍
  3. 达梦8之管理工具超实用小技巧
  4. 前端框架Layui学习五:弹出层和数据表格
  5. 计算机显卡维修,显卡的各种故障维修方法
  6. 下载的视频太大无法放进u盘
  7. .pfx数字证书制作及操作使用
  8. 基于移动平台的线激光扫描物体三维重建
  9. 李彦宏:未来五年语音图片搜索将超过文字
  10. 短信平台系统搭建 后台功能管理 web源码架构 多线路由通道详解介绍