最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中的文件在下次使用的时候能正常使用。
代码:
/// <summary>
/// 读取出word文档中的内容后对word文档进行修改并保存在本地
/// </summary>
/// <param name="jtmodel">实体类(在数据库中读取出来的数据)</param>
/// <param name="pathfile">替换后的字符</param>
private void ReadWord(Jtmodle jtmodel, String pathfile)
{
Microsoft.Office.Interop.Word.Document oWordDoc = null;
Microsoft.Office.Interop.Word.ApplicationClass oWordApp = null;
object missing = System.Reflection.Missing.Value;
try
{
object fileName = System.Web.HttpContext.Current.Server.MapPath(pathfile).ToString();
object readOnly = false;
object isVisible = true;
oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
oWordDoc = oWordApp.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);
#region 字符替换
oWordApp = Replce("{htmc}", jtmodel.htmc.ToString(), oWordApp);
oWordApp = Replce("{htbh}", jtmodel.htbh.ToString(), oWordApp);
oWordApp = Replce("{qydd}", jtmodel.qydd.ToString(), oWordApp);
oWordApp = Replce("{qyrq}", jtmodel.qyrq.ToString(), oWordApp);
oWordApp = Replce("{sxrq}", jtmodel.sxrq.ToString(), oWordApp);
oWordApp = Replce("{fkqx}", jtmodel.fkqx.ToString(), oWordApp);
oWordApp = Replce("{jfqx}", jtmodel.jfqx.ToString(), oWordApp);#endregion #region 保存文件到客户端
string filePath = @"d:\\WordDown\\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (!System.IO.File.Exists(@"d:\\WordDown\\" + jtmodel.name.Trim() + ".doc"))
{
oWordDoc.SaveAs(@"d:\\WordDown\\" + jtmodel.name.Trim() + ".doc");
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", @"<script>alert('合同文档生成成功,已保存在本地磁盘D:\\WordDown\\" + jtmodel.name.Trim() + ".doc!');</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", @"<script>alert('该合同文档已经存在本地磁盘D:\\WordDown\\" + jtmodel.name.Trim() + ".doc,生成失败!');</script>");
}
oWordApp.NormalTemplate.Saved = true;
oWordDoc.Close(ref missing, ref missing, ref missing);
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
#endregion
}
catch
{
oWordDoc.Close(ref missing, ref missing, ref missing);
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
}
}
/// <summary>
/// 替换word中的字符串
/// </summary>
/// <param name="oldstr">被替换的符</param>
/// <param name="newstr">替换后的字符</param>
/// <param name="oWordApp"></param>
private Microsoft.Office.Interop.Word.ApplicationClass Replce(string oldstr, string newstr, Microsoft.Office.Interop.Word.ApplicationClass oWordApp)
{
object missing = System.Reflection.Missing.Value;
oWordApp.Selection.Find.ClearFormatting();
oWordApp.Selection.Find.Replacement.ClearFormatting();
oWordApp.Selection.Find.Text = oldstr;
oWordApp.Selection.Find.Replacement.Text = newstr;
object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
oWordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref objReplace, ref missing, ref missing, ref missing, ref missing);
return oWordApp;
}
原本不打算用替换字符串的方式编辑word,在网上看到还有可以在word中添加标签的然后对word文档进行编辑,但是苦于不知道怎么给word文档插入标签,只好适用替换字符串的方法来实现功能了