C#操作Word之TypeText()寫文本


之前我們采用Find.Replacement.Text的方式對word模板上的內容進行替換,Replacement是專門為文本替換而生,但是當我們替換的內容超過一定字符數會出現“字符串參量過長。”異常錯誤。所以本節我們采用TypeText('字符')來代替原來的Find.Replacement.Text,因為TypeText專職用來寫文本用的,所以不會因為字符數影響。

我們還是采用之前的例子,對用戶的一個登記信息導入到word,有一個人信息介紹的單元格,該信息很容易超過500個字符

 姓名  張三  性別  男
 籍貫  浙江  學歷  本科
 家庭地址  浙江省未名市未名區未名街
 電話  12345678  省份證號  123456789012345678
個人信息介紹 aaaaaaaaaaaa……

還是先准備類似的word模板

 姓名  {name}  性別  {sex}
 籍貫  {provinve}  學歷  {education}
 家庭地址  {address}
 電話  {telephone}  省份證號  {cardno}
個人信息介紹  {info}

下面上代碼

 /// <summary>
        /// 用TypeText替換word中的文本
        /// </summary>
        protected void TypeTextToExcel()
        {
            Word.Application app = null;
            Word.Document doc = null;
            //新的word文檔路徑
            string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
            string physicNewFile = Server.MapPath(newFile);
            try
            {  
                object oMissing = System.Reflection.Missing.Value;
                //模板文檔
                object fileName = Server.MapPath("template.doc");
                app = new Word.Application();//創建word應用程序
                //打開模板word文檔
                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);

                //構造數據
                Dictionary<string, string> datas = new Dictionary<string, string>();
                datas.Add("{name}", "張三");
                datas.Add("{sex}", "");
                datas.Add("{provinve}", "浙江");
                datas.Add("{address}", "浙江省杭州市");
                datas.Add("{education}", "本科");
                datas.Add("{telephone}", "12345678");
                datas.Add("{cardno}", "123456789012345678");
                datas.Add("{info}", new String('a',500));//構造大數據字段
             
                foreach (var item in datas)
                {
                    app.Selection.Find.ClearFormatting();
                    app.Selection.Find.Text = item.Key;//需要查找的字符
                    app.Selection.Find.Execute(); //只負責找到匹配字符          
                    app.Selection.TypeText(item.Value);//在找到的字符區域寫數據
                }
                //另存word文檔
                doc.SaveAs(physicNewFile,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            }
            catch(Exception ex)
            {
               
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//關閉文檔
                }
                if (app != null)
                {
                    app.Quit();//退出應用程序
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM