利用.NET提供的Microsoft.Office.Interop.Word插件来填充资料给WORD模板
来源:互联网 发布:英文期刊数据库 编辑:程序博客网 时间:2024/06/07 22:33
这个是根据WORD中的页签来填充值的方法:
先引用 using Microsoft.Office.Interop.Word;
protected void Button1_Click(object sender, EventArgs e)
{
//利用。NET提供的Microsoft.Office.Interop.Word插件来填充资料给WORD模板。
string strPath = "~/test.doc"; //模板路径
string strPath2 = "~/testtemp.doc";//生成新的WORD的名称与路径
string strWordTemplate = Server.MapPath(strPath); //这里是您的模板文档
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); // 定义一个Word.Application 对象
if (!File.Exists(strWordTemplate))
return ;
Object missing = System.Reflection.Missing.Value;
object fileName = (object)strWordTemplate;
object isVisible = missing;
object readOnly = missing;
WordApp.Visible = false;
//创建一个Word.Document的实例
Microsoft.Office.Interop.Word.Document doc = null;
try
{
doc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing,ref missing);
Response.Redirect(strPath1); //直接打开用ie打开另存的文档,与下面红色部分,是两种打开方法。第一种
}
catch (Exception Ex)
{
throw new Exception(Ex.Message);
}
try
{
foreach (Microsoft.Office.Interop.Word.Bookmark BM in doc.Bookmarks) //这是最关键的地方:对文档的任何书签进行便利匹配
{
switch(BM.Name)
{
case "id2": //替换Advice书签的内容,其他相同
BM.Select();
BM.Range.Text = "李生";
break;
}
}
string strPath1 = "~/testtemp.doc";
object fn = (object)Server.MapPath(strPath1);
doc.SaveAs(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing,ref missing,ref missing,ref missing,ref missing); //这里是另存为一个一个文档
}
catch(Exception err)
{
}
finally
{
doc.Close(ref missing,ref missing,ref missing);
WordApp.Quit(ref missing,ref missing,ref missing);
WordApp = null;
}
FileInfo mode = new FileInfo(@"D:\mywebform\YoungNetFirst\YoungNetFirst\testtemp.doc");//生成新的WORD档的路径。
//打开保存对话框 第二种
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset = "UTF-8";
Response.ContentType = "application/ms-word";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(mode.Name));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.AppendHeader("Content-Length", mode.Length.ToString());
Response.Charset = "";
this.EnableViewState = false;
Response.WriteFile(@"D:\mywebform\YoungNetFirst\YoungNetFirst\testtemp.doc");
//删除创建的WORD文件
FileInfo fileinf = new FileInfo(@"D:\mywebform\YoungNetFirst\YoungNetFirst\testtemp.doc");
fileinf.Delete();
//关闭连接
Response.Flush();
Response.End();
}
- 利用.NET提供的Microsoft.Office.Interop.Word插件来填充资料给WORD模板
- WinForm利用Microsoft.Office.Interop.Word來合併多個Word檔
- Microsoft.Office.Interop.Word 对word 的基本操作
- Microsoft.Office.Interop.Word 对word 的基本操作
- Microsoft.Office.Interop.Word 创建word
- Microsoft.Office.Interop.Word 创建word
- Word转Html ( Microsoft.Office.Interop.Word )
- Microsoft.Office.Interop.Word 创建word
- Microsoft.Office.Interop.Word通过模板生成文档
- Microsoft office interop word下打印机的运用
- 关于Microsoft.Office.Interop.Word类库
- 系统找不到 Microsoft.Office.Interop.Word ---解决方案
- 解决引用Microsoft.Office.Core 和Microsoft.Office.Interop.Word
- asp.net 写入word (Microsoft.Office.Interop.Word)经验记录(看不明白问我)
- asp.net Microsoft.Office.Interop.Word 导出word模版-合并单元格
- .net 引用Microsoft.Office.Interop.Word.dll对word文件进行操作
- [C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)
- 解决引用Microsoft.Office.Core和Microsoft.Office.Interop.Word 的问题
- JQueryEasyUI学习笔记(七)datagrid
- unload data tools--ociuldr3
- NYOJ 286 动物统计
- fedora 18下包冲突的解决办法
- OPENGL中GLU和GLUT工具箱收藏
- 利用.NET提供的Microsoft.Office.Interop.Word插件来填充资料给WORD模板
- 2012蓝桥杯省赛第十题 取球游戏 (SG打表)
- Ubuntu下初试Oracle
- Java的一些概念和例子
- 短信发送器
- Jquery 基础
- 第3章 栈和队列——栈的应用:数制转换
- 蓝桥杯(购物券)
- linux下init等级