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文档的几种方式的更多相关文章

  1. pdf如何转换为word文档

    我们经常会遇到需要将PDF转换为WORD文档,对于我来讲,有些PDF没有目录,看起来非常不方便,于是就特别想转成WORD,然后增加目录,想看某一节内容时,快速查找. 这里我总结了一些方法,后续也会不断 ...

  2. ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)

    ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)      上述博文里提到的在线浏览pdf的方案不错,但word转pdf的那个dll只支持doc不支持docx,附上最新的下载链 ...

  3. 网页导出成word文档的默认视图方式问题

    网页导出成word文档的默认视图方式问题 一般保存后的word文档默认是“Web版式视图”打开,这样会给客户的感觉不是真正的word文档,必须实现打开就是“页面视图” 1. 修改<html> ...

  4. c#中操作word文档-一、模板方式写入

    转载自:http://blog.csdn.net/fujie724/article/details/5443322 适合模板写入 今天正好有人问我,怎么生成一个报表式的Word文档. 就是文字的样式和 ...

  5. PHP生成word文档的三种实现方式

    PHP生成word原理 利用windows下面的 com组件 利用PHP将内容写入doc文件之中 具体实现: 利用windows下面的 com组件 原理:com作为PHP的一个扩展类,安装过offic ...

  6. 前端调用后台接口下载word文档的两种方法

    1传统的ajax虽然能提交到后台,但是返回的数据被解析成json,html,text等字符串,无法响应浏览器下载.就算使用bob模拟下载,数据量大时也不方便 废话不多说:上代码(此处是Layui监听提 ...

  7. [置顶] stax解析xml文档的6种方式

    原文链接:http://blog.csdn.net/u011593278/article/details/9745271 stax解析xml文档的方式: 基于光标的查询: 基于迭代模型的查找: 基于过 ...

  8. 第十二节:WebApi自动生成在线Api文档的两种方式

    一. WebApi自带生成api文档 1. 说明 通过观察,发现WebApi项目中Area文件夹下有一个HelpPage文件夹,如下图,该文件夹就是WebApi自带的生成Api的方式,如果该文件夹没了 ...

  9. 使用ghpage(github服务)搭建文档网站几种方式

    可以通过github提供的ghpage服务来搭建网站,有以下三种方式来实现: 1.文档放在master分支,作为一个子目录. 仓库:https://github.com/Ourpalm/ILRunti ...

随机推荐

  1. sync---强制将被改变的内容立刻写入磁盘

    sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...

  2. 洛谷 P2360 地下城主

    P2360 地下城主 题目描述 你参加了一项秘密任务,在任务过程中你被困在了一个3D的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务.地牢由若干层组成,每一层的形状 ...

  3. 软考之路--从生活着手,看PV怎样操作

    PV操作.是软考其中一个非常重要的考点,一听到这个名词,顿时赶脚高大上有么有,在软考的历年试题中,也不乏PV操作的身影,老师也对PV操作进行了一次讲课,那时年少.听得稀里糊涂,也不是非常理解,在小编的 ...

  4. [React] Create a queue of Ajax requests with redux-observable and group the results.

    With redux-observable, we have the power of RxJS at our disposal - this means tasks that would other ...

  5. Java7与G1

    Lucene 4.8開始不支持java6了,所以在下次版本号升级之前我们要先升级至java7. 我使用1/3的全量索引(7.3G).进行測试,20并发,40万请求: sun jdk 1.6.0_26 ...

  6. gridView -item 大小调节(dimen-代码引用)

    今天在修改一个gridview的时候,发现里面的内容并不会自动适应,填满整个gridview,而是会产生滑动,尝试了很多的方法,包括在item文件中设定width和height,结果,宽度可调,高度却 ...

  7. Linux平台下使用AdventNet ManageEngine OpUtils监控网络

    AdventNet ManageEngine OpUtils 是一套系统和网络监视工具,它有Linux/Windows系统平台的免费版和企业版,该软件是一款用于监视诸如路由器,交换机,服务器或者桌面这 ...

  8. c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)

    /// <summary>        /// c#的中英文混合字符串截取(区分中英文)        /// </summary>        /// <param ...

  9. map(froeach改变值,map生成新数组)

    http://www.365mini.com/page/jquery-map.htm <input id="n1" name="uid" type=&qu ...

  10. BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...