在ASP.NET里操作Word的第一步就是添加COM引用到你的工程里,通过右键点击“解决方案资源管理器”的“引用”,添加引用。选择COM选项卡,添加Microsoft Word 12.0 Object Library(其中12.0是Word版本号,根据当前电脑上安装的Word版本确定)。 ASP.Net会自动生成Word的COM包装类程序集添加到应用程序目录里。

using Word;

public string creatwordFile()
{
object Missing = Type.Missing;
//取得Word文件路径
string strTemp = "Doc/联盟推广协议.doc";
//新Word文件保存路径
string newFileName = "doc/"+name+".doc";
//创建一个名为WordApp的组件对象

string strBM;
object objBM;
Word.Application WordApp = new Word.ApplicationClass();
//必须设置为不可见
WordApp.Visible = false;
try
{
//创建以strTemp为模板的文档
object oTemplate = Server.MapPath(strTemp);
Word.Document WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
WordDoc.Activate();

//对标签"cpname"进行填充
strBM = "cpname";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_cpname.Value.ToString());
}

strBM = "cpname2";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_cpname.Value.ToString());
}

strBM = "cpname3";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_cpname.Value.ToString());
}

strBM = "address";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_regaddress.Text);
}

strBM = "regcode";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_regcode.Text);
}
strBM = "sqdb";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_sqdb.Value.ToString());
}
strBM = "address2";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_address.Value.ToString());
}
strBM = "postcode";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_postcode.Text.ToString());
}
strBM = "tel";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_tel.Value.ToString());
}
strBM = "fax";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_fax.Value.ToString());
}
strBM = "mail";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(Txt_mail.Text.ToString());
}
strBM = "bankname";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_bankname.Text.ToString());
}
strBM = "accountname";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(Txt_accoutname.Text.Trim());
}
strBM = "bankaccount";
objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText(txt_account.Text.Trim());
}

//保存为新文件
object oNewFileName = Server.MapPath(newFileName);
WordDoc.SaveAs(ref oNewFileName, 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);
WordDoc.Close(ref Missing, ref Missing, ref Missing);

WordApp.Quit(ref Missing, ref Missing, ref Missing);

return name + ".doc";
}
catch (Exception ex)
{
return ex.Message;
}

}

本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/08/05/1539243.html,如需转载请自行联系原作者

asp.net word操作相关推荐

  1. 【译】在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本

    本篇文章是讲述使用iTextSharp这个开源组件的系列文章的第三篇,iTextSharp可以通过Asp.Net创建PDFs,就像HTML和ASP.Net为文本提供了多种容器一样,iTextSharp ...

  2. 计算机一级必考知识点,计算机一级word操作知识点

    操作部分提纲 第一部分文字录入 1.注意打出来的内容和题目参照内容要对齐. 2.注意一些比较特殊的标点符号 \ 是顿号._是破折号--^是省略号--@是分隔号·其他特殊符号可以通过输入法状态栏上的软键 ...

  3. 黄聪:C#编写的Word操作类,有换页,添加表格,文本功能

    最近要帮老师做个工资管理系统,需要自动生成Word. 就上网找了个Word操作类,再做了点修改,下面公布一下自己的代码: using System; using System.Collections. ...

  4. 计算机一级考试word操作题主要题型,(word)计算机一级考试题型.doc

    (word)计算机一级考试题型 题型内容题数分值单选题基础知识4共30题 每题1分 计30分数制与编码2硬件知识4软件知识4Windows3Word2EXCEL2网络知识6多媒体知识1安全与病毒知识2 ...

  5. 浙江高校计算机等级考试二级办公,浙江省高校计算机等级考试二级(高级办公)Word操作提示.doc...

    浙江省高校计算机等级考试二级(高级办公)Word操作提示 说明:经过多位教师的反复验证,以下操作步骤能做出与效果图一样的文档. 具体操作步骤如下: 1(1)~(2)操作: 选择"格式&quo ...

  6. asp html转word文档,asp生成word文档

    asp生成word文档 今天在处理一个在线申请功能时候,需要生成word文档.在网上找了些资料学习,终于成功了.下面是个简单的例子.其中程序包括index.htm,createdoc.asp.在之前你 ...

  7. 计算机关于word试题及答案,职称计算机考试Word操作试题及答案

    一.要求:新建一个书法字帖(字帖中加入"白"和"百"两个字). [考点]:创建书法贴 [操作步骤]: 1.左键点"Office 按钮",选择 ...

  8. 关于Aspose对于Word操作的一些扩展及思考

    Aspose.word Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务.Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocu ...

  9. python 手机自动化操作_Python自动化办公之word操作

    Python自动化办公之word操作,主要是用到python-docx库,针对word操作,对大批量重复性工作,使用python可以节省大量的时间和经历 python-docx word文档中的文字有 ...

最新文章

  1. 王贻芳院士:为什么中国要探究中微子实验?
  2. PyCaret-低代码ML库使用指南
  3. php stortime,文件存储 - Laravel 5.8 中文文档手册 - php中文网手册
  4. android找不到aar包
  5. [web 安全] php随机数安全问题
  6. 软定时器的删除与状态查询
  7. angular2.0学习日记1
  8. MSSQL WITH (NOLOCK) 脏读
  9. ES6(三)——Set、WeakSet、Map、WeakMap
  10. 这下真“火”了!为涨粉在红绿灯底座上跳舞 两女子被行拘...
  11. K-means++算法的学习笔记~
  12. Unable to find the requested .Net Framework Data Provider
  13. 熟练掌握各种MQ?那面试官很可能用这道题目先摸摸你的老底!
  14. 图片验证码是如何刁难用户的?一招教你彻底去除图片验证码!
  15. 网易我的世界qq版服务器维护中,网易《我的世界》官方发布Hypixel中国版服务器停止运营公告...
  16. DDR的Controller、Channel、Chip、Rank、Bank、Row、Column、Sided
  17. 【第一个Vue上手小项目Day4】史上最简单的Element-table表格+Pagination 分页(前后端结合)
  18. 常犇_专访丨《河神》制片人常犇:走夜路不怕黑,做好剧别怕累
  19. C语言 - 巧解正数,负数以及零的按位取反
  20. h5嵌入android的视频播放器

热门文章

  1. LSSVM分类和回归
  2. 浅谈Android之SurfaceFlinger相关介绍(一)
  3. MSYS 1.0.11 + MinGW安装方法
  4. 记一次Max模型导入到GIS平台歪了,尺寸不对过程分析
  5. Unity数据可视化 温度图效果(一)
  6. 高德地图完整功能的html,揭秘高德地图八大不为人知的强大功能
  7. LiDAR点云处理软件
  8. Nginx (一) --------- Nginx 简介
  9. ui设计培训课程是哪些
  10. 二本大龄程序员居然拿到百度offer(百度面经)