C# word操作
Application word = new Application();
Document doc = null;
if (word == null)
{
return;
}
word.Visible = false;
word.DisplayAlerts = WdAlertLevel.wdAlertsNone;
object path = Server.MapPath("..") + "\\Report\\xkcysqb.rtf";
object oMissing = System.Reflection.Missing.Value;
doc = word.Documents.Add(ref path, oMissing, oMissing, oMissing);//加载模板
doc.Select();
object findText = "$$sqr$$";
object replaceText = dt.Rows[0]["sqr"].Equals(DBNull.Value) ? "" : dt.Rows[0]["sqr"].ToString();
object replace = WdReplace.wdReplaceAll;
doc.Content.Find.Execute(ref findText, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, replaceText, replace, oMissing, oMissing, oMissing, oMissing);//标签式替换内容
findText = "$$date$$";
replaceText = dt.Rows[0]["sqdate"].Equals(DBNull.Value) ? "" : dt.Rows[0]["sqdate"].ToString();
doc.Content.Find.Execute(ref findText, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, replaceText, oMissing, oMissing, oMissing, oMissing, oMissing);
String filename = Server.MapPath("..") + "\\Excel\\xkcysqb.rtf";
if (File.Exists(filename))
{
File.Delete(filename);
}
doc.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
word.Quit(oMissing,oMissing,oMissing);
if (doc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
doc = null;
}
if (word != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
word = null;
}
System.GC.Collect();