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