asp.net word操作处理-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: