经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 替换Word文本—— 用文档、图片、表格替换文本 - E-iceblue
来源:cnblogs  作者:E-iceblue  时间:2018/11/29 9:32:07  对本文有异议

编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文档、图片或者表格来替换文档中的指定文本字符串。示例要点如下:

1. 用文档替换Word中的文本

2. 用图片替换Word中的文本

3. 用表格替换Word中的文本

 

工具

下载安装后,注意在程序中添加引用Spire.Doc.dll(如下图),dll文件可在安装路径下的Bin文件夹中获取。

C#代码示例

【示例1】用文档替换Word中的文本

测试文档:

步骤1:加载文档

  1. //加载源文档
  2. Document document = new Document("Original.docx");
  3. //加载用于替换的文档
  4. IDocument replaceDocument = new Document("test.docx");

步骤2:用文档替换文本

  1. document.Replace("History", replaceDocument, false, true);

步骤3:保存文档

  1. document.SaveToFile("result.docx", FileFormat.Docx2013);

替换结果:

 

全部代码:

  1. using Spire.Doc;
  2. using Spire.Doc.Interface;
  3. namespace ReplaceTextWithDocument_Doc
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //加载源文档
  10. Document document = new Document("Original.docx");
  11. //加载用于替换的文档
  12. IDocument replaceDocument = new Document("test.docx");
  13. //用文档替换源文档中的指定文本
  14. document.Replace("History", replaceDocument, false, true);
  15. //保存文档
  16. document.SaveToFile("result.docx", FileFormat.Docx2013);
  17. System.Diagnostics.Process.Start("result.docx");
  18. }
  19. }
  20. }
View Code

 

 

【示例2】用图片替换Word中的文本

测试文档:

步骤1:加载文件

  1. //实例化Document类的对象,并加载测试文档
  2. Document doc = new Document();
  3. doc.LoadFromFile("testfile.docx");
  4. //加载替换的图片
  5. Image image = Image.FromFile("g.png");

步骤2:查找需要替换掉的文本字符串

  1. //获取第一个section
  2. Section sec= doc.Sections[0];
  3. //查找文档中的指定文本内容
  4. TextSelection[] selections = doc.FindAllString("Google", true, true);
  5. int index = 0;
  6. TextRange range = null;

步骤3:用图片替换文本

  1. //遍历文档,移除文本内容,插入图片
  2. foreach (TextSelection selection in selections)
  3. {
  4. DocPicture pic = new DocPicture(doc);
  5. pic.LoadImage(image);
  6. range = selection.GetAsOneRange();
  7. index = range.OwnerParagraph.ChildObjects.IndexOf(range);
  8. range.OwnerParagraph.ChildObjects.Insert(index, pic);
  9. range.OwnerParagraph.ChildObjects.Remove(range);
  10. }

步骤4:保存文档

  1. doc.SaveToFile("result.docx", FileFormat.Docx);

替换结果:

全部代码:

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;
  4. using System.Drawing;
  5. namespace ReplaceTextWithImg_Doc
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. //实例化Document类的对象,并加载测试文档
  12. Document doc = new Document();
  13. doc.LoadFromFile("testfile.docx");
  14. //加载替换的图片
  15. Image image = Image.FromFile("g.png");
  16. //获取第一个section
  17. Section sec= doc.Sections[0];
  18. //查找文档中的指定文本内容
  19. TextSelection[] selections = doc.FindAllString("Google", true, true);
  20. int index = 0;
  21. TextRange range = null;
  22. //遍历文档,移除文本内容,插入图片
  23. foreach (TextSelection selection in selections)
  24. {
  25. DocPicture pic = new DocPicture(doc);
  26. pic.LoadImage(image);
  27. range = selection.GetAsOneRange();
  28. index = range.OwnerParagraph.ChildObjects.IndexOf(range);
  29. range.OwnerParagraph.ChildObjects.Insert(index, pic);
  30. range.OwnerParagraph.ChildObjects.Remove(range);
  31. }
  32. //保存文档
  33. doc.SaveToFile("result.docx", FileFormat.Docx);
  34. System.Diagnostics.Process.Start("result.docx");
  35. }
  36. }
  37. }
View Code

 

【示例3】用表格替换Word中的文本

测试文档:

 

步骤1:加载文档

  1. Document doc = new Document();
  2. doc.LoadFromFile("test.docx");

步骤2:查找关键字符串

  1. Section section = doc.Sections[0];
  2. TextSelection selection = doc.FindString("参考附录", true, true);

步骤3:获取关键字符串所在段落

  1. TextRange range = selection.GetAsOneRange();
  2. Paragraph paragraph = range.OwnerParagraph;
  3. Body body = paragraph.OwnerTextBody;
  4. int index = body.ChildObjects.IndexOf(paragraph);

步骤4:添加表格

  1. Table table = section.AddTable(true);
  2. table.ResetCells(2, 3);
  3. range = table[0, 0].AddParagraph().AppendText("管号(McFarland)");
  4. range = table[0, 1].AddParagraph().AppendText("0.5");
  5. range = table[0, 2].AddParagraph().AppendText("1");
  6. range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)");
  7. range = table[1, 1].AddParagraph().AppendText("0.2");
  8. range = table[1, 2].AddParagraph().AppendText("0.4");

步骤5:移除段落,插入表格

  1. body.ChildObjects.Remove(paragraph);
  2. body.ChildObjects.Insert(index, table);

步骤6:保存文档

  1. doc.SaveToFile("result.doc", FileFormat.Doc);

替换结果:

全部代码:

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;
  4. namespace ReplaceTextWithTable_Doc
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. //实例化Document类的对象,并加载测试文档
  11. Document doc = new Document();
  12. doc.LoadFromFile("test.docx");
  13. //查找关键字符串文本
  14. Section section = doc.Sections[0];
  15. TextSelection selection = doc.FindString("参考附录", true, true);
  16. //获取关键字符串所在的段落
  17. TextRange range = selection.GetAsOneRange();
  18. Paragraph paragraph = range.OwnerParagraph;
  19. Body body = paragraph.OwnerTextBody;
  20. int index = body.ChildObjects.IndexOf(paragraph);
  21. //添加一个两行三列的表格
  22. Table table = section.AddTable(true);
  23. table.ResetCells(2, 3);
  24. range = table[0, 0].AddParagraph().AppendText("管号(McFarland)");
  25. range = table[0, 1].AddParagraph().AppendText("0.5");
  26. range = table[0, 2].AddParagraph().AppendText("1");
  27. range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)");
  28. range = table[1, 1].AddParagraph().AppendText("0.2");
  29. range = table[1, 2].AddParagraph().AppendText("0.4");
  30. //移除段落,插入表格
  31. body.ChildObjects.Remove(paragraph);
  32. body.ChildObjects.Insert(index, table);
  33. //保存文档
  34. doc.SaveToFile("result.doc", FileFormat.Doc);
  35. System.Diagnostics.Process.Start("result.doc");
  36. }
  37. }
  38. }
View Code

 

以上是本次关于“C# 用文档、图片、表格替换Word中的文本字符串的”的全部内容。

(本文完)

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号