using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;
using Microsoft.Office.Interop.Word;
/// <summary>
/// WriteWord 的摘要说明
/// </summary>
public class WriteWord
{
private Document oDoc;
private ApplicationClass oWordApplic = new ApplicationClass() ;
public WriteWord()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void WriteNewWord()
{
string name = "zhong过";
string age = "20";
string sex = "男";
string date = "2008-10-10";
string wordName = HttpContext.Current.Server.MapPath("ResultMessage/" + "A0.doc");
//try
//{
Open(wordName);
string strtext = "<$name$>";
string ReplaceStr = name;
ReplaceText(strtext, ReplaceStr);
string strtext1 = "<$sex$>";
string ReplaceStr1 = sex;
ReplaceText(strtext1, ReplaceStr1);
string strtext2 = "<$age$>";
string ReplaceStr2 = age;
ReplaceText(strtext2, ReplaceStr2);
string strtext3 = "<$date$>";
string ReplaceStr3 = date;
ReplaceText(strtext3, ReplaceStr3);
SaveAs("D:/CNSI/AO.doc");
// Quit();
//}
//catch (Exception ex)
//{
// Quit();
//}
}
public bool ReplaceText(string findStr, string replaceStr)
{
object replaceAll = WdReplace.wdReplaceAll;
object missing = System.Reflection.Missing.Value;
oWordApplic.Selection.Find.ClearFormatting();
object findText = findStr;
oWordApplic.Selection.Find.Replacement.ClearFormatting();
oWordApplic.Selection.Find.Replacement.Text = replaceStr;
if (oWordApplic.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
{
return true;
}
else
{
return false;
}
}
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
oDoc = oWordApplic.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);
oDoc.Activate();
}
public void Quit()
{
object missing = System.Reflection.Missing.Value;
oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
}
public void SaveAs(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
oDoc.SaveAs(ref fileName, 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);
}
}