dailiboy 2015-01-25 00:52 采纳率: 14.3%
浏览 2038

如何将网页中指定DIV中的内容插入到WORD中指定标签位置进行替换?

我用以下代码实现了文本替换指定标签。请问如何实现将网页中指定DIV中的内容对WORD中指定标签进行替换?请给出源码,请尽可能通过这个类进行操作。。

 protected void ReplaceWordDocAndSave(Microsoft.Office.Interop.Word.Document docObject, object savePath)
    {
        object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
        object readOnly = false;
        object isVisible = false;

        string strOldText = "{WORD}";
        string strNewText = "用于替换的文本";

        List<string> IListOldStr = new List<string>();
        IListOldStr.Add("被替换的文本");

        Object Nothing = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        //Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing); 
        Microsoft.Office.Interop.Word.Document oDoc = docObject;

        object FindText, ReplaceWith, Replace;
        object MissingValue = Type.Missing;

        foreach (string str in IListOldStr)
        {

            oDoc.Content.Find.Text = str;
            //要查找的文本 
            FindText = str;
            //替换文本 
            ReplaceWith = strNewText;

            //wdReplaceAll - 替换找到的所有项。 
            //wdReplaceNone - 不替换找到的任何项。 
            //wdReplaceOne - 替换找到的第一项。 
            Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

            //移除Find的搜索文本和段落格式设置 
            oDoc.Content.Find.ClearFormatting();

            if (oDoc.Content.Find.Execute(ref FindText, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue))
            {
                Response.Write("替换成功!");
                Response.Write("<br>");
            }
            else
            {
                Response.Write("没有相关要替换的:(" + str + ")字符");
                Response.Write("<br>");
            }
        }

        oDoc.SaveAs(ref savePath, 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, ref Nothing);

        //关闭wordDoc文档对象     
        oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        //关闭wordApp组件对象     
        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
    } 
  • 写回答

1条回答 默认 最新

  • dailiboy 2015-01-29 05:46
    关注

    顶上去,在线求答案,就差这一步了,求大侠。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的