asp.net word操作处理

简介:

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/1539239.html,如需转载请自行联系原作者


相关文章
|
7月前
|
开发框架 前端开发 .NET
ASP.NET WEB——项目创建与文件上传操作
ASP.NET WEB——项目创建与文件上传操作
95 0
|
7月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
212 0
|
7月前
|
XML 开发框架 .NET
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
76 1
|
存储 开发框架 NoSQL
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
344 0
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
|
开发框架 JavaScript 前端开发
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
441 0
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
157 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
150 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
157 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
124 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
123 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)