您的位置:首页 > 其它

word 字符串参量过长 解决

2016-06-30 13:35 330 查看
动态生成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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: