将html转换为word文档的几种方式
1 基于wps直接将页面信息下载成word文档
public void test()
{ WPS.Application wps = null;
try
{
wps = new WPS.Application();
}
catch (Exception ex)
{
return;
}
var httpurl = "http://www.baidu.com";
WPS.Document doc = wps.Documents.Open(httpurl, false, true);
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString(); string saveFileName = "D:\\1.doc";
doc.SaveAs(saveFileName, WPS.WdSaveFormat.wdFormatDocument); doc.Close(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
wps.Quit(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
}
这种情况下载的word文档中,样式全乱了,当时参考资料为:http://lanhy2000.blog.163.com/blog/static/4367860820119198575552/
2 用数据流的形式将页面下载成word文档
1>首先获取webUrl页面输出内容
/// <summary>
/// 获取weburl输出内容
/// </summary>
/// <param name="url">weburl</param>
/// <returns>输出内容</returns>
public static string GetPage(string url)
{
WebResponse result = null;
try
{
WebRequest req = WebRequest.Create(new Uri(url));
result = req.GetResponse(); var receivedStream = result.GetResponseStream();
var sr = new System.IO.StreamReader(receivedStream, GetEncoding(GetContentType(result.ContentType).FirstOrDefault().Key));
return sr.ReadToEnd();
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
//ensure that network resources are not wasted
if (result != null)
result.Close();
}
}
2>然后下载生成成word文档
//数据流的方式
public void test2()
{
var pageString = Linkin.Toolkit.Utility.Network.GetPage("http://www.baidu.com");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=result.doc");
System.Web.HttpContext.Current.Response.ContentType = "application/ms-word";
System.Web.HttpContext.Current.Response.Charset = "utf-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Write(pageString);
Response.End();
}
3>生成后的word文档因为没有样式,所有稍微丑了一些,有待优化,如图:

3 基于office com控件,在模板中添加书签的形式,将数据写入word模板中并保存
1>首先需要向工程中的“引用”加入Word类库的引用(如图)。我是Office 2007。其他版本可能略有不同。在COM里面。

2>用Word设计一个模板文档(后缀名*.doc)。(如图)

3>向模板中的需要显示动态内容的地方添加书签。具体方法是。光标落到欲插入内容的地方,选择菜单栏上的“插入”——〉“书签”。
在我的模板中添加完书签的样子如图

4>保存这个已完成的模板到任意路径,例如 D://template.doc
5>具体读取模板,添加数据,保存文件代码如下:
public static void ExportToWord()
{
object oMissing = System.Reflection.Missing.Value;
//创建一个Word应用程序实例
Word._Application oWord = new Word.Application();
//设置为不可见
oWord.Visible = false;
//模板文件地址,这里假设在X盘根目录
object oTemplate = "D://template.doc";
//以模板为基础生成文档
Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
//声明书签数组
object[] oBookMark = new object[];
//赋值书签名
oBookMark[] = "beizhu";
oBookMark[] = "xingming";
oBookMark[] = "xingbie";
oBookMark[] = "chushengriqi";
oBookMark[] = "jiguan"; //赋值任意数据到书签的位置
oDoc.Bookmarks.get_Item(ref oBookMark[]).Range.Text = "使用模板实现Word生成";
oDoc.Bookmarks.get_Item(ref oBookMark[]).Range.Text = "李四";
oDoc.Bookmarks.get_Item(ref oBookMark[]).Range.Text = "女";
oDoc.Bookmarks.get_Item(ref oBookMark[]).Range.Text = "1987.06.07";
oDoc.Bookmarks.get_Item(ref oBookMark[]).Range.Text = "夕阳无限好/r/n只是近黄昏"; string savePath = "D:\\1.doc"; object filename = savePath; oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//关闭word
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
6>到此,保存word文件成功,如图

4 基于WPS com控件,模板表格标志导出word文档,此方式和方式3比较相似
1>首先需要向工程中的“引用”加入wps类库的引用(如图)。

2>在本地任意路径下面创建模板,并在模板中需要添加数据的地方表明标签,例如:D:\\Resume.doc 如图

3>C#代码控制模板,并填充数据
//基于 wps com控件 表格标志 导出word
public void test4()
{ WPS.Application wps = null;
try
{
wps = new WPS.Application();
}
catch
{ }
////获取当前项目的路径
//string path = AppDomain.CurrentDomain.BaseDirectory;
//WPS.Document doc = wps.Documents.Open(path + "Templetes\\Resume.doc", false, true);
WPS.Document doc = wps.Documents.Open("D:\\Resume.doc", false, true); #region 基本信息
var titleTable = doc.Tables.Item();
titleTable.Cell(, ).Range.Text = titleTable.Cell(, ).Range.Text.Replace("{RealName}", "简历名称");
titleTable.Cell(, ).Range.Text = titleTable.Cell(, ).Range.Text.Replace("{ExpectJobCategory}", "期望职位");
titleTable.Cell(, ).Range.Text = titleTable.Cell(, ).Range.Text.Replace("{UpdateTime}", "更新时间");
#endregion string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
string serverPath = @"D:\" + filename + ".doc";
doc.SaveAs(serverPath, WPS.WdSaveFormat.wdFormatDocument); doc.Close(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
wps.Quit(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
}
4>生成后的word文件 如图

5 这种方法相对以上都比较复杂,先保存地址,以待研究
http://www.cnblogs.com/kingteach/archive/2011/11/22/2258801.html
将html转换为word文档的几种方式的更多相关文章
- pdf如何转换为word文档
我们经常会遇到需要将PDF转换为WORD文档,对于我来讲,有些PDF没有目录,看起来非常不方便,于是就特别想转成WORD,然后增加目录,想看某一节内容时,快速查找. 这里我总结了一些方法,后续也会不断 ...
- ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)
ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF) 上述博文里提到的在线浏览pdf的方案不错,但word转pdf的那个dll只支持doc不支持docx,附上最新的下载链 ...
- 网页导出成word文档的默认视图方式问题
网页导出成word文档的默认视图方式问题 一般保存后的word文档默认是“Web版式视图”打开,这样会给客户的感觉不是真正的word文档,必须实现打开就是“页面视图” 1. 修改<html> ...
- c#中操作word文档-一、模板方式写入
转载自:http://blog.csdn.net/fujie724/article/details/5443322 适合模板写入 今天正好有人问我,怎么生成一个报表式的Word文档. 就是文字的样式和 ...
- PHP生成word文档的三种实现方式
PHP生成word原理 利用windows下面的 com组件 利用PHP将内容写入doc文件之中 具体实现: 利用windows下面的 com组件 原理:com作为PHP的一个扩展类,安装过offic ...
- 前端调用后台接口下载word文档的两种方法
1传统的ajax虽然能提交到后台,但是返回的数据被解析成json,html,text等字符串,无法响应浏览器下载.就算使用bob模拟下载,数据量大时也不方便 废话不多说:上代码(此处是Layui监听提 ...
- [置顶] stax解析xml文档的6种方式
原文链接:http://blog.csdn.net/u011593278/article/details/9745271 stax解析xml文档的方式: 基于光标的查询: 基于迭代模型的查找: 基于过 ...
- 第十二节:WebApi自动生成在线Api文档的两种方式
一. WebApi自带生成api文档 1. 说明 通过观察,发现WebApi项目中Area文件夹下有一个HelpPage文件夹,如下图,该文件夹就是WebApi自带的生成Api的方式,如果该文件夹没了 ...
- 使用ghpage(github服务)搭建文档网站几种方式
可以通过github提供的ghpage服务来搭建网站,有以下三种方式来实现: 1.文档放在master分支,作为一个子目录. 仓库:https://github.com/Ourpalm/ILRunti ...
随机推荐
- 解决 php7 cli 模式下中文乱码的两中方法
解决 php7 cli 模式下中文乱码的两中方法1. 给PHP文件开头加上 exec('chcp 936'); 然后把该文件以 ANSI 格式编码2. 在 php.ini 中设置 default_ch ...
- Istio Service Mash管理微服务
Istio Service Mash管理微服务 今天的文章通过Istio开源项目展示如何为Kubernetes管理的微服务提供可见性,弹性,安全性和控制. 服务是现代软件体系结构的核心.比起复杂庞大的 ...
- C++标准库概述
一.C++标准库的主要组件: 1.标准C库 2.I/O流技术(对标准输入输出设备称为标准I/O,对在外磁盘上文件的输入输出称为文件I/O,对内存中指定的字符串存储空间的输入输出称为串I/O) 3.st ...
- 【深入篇】Andorid中常用的控件及属性
TextView android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/al ...
- Index was out of range
Index was out of range. Must be non-negative and less than the size of the collection. Parameter nam ...
- vue+mui+html5+ plus开发的混合应用底部导航的显示与隐藏
1. 模板相关内容(template) <template> <div> <transition :name="transitionName"> ...
- HDU 4696 Answers 水题
http://acm.hdu.edu.cn/showproblem.php?pid=4696 由题意可知 1<=Ci<=2 而且图上一定有环 那么我们可以得出: 只要存在奇环(即Ci=1) ...
- Javascript和jquery事件--事件冒泡和事件捕获
jQuery 是一个 JavaScript 库,jQuery 极大地简化了 JavaScript 编程,在有关jq的描述中,jq是兼容现有的主流浏览器,比如谷歌.火狐,safari等(当然是指较新的版 ...
- hostname---显示和设置系统的主机
hostname命令用于显示和设置系统的主机名称.环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器之后还是原来的主机名. ...
- 推断字符串string是数字、json结构、xml结构
import org.json.JSONException; import org.json.JSONObject; import org.dom4j.DocumentException; impor ...