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> ...
随机推荐
- java ant 命令大全
ANT命令总结 1 Ant是什么? Apache Ant 是一个基于 Java的生成工具.生成工具在软件开发中用来将源代码和其他输入文件转换为可执行文件的形式(也有可能转换为可安装的产品映像形式).随 ...
- iOS学习网站及大牛网址(实时更新)
iOS学习网站及大牛网址(实时更新) 学习网站 https://github.com/Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库 https://github.co ...
- mac搭建PHP开发环境
在Mac系统上搭建Php服务器环境: LAMP: Linux Apache MySQL PHP MAMP: MACOS APACHE(自带) MYSQL(需自己安装) PHP(自带) 一.APACHE ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...
- Spark的应用程序
Spark的应用程序,分为两部分:Spark driver 和 Spark executor.
- 关于scrollTop的那些事
大家在实际项目中,应该是要经常用到scrollTop的,它表示的是可视窗口距离页面顶部的距离,这个scrollTop是可读写的,所以可以用来做页面滚动. 但是大家或多或少遇到一些浏览器兼容问题,为什么 ...
- document.documentElement.clientHeight||document.documentElement.scrollHeight
在我看<JavaScript高级程序设计>(第三版)的时候,在clientHeight和scrollHeight那部分把我弄糊涂了. 原书是这样写的:( //宽度同理,就不仔细描述了.) ...
- linux@64 获取时间的性能评估
听人说gettimeofday 在64bit下有缓存,速度很快,测试下了,感觉不对啊.. #include <time.h> #include <sys/time.h> #in ...
- Codeforces 599C Day at the Beach(想法题,排序)
C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...
- [iOS UI进阶 - 2.1] 彩票Demo v1.1
A.需求 1.优化项目设置 2.自定义导航栏标题按钮 3.多版本处理 4.iOS6和iOS7的适配 5.设置按钮背景 6.设置值UIBarButtonItem样式 code source: htt ...