zoukankan      html  css  js  c++  java
  • OpenXml修改word特定内容

    采用OpenXml来修改word特定内容,如下:

    word:

    OpenXml修改word之前:

    OpenXml修改word之后:

    代码:

     1  string path = @"C:UsersAdministratorDesktop新建文件夹 (2)1.docx";
     2             using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
     3             {
     4                 List<Text> textList = new List<Text>();
     5                 Body body = doc.MainDocumentPart.Document.Body;
     6                 foreach (Paragraph paragraph in body.Elements<Paragraph>())
     7                 {
     8                     if (paragraph.InnerText.Contains("河南郑州"))
     9                     {
    10                         foreach (Run run in paragraph.Elements<Run>())
    11                         {
    12                             textList.AddRange(run.Elements<Text>());
    13                         }
    14                         paragraph.Elements<Run>().FirstOrDefault().Append(new Text("欢迎你,河南郑州!"));
    15                     }
    16                 }
    17                 foreach (Table table in body.Elements<Table>())
    18                 {
    19                     foreach (TableRow tableRow in table.Elements<TableRow>())
    20                     {
    21                         foreach (TableCell tableCell in tableRow.Elements<TableCell>())
    22                         {
    23                             if (tableCell.InnerText.Contains("郑州河南"))
    24                             {
    25                                 foreach (Paragraph  paragraph in tableCell.Elements<Paragraph>())
    26                                 {
    27                                     foreach (Run run in paragraph.Elements<Run>())
    28                                     {
    29                                         textList.AddRange(run.Elements<Text>());
    30                                     }
    31                                 }
    32                                 tableCell.Elements<Paragraph>().FirstOrDefault().Elements<Run>().FirstOrDefault().Append(new Text("欢迎你,河南郑州!"));
    33                             }
    34                         }
    35                     }
    36                 }
    37                 foreach (var removeText in textList)
    38                 {
    39                     removeText.Remove();
    40                 }
    41             }
    View Code
  • 相关阅读:
    使用Vue快速开发单页应用
    轻松入门React和Webpack
    使用Redux管理你的React应用
    深入理解 react-router 路由系统
    webpack学习之路
    webpack编译流程漫谈
    HTML5无刷新修改Url,history pushState/replaceState
    gulp的流与执行顺序
    RequireJS对文件合并与压缩实现方法
    RequireJS模块化后JS压缩合并
  • 原文地址:https://www.cnblogs.com/HYJ0201/p/8146373.html
Copyright © 2011-2022 走看看