HTML转换成word文档
1工具类保存word文件
public class WordAction
{
public static void SaveAsWord(string fileName, string pFileName)//使用原生方法将mht转换为word文档,不是那种直接修改后缀名的方式
{
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object file1 = fileName;
object html1 = pFileName;
object format = WdSaveFormat.wdFormatDocument;
ApplicationClass oWordApp = new ApplicationClass();
oWordApp.Visible = false;
Document oWordDoc = oWordApp.Documents.Open(ref file1, ref format, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
oWordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;//将web视图修改为默认视图,不然打开word的时候会以web视图去展示,而不是默认视图。(唯独这句代码是自己加的 = =|||)
oWordDoc.SaveAs(ref html1, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
oWordDoc.Close(ref missing, ref missing, ref missing);
oWordDoc = null;
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
oWordApp = null;
// killAllProcess(); }
}
2转换
public class HTMLToWord
{
private static void HtmlToMht(string src, string dst)
{
CDO.Message msg = new CDO.Message();
CDO.Configuration c = new CDO.Configuration();
msg.Configuration = c;
msg.CreateMHTMLBody(src, CDO.CdoMHTMLFlags.cdoSuppressNone, "", "");
ADODB.Stream stream = msg.GetStream();
stream.SaveToFile(dst, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
}
/// <summary>
/// html转换成word
/// </summary>
/// <param name="title">word的名称(内容的标题)</param>
/// <param name="content">html的内容</param>
/// <param name="path">转换后word的保存的路径</param>
public static void WriteHtml(string title, string content,string path)//参数内容都是从数据库读出来的文章信息,其中content就是ewebeditor生成的html代码
{
DateTime dt = DateTime.Now;//将string型的日期格式转为DateTime型的因为默认的日期格式不能作为文件名,所以将日期的“:”替换为“-”
string Temp_Name = HttpContext.Current.Server.MapPath("~/Content") + "/HtmlTemp.html";
string File_Name = @"D:\abc\a.html";//生成html文件的路径
string File_NameM = @"D:\abc\b.mht";//生成mht文件的路径
string File_Name2 = path+@"\"+title+".doc";//生成Word文档的路径
StreamReader sr = new StreamReader(Temp_Name);
StringBuilder htmltext = new StringBuilder();
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);//读取到html模板的内容
}
sr.Close();
//替换相应的内容到指定的位置
htmltext = htmltext.Replace("$htmldata[1]", title);
htmltext = htmltext.Replace("$htmldata[2]", title);
htmltext = htmltext.Replace("$htmldata[3]", content);
using (StreamWriter sw = new StreamWriter(File_Name, false, System.Text.Encoding.GetEncoding("UTF-8"))) //保存地址
{
//生成HTML文件
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
HtmlToMht(File_Name, File_NameM);//因为带图片的html直接转为Word的话,图片会以引用的形式展示(也就是说不是内置到word文档里去的,一旦断网或将图片放在别的路径之后,打开word文档图片会显示不出来,所以通过折冲的办法先生成html,然后转换为mht,再转为word)
WordAction.SaveAsWord(File_NameM, File_Name2);//生成word
} }
HTML转换成word文档的更多相关文章
- ABBYY将JPEG文件转换成Word文档的方法
日常工作中处理JPEG格式的图像文件时,有时需要转换成Word文档进行编辑,市场上应用而生了很多转换工具,相信不少人听说过OCR(光学字符识别)软件,可以用来转换图像文件,而在OCR软件中, ABBY ...
- 如何使用ABBYY FineReader 12将JPEG文件转换成Word文档
日常工作中处理JPEG格式的图像文件时,有时需要转换成Word文档进行编辑,市场上应用而生了很多转换工具,相信不少人听说过OCR(光学字符识别)软件,可以用来转换图像文件,而在OCR软件中, ABBY ...
- C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)
这篇博客将要讨论的是关于: 如何从C#的source以及注释, 生成一份Word格式的关于各个类,函数以及成员变量的说明文档. 他的大背景如下...... 最近的一个项目使用C#, 分N个模块, 在项 ...
- 将Latex tex文档转换成 word文档(上)
有时候逼不得已,必须得将自己精心排版好的latex 文档 转换成word 给别人编辑 以下提供一个方法 下载 Tex2Word 工具,地址我的网盘 安装 解压后安装,使用默认安装路径 安装过程中.点击 ...
- 将Latex tex文档转换成 word文档(下)
在上篇中我们介绍了一款将 tex 文件转换成 word 文件的工具 借用万能的搜索引擎,在 Google 上找到了更好的工具 它就是Pandoc 介绍 Pandoc 是由 John McaFarlan ...
- php将html转成word文档下载
<meta charset="utf-8" /> <?php class word{ function start(){ ob_start(); echo '&l ...
- 使用Spire PDF for .NET将HTML转换成PDF文档
目录 开发环境说明 Spire PDF for .NET (free edition)体验 资源下载 开发环境说明 Microsoft Visual Studio 2013 Ultimate Edit ...
- PDF文件可以转换成txt文档吗
PDF是一种便携式的文件格式,传送和阅读都非常方便,是Adobe公司开发的跨平台文件格式,它无论在哪种打印机上都可以保证精确的颜色和准确的打印效果.可是有点遗憾的是PDF格式一般不能在手机上打开,或者 ...
- 网页导出成word文档的默认视图方式问题
网页导出成word文档的默认视图方式问题 一般保存后的word文档默认是“Web版式视图”打开,这样会给客户的感觉不是真正的word文档,必须实现打开就是“页面视图” 1. 修改<html> ...
随机推荐
- <转>Python运行的17个时新手常见错误小结
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在 ...
- Sql中判断“数据库"、"表"、"临时表"、"存储过程"和列”是否存在
--判断数据库是否存在 IF EXISTS (SELECT * FROM MASTER..sysdatabases WHERE NAME = ''库名'') PRINT ''exists ...
- Linux下gcc和g++编译helloworld
linux C(hello world) 1.使用vi/vim进行编写代码并保存为hello_world.c.如下: 1 2 3 4 5 6 /* This is my first C program ...
- .net中的"异步"-手把手带你体验
周二刚过,离5.1小长假还有那么一阵,北京的天气已经开始热起来了.洗完澡,突然想起博客园一位大哥暂称呼元哥吧,当时我写了一篇windows服务的安装教程(http://www.cnblogs.com/ ...
- Web Service学习之九:Restful WebService
1.Rest 2.Rest webService 3.SpringMVC Restful 参考: http://spring.io/guides/gs/rest-service/ http://www ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- thymeleaf中的th:each用法
一.th:eath迭代集合用法: <table> <thead> <tr> <th>序号</th> <th>用户名</th ...
- final static T
/** * An empty table instance to share when the table is not inflated. */ static final Entry<?,?& ...
- #pragma comment使用
编程经常碰到,理解的总不是很透彻,在这里查阅资料总结一下! 在编写程序的时候,我们常用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. #pragma once : 这是一个 ...
- Unity3D之Mecanim动画系统学习笔记(十一):高级功能应用
动作游戏 还记得读书的时候熬夜打<波斯王子>的时光,我们的王子通过跳跃穿过墙壁的小洞.在高层建筑上进行攀爬和跳跃,还有在操作失误掉下高楼和触发必死机关后使用时之沙的时光倒流功能回归死亡之前 ...