打开现有的Word文档,编辑其书签并使用Open Xml方法保存为新文档

时间:2012-06-19 08:12:56

标签: openxml

我想使用Open Xml Open Method打开文档文件,在文档内搜索书签,将文本框中的文本或aspx页面中的标签分配给书签,最后将其保存到新文档中。在我使用Office Interop Word方法之前。现在我想在Open Xml方法中尝试相同的东西。但我无法实现它。你可以帮帮我吗?在此先感谢。

供您参考我的办公室互操作代码如下,

object Nothing = System.Reflection.Missing.Value;
    object format = Word.WdSaveFormat.wdFormatDocument;

    Word.Application wordApp = new Word.ApplicationClass();

    object srcFileName = Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Templates\Employment Certificate.doc"));

    Word.Document wordDoc = wordApp.Documents.Open
   (ref srcFileName, ref format, ref Nothing, ref Nothing,
   ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
   ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
   ref Nothing, ref Nothing);

        object bookmarkDate = "Date";
        wordDoc.Bookmarks.get_Item(ref bookmarkDate).Select();

        wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblRequestdate.Text);

        string DocName;
        DocName = string.Format("{0}_employment_certificate", lblRequestNo.Text);
        hFilename.Value = DocName;
        wordDoc.SaveAs(Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Letters\" + DocName + ".doc")));


        wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        if (wordDoc != null)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
            wordDoc = null;
        }

        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        if (wordApp != null)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
            wordApp = null;
        }
    GC.Collect();

1 个答案:

答案 0 :(得分:0)

我可以使用Open Xml方法实现它。

相关问题