博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
word 生成HTML
阅读量:4633 次
发布时间:2019-06-09

本文共 2763 字,大约阅读时间需要 9 分钟。

View Code
1           string wordPath = Server.MapPath("/Fileword/" + FileUpload1.FileName);           2             string htmlPath = Server.MapPath("/Fileword/测试.html"); 3             //上传word文件 4             FileUpload1.SaveAs(wordPath); 5             #region 文件格式转换 6             //请引用Microsoft.Office.Interop.Word 7             ApplicationClass word = new ApplicationClass(); 8             Type wordType = word.GetType(); 9             Documents docs = word.Documents;10             // 打开文件11             Type docsType = docs.GetType();12             object fileName = wordPath;13             Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, (object)docs, new Object[] { fileName, true, true });14             //判断与文件转换相关的文件是否存在,存在则删除。(这里,最好还判断一下存放文件的目录是否存在,不存在则创建)15             if (File.Exists(htmlPath)) { File.Delete(htmlPath); }16             ////每一个html文件,有一个对应的存放html相关元素的文件夹(html文件名.files)17             if (Directory.Exists(htmlPath.Replace(".html", ".files")))18             {19                 Directory.Delete(htmlPath.Replace(".html", ".files"), true);20             };21             //转换格式,调用word的“另存为”方法22             Type docType = doc.GetType();23             object saveFileName = htmlPath;24             docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML });25             // 退出 Word26             wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);

这样生成的html , 会有很多冗余HTML,去除冗余HTML方法如下

View Code
1  public static string CleanWordHtml(string html) 2     { 3         StringCollection sc = new StringCollection(); 4         sc.Add(@"
"); 6 sc.Add(@""); 7 sc.Add(@"\s?class=\w+"); 8 sc.Add(@"<(meta|link|/?o:|/?font|/?strong|/?st\d|/?head|/?html|body|/?body|/?w:|/?m:|/?v:|!\[)[^>]*?>"); 9 sc.Add(@"(<[^>]+>)+ ()+");10 sc.Add(@"
(\w|\W)+?
");//清除xml标签及所有值 11 sc.Add(@"(\n\r){2,}"); 12 foreach (string s in sc)13 {14 html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase);15 }16 17 foreach (Match match in Regex.Matches(html, "style='[^']+'", RegexOptions.IgnoreCase))18 {19 html = html.Replace(match.Value, match.Value.Replace('"', ' ').Replace("'","\""));20 21 }22 html = Regex.Replace(html, @"(?<=style=['""])[^'""]*(?=['""])", delegate(Match m)23 {24 return string.Join(";", m.Value.Split(';').Where(t => Regex.IsMatch(t.Trim(), @"^(background|color):")).ToArray());25 });26 27 return html;28 29 }

可根据自己的需求修改正则

 

转载于:https://www.cnblogs.com/1987kevin/archive/2013/03/25/2980724.html

你可能感兴趣的文章
(转载)虚幻引擎3--第三章–Unreal中的类
查看>>
SQL Server 系统表使用举例
查看>>
理解JavaScript里的 [].forEach.call() 写法
查看>>
eclipse 导入web项目时常见错误
查看>>
POM.XML文档汉化
查看>>
注册表中LEGACY残留项的清理技巧
查看>>
如何制作中文Javadoc包,并导入到Eclipse
查看>>
6月21 百度文本编辑器
查看>>
每日记载内容总结28
查看>>
将图片文件转换为.py文件
查看>>
Lua中table的实现-《Lua设计与实现》
查看>>
二十三种设计模式[3] - 生成器模式(Builder Pattern)
查看>>
异常-Throwable的几个常见方法
查看>>
Delphi Property详解
查看>>
jquery禁用a标签,jquery禁用按钮click点击
查看>>
ccleaner Command-line parameters
查看>>
课后作业-阅读任务-阅读提问-3
查看>>
LIBCLNTSH.SO: WRONG ELF CLASS: ELFCLASS32错误一例
查看>>
如何禁止特定用户使用sqlplus或PL/SQL Developer等工具登陆?
查看>>
Ubuntu 14.04安装配置NFS服务器
查看>>