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. OL记载Arcgis Server切片

    概述: 本文讲述怎样在OpenLayers中调用Arcgis Server切片并显示. 思路: 在OpenLayers中载入Arcgis Server切片用XYZ图层,Arcgis Server的切片 ...

  2. 通过Debug-->Attach to Process的方式来调试网站

    找到网站所对应的应用程序池

  3. DOM节点的创建、插入、删除、查找、替换

    在前端开发中,js与html联系最紧密的莫过于对DOM的操作了,本文为大家分享一些DOM节点的基本操作. 一.创建DOM节点 使用的命令是 var oDiv = document.createElem ...

  4. javafx style and cssFile

    public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...

  5. JNDI学习总结(2)——JNDI数据源的配置

    一.数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)    ②连接数据库(Con ...

  6. 公告:本博客搬迁到:http://www.courtier.cc

    公告:       您好,本人意见本博客搬迁到:http://www.courtier.cc

  7. [AngularFire] Resolve snapshotChanges doesn't emit value when data is empty

    Updated to AngularFire2 v5.0. One important change is that you need to call .snapshotChanges() or .v ...

  8. 取消xp开机默认登陆账户

    取消xp开机默认登陆账户 建了个新用户,把以前的用户删除后重新启动电脑,始终停留在 "正在启动" 界面,网上说是 Event Log:Eventlog(系统日志纪录服务) 没有自动 ...

  9. android-LinearLayout 控件占满父容器位置实现

    经常碰到需要把一个控件放在手机底部的情况,以前都是在LinearLayout尝试使用gravity="bottom" ,但是,没有效果,后来在网上查到了方法,如下 <Line ...

  10. node----ajax请求太大报错------解决方法

    //----分析主体程序var bodyParser = require(‘body-parser‘); app.use(bodyParser.json({limit: ‘50mb‘})); app. ...