关于C#向word写入不能超过500字符的一种解决办法

关于C#向word写入不能超过500字符的一种解决办法


思路:在使用C#语言单次使用Replace替换word中的字符时,汉字的格式不能超过250字。具体表现为弹窗显示字符参量过长,此时可以使用插入至word书签处的方法来规避该数量问题。
具体实现方法:
1、在程序引入中添加好Microsoft.Office.Core和Microsoft.Office.Interop.Word两个程序集。
2、单独声明WordClass类(可根据喜好来命名),建立相应的替换函数与字段。代码入下:

class WordClass
    {
        private _Application WordApp = null;
        private _Document WordDoc = null;
        object missing = System.Reflection.Missing.Value;

        public _Application Application
        {
            get
            {
                return WordApp;
            }
            set
            {
                WordApp = value;
            }
        }
        public _Document Document
        {
            get
            {
                return WordDoc;
            }
            set
            {
                WordDoc = value;
            }
        }
        /// <summary>
        /// 通过模板创建文件
        /// </summary>
        /// <param name="filepath"></param>
        public void CreateNewDocument(string filepath)
        {
            killWinWordProcess();
            WordApp = new Application();
            WordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            WordApp.Visible = false;
            object templateName = filepath;
            WordDoc = WordApp.Documents.Open(ref templateName, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }
        /// <summary>
        /// 保存新文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="file"></param>
        public void SaveDocument(string filepath)
        {
            //object fileName = filename;
            object filePath = filepath;
            object format = WdSaveFormat.wdFormatDocument;      //保存格式
            object mode = WdCompatibilityMode.wdWord2007;
            //object missing = System.Reflection.Missing.Value;
            WordDoc.SaveAs2(ref filePath, ref format, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing);
            //关?闭À?WordDoc,ê?WordApp对?象¨®
            object SaveChanges = WdSaveOptions.wdSaveChanges;
            object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
            object RouteDocument = false;
            WordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
            WordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
            killWinWordProcess();
        }
/// <summary>
        /// 插入一段文字,text为文字内容
        /// </summary>
        /// <param name="bookmark"></param>
        /// <param name="text"></param>
        public void InsertText(string bookmark, string text)
        {
            object objstart = bookmark;
            object objrange = WordDoc.Bookmarks.get_Item(ref objstart).Range;
            Paragraph wordpara = WordDoc.Content.Paragraphs.Add(ref objrange);
            wordpara.Range.Text = text;
        }
/// <summary>
        /// 删除winword.exe进程防止影响其他word文档使用
        /// </summary>
        public void killWinWordProcess()
        {
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
            foreach (System.Diagnostics.Process process in processes)
            {
                bool b = process.MainWindowTitle == "";
                if (process.MainWindowTitle == "")
                { process.Kill(); }
            }
        }
        /// <summary>
        /// 替换word中的文字,该函数有字符数限制
        /// </summary>
        /// <param name="strold"></param>
        /// <param name="strnew"></param>
        public void Replace(string strold, string strnew)
        {
            WordApp.Selection.Find.ClearFormatting();
            WordApp.Selection.Find.Replacement.ClearFormatting();
            WordApp.Selection.Find.Text = strold;
            WordApp.Selection.Find.Replacement.Text = strnew;
            object objreplace = WdReplace.wdReplaceAll;
            WordApp.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);
        }
    }

3、在word模板文件中添加书签,具体功能在插入选项中,插入的书签一般不会在word中直接显示,只需要记住书签名即可。
4、使用时实例化类,并根据模板文件创建新word文档,代码入下:

WordClass worder = new WordClass();
worder.CreateNewDocument("模板路径"+ "\\模板文件名.doc");
之后插入内容
worder.InsertText("书签名", 要插入的内容); 
之后进行保存即可
worder.SaveDocument("路径"+"文件名");
MessageBox.Show("保存成功");

5、该文讨论了一种规避word不能单次替换500字符限制的方法,欢迎各位提出批评指正。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值