C#操作word之文本批量替换
来源:互联网 发布:mybatis怎么打印sql 编辑:程序博客网 时间:2024/05/22 04:57
C#操作word之文本批量替换
首先引用office组件,电脑必须有安装office
using System;using System.Collections.Generic;using System.IO;using Word = Microsoft.Office.Interop.Word;
代码:
protected void Replace(string path) { string contractNO = string.IsNullOrEmpty(TextBoxcontractNO.Text.Trim()) ? " " : TextBoxcontractNO.Text.Trim(); string Firstparty = string.IsNullOrEmpty(TextBoxFirstparty.Text.Trim()) ? " " : TextBoxFirstparty.Text.Trim(); string FirstpartyIDcardNo = string.IsNullOrEmpty(TextBoxFirstpartyIDcardNo.Text.Trim()) ? " " : TextBoxFirstpartyIDcardNo.Text.Trim(); string FirstpartyTelephone = string.IsNullOrEmpty(TextBoxFirstpartyTelephone.Text.Trim()) ? " " : TextBoxFirstpartyTelephone.Text.Trim(); string secondparty = string.IsNullOrEmpty(TextBoxsecondparty.Text.Trim()) ? " " : TextBoxsecondparty.Text.Trim(); string secondpartyIDcardNo = string.IsNullOrEmpty(TextBoxsecondpartyIDcardNo.Text.Trim()) ? " " : TextBoxsecondpartyIDcardNo.Text.Trim(); string secondpartyTelephone = string.IsNullOrEmpty(TextBoxsecondpartyTelephone.Text.Trim()) ? " " : TextBoxsecondpartyTelephone.Text.Trim(); string Partyamargincapital = string.IsNullOrEmpty(TextBoxPartyamargincapital.Text.Trim()) ? " " : TextBoxPartyamargincapital.Text.Trim(); string Partyasecuritydeposit = string.IsNullOrEmpty(TextBoxPartyasecuritydeposit.Text.Trim()) ? " " : TextBoxPartyasecuritydeposit.Text.Trim(); string TheamountofcapitalinvestedinPartyB = string.IsNullOrEmpty(TextBoxTheamountofcapitalinvestedinPartyB.Text.Trim()) ? " " : TextBoxTheamountofcapitalinvestedinPartyB.Text.Trim(); string TheinvestmentamountofPartyBislowercase = string.IsNullOrEmpty(TextBoxTheinvestmentamountofPartyBislowercase.Text.Trim()) ? " " : TextBoxTheinvestmentamountofPartyBislowercase.Text.Trim(); string PartyBmargincapital = string.IsNullOrEmpty(TextBoxPartyBmargincapital.Text.Trim()) ? " " : TextBoxPartyBmargincapital.Text.Trim(); string MarginofPartyB = string.IsNullOrEmpty(TextBoxMarginofPartyB.Text.Trim()) ? " " : TextBoxMarginofPartyB.Text.Trim(); string fundaccount = string.IsNullOrEmpty(TextBoxfundaccount.Text.Trim()) ? " " : TextBoxfundaccount.Text.Trim(); string Profitratio = string.IsNullOrEmpty(TextBoxProfitratio.Text.Trim()) ? " " : TextBoxProfitratio.Text.Trim(); string Maximumcommitmentinvestment = string.IsNullOrEmpty(TextBoxMaximumcommitmentinvestment.Text.Trim()) ? " " : TextBoxMaximumcommitmentinvestment.Text.Trim(); string Maximumlossamount = string.IsNullOrEmpty(TextBoxMaximumlossamount.Text.Trim()) ? " " : TextBoxMaximumlossamount.Text.Trim(); string Percentageofcontractinvestment = string.IsNullOrEmpty(TextBoxPercentageofcontractinvestment.Text.Trim()) ? "" : TextBoxPercentageofcontractinvestment.Text.Trim(); string Incomepercentage = string.IsNullOrEmpty(TextBoxIncomepercentage.Text.Trim()) ? " " : TextBoxIncomepercentage.Text.Trim(); Word.Application app = null; Word.Document doc = null; //将要导出的新word文件名 string newFile = "牛倌儿网合伙投资理财三方借款合同 - 绑定数据.docx"; string physicNewFile = Server.MapPath(newFile); try { app = new Word.Application();//创建word应用程序 object fileName = Server.MapPath(path);//模板文件 //打开模板文件 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); //构造数据 Dictionary<string, string> datas = new Dictionary<string, string>(); datas.Add("contractNO", contractNO); datas.Add("Firstparty", Firstparty); datas.Add("FirstpartyIDcardNo", FirstpartyIDcardNo); datas.Add("FirstpartyTelephone", FirstpartyTelephone); datas.Add("secondparty", secondparty); datas.Add("secondpartyIDcardNo", secondpartyIDcardNo); datas.Add("secondpartyTelephone", secondpartyTelephone); datas.Add("Partyamargincapital", Partyamargincapital); datas.Add("Partyasecuritydeposit", Partyasecuritydeposit); datas.Add("TheamountofcapitalinvestedinPartyB", TheamountofcapitalinvestedinPartyB); datas.Add("TheinvestmentamountofPartyBislowercase", TheinvestmentamountofPartyBislowercase); datas.Add("PartyBmargincapital", PartyBmargincapital); datas.Add("MarginofPartyB", MarginofPartyB); datas.Add("fundaccount", fundaccount); datas.Add("Profitratio", Profitratio); datas.Add("Maximumcommitmentinvestment", Maximumcommitmentinvestment); datas.Add("Maximumlossamount", Maximumlossamount); datas.Add("Percentageofcontractinvestment", Percentageofcontractinvestment); datas.Add("Incomepercentage", Incomepercentage); object replace = Word.WdReplace.wdReplaceAll; foreach (var item in datas) { app.Selection.Find.Replacement.ClearFormatting(); app.Selection.Find.MatchWholeWord = true; app.Selection.Find.ClearFormatting(); 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); //准备导出word Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; Response.AddHeader("Content-Disposition", "attachment;filename=牛倌儿网合伙投资理财三方借款合同 - 绑定数据.docx"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.ContentType = "application/ms-word"; Response.End(); } catch (System.Threading.ThreadAbortException ex) { //这边为了捕获Response.End引起的异常 } catch (Exception ex) { } finally { if (doc != null) { doc.Close();//关闭word文档 } if (app != null) { app.Quit();//退出word应用程序 } //如果文件存在则输出到客户端 if (File.Exists(physicNewFile)) { Response.WriteFile(physicNewFile); } } } protected void bt_readreport_Click(object sender, EventArgs e) { string path =FileUploadSelectWord.PostedFile.FileName; if (string.IsNullOrEmpty(path)) { Response.Write("<script>alert('必须选择文档!')</script>"); } else { Replace(path); } }
注意,如果你的word中替换标记中有部分是相同字符的需要开启圈子匹配,例子中已经开启:
app.Selection.Find.MatchWholeWord = true;
这里的app是Word.Application()word应用程序的实例。
0 0
- C#操作word之文本批量替换
- C#操作word之文本替换
- C# 操作Word批量替换
- C#操作office之word文本替换,只能实现替换其中一个,可以替换超长文本
- 批量删除文本空行/批量替换文本-word¬epad++
- C# Word 查找替换操作
- C#替换Word中的文本内容
- word之八大文本替换技巧
- C# 在word中查找及替换文本
- 批量替换文本内容-PHP操作文件方法
- word 批量 文字替换图片
- word中批量替换字体
- 文本字符批量替换专家
- sed批量替换文本内容
- Python批量替换文本内容
- c#对word操作的查找替换功能
- c# word替换(Find.Excute方法)操作 去除空行方法
- c# word替换(Find.Excute方法)操作 去除空行方法
- Get请求携带数据量的各种限制及解决办法
- 如何从 Xcode 控制台输出 JavaScript 的 log?
- 深入NSTimer(iOS)
- OpenGL配置
- 实时编辑dom的代码,包含新的判断浏览器核心及版本的方法,留存参考
- C#操作word之文本批量替换
- 关于fresco的使用之--CacheKey获取本地缓存
- spark-submit 和 spark-shell 后面可跟的参数
- 解决 mac ssh空闲 连接断开问题
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 动态树
- [Linux]管道命令 ”|“
- 语义WEB服务
- php手册-关于true和false
- java查询优化