word 字符串参量过长 解决

动态生成word文档,传参数过长处理。

   public static void ReplaceToWord(object path, Dictionary<string, string> datas, string newfile, string physicNewFile)
        {
            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word._Document doc = null;
            try
            {
                app = new Microsoft.Office.Interop.Word.Application();//创建word应用程序                
                object fileName = path;//模板文件C:\Users\Administrator\Desktop\
                //打开模板文件                 
                object oMissing = System.Reflection.Missing.Value;
                doc = app.Documents.Open(ref fileName,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                object replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                                
                foreach (var item in datas)
                {
                    app.Selection.Find.Replacement.ClearFormatting();
                    app.Selection.Find.ClearFormatting();
                    if (item.Value.Length > 220)
                        FindAndReplaceLong(app, item.Key, item.Value);
                    else
                        FindAndReplace(app, item.Key, item.Value);
                    //2016-06-30字符串太长
                    //app.Selection.Find.Text = item.Key;//需要被替换的文本                    
                    //app.Selection.Find.Replacement.Text = item.Value;//替换文本
                    执行替换操作
                    //app.Selection.Find.Execute(
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing, ref oMissing,
                    //ref oMissing, ref replace,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing);
                }

                //对替换好的word模板另存为一个新的word文档
                doc.SaveAs(physicNewFile,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭word文档
                }
                if (app != null)
                {
                    app.Quit();//退出word应用程序
                }
            }
        }
        public static void FindAndReplaceLong(Word.Application wordApp, object findText, object replaceText)
        {
            int len = replaceText.ToString().Length;   //要替换的文字长度
            int cnt = len / 220;                    //不超过220个字
            string newstr;
            object newStrs;
            if (len < 220)   //小于220字直接替换
            {
                FindAndReplace(wordApp, findText, replaceText);
            }
            else
            {
                for (int i = 0; i <= cnt; i++)
                {
                    if (i != cnt)
                        newstr = replaceText.ToString().Substring(i * 220, 220) + findText;  //新的替换字符串
                    else
                        newstr = replaceText.ToString().Substring(i * 220, len - i * 220);    //最后一段需要替换的文字
                    newStrs = (object)newstr;
                    FindAndReplace(wordApp, findText, newStrs);                              //进行替换
                }
            }
        }


        public static void FindAndReplace(Word.Application wordApp, object findText, object replaceText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;
            wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards,
                ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceText,
                ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
        }

Asp.Net操作Word内容

如果要正常操作Word Com组件的话,必须要给用户赋上足够的权限的,

1、运行Dcomcnfg.exe

2、组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档
3、点击属性
4、选择“安全性”
5、选定“使用自定义访问权限”和“使用自定义启动权限”
6、分别编辑权限,添加ASPNET,VS Developers,Debugger User //不一定
7、选择“身份标识”,在选定“交互式用户” 即可 (关键步骤)必须
8、在Web.config里加 <identity impersonate="true"/>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值