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. iOS8 对开发人员来说意味着什么?

    今天凌晨.Apple WWDC2014 iOS8 正式推出. 或许,对于广大iOS用户来说,iOS8的创新并非特别多. 但对于开发人员来说,影响却将会是无比巨大的! 正如Apple官网上的广告:Hug ...

  2. HTTP服务器状态码定义

    HTTP服务器状态代码定义 1.1 消息1xx(Informational 1xx) 该类状态代码用于表示临时回应.临时回应由状态行(Status-Line)及可选标题组成, 由空行终止.HTTP/1 ...

  3. Spark MLlib协同过滤算法

    算法说明 协同过滤(Collaborative Filtering,简称CF,WIKI上的定义是:简单来说是利用某个兴趣相投.拥有共同经验之群体的喜好来推荐感兴趣的资讯给使用者,个人透过合作的机制给予 ...

  4. 记录一个mybatis编写xml遇到的错误:java.lang.unsupportedOperationException

    写完xml里的sql在执行xml中的sql时报错,经过排查找到问题出在方法中的resultType这个属性的类型上 如图所示:只需要将sortedSet改为set集合里所存储的对象的类型即可. 这里我 ...

  5. GetInvocationList 委托链表

    最近发现C#程序初始化时在构造函数中,偶尔出现事件注册不成功.后查资料发现有GetInvocationList 这么一个获取类中的委托链表的函数, 使用方法如下: 1.在需委托的类(Class1)中增 ...

  6. js08--函数1

    函数当成类看当成匿名对象看,都是跟java吻合的,只有当成函数看(函数自己可以执行)跟java区别在这里. function fact(){} var F = fact ; 函数名是函数对象的地址,是 ...

  7. Android广告轮播图实现

    先看效果 第一步,布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmln ...

  8. Node里面的对象创建问题

    1.利用new Object()创建时 var a =new Object() a.b = 1 console.log(a) // 打印出来是[object Object] console.log(J ...

  9. git 常用命令(分支)

    查看分支 git branch -r 修改分支名字dev-->test git branch -m dev test 切换分支dev git checkout dev 创建本地分支dev git ...

  10. Core Animation 文档翻译—附录A(Layer样貌相关属性动画)

    前言   在渲染过程中,核心动画获取Layer的各种属性并以特定的顺序渲染他们.这个顺序决定了Layer的最终的样貌.本节将会阐述通过设置不同的Layer样貌相关属性对应产生的渲染结果. 注意:Mac ...