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