OpenXml读取word内容(二)

注意事项

上一篇已经说明,这次就不一一说了,直接来正文;

word内容

OpenXml读取word内容(二)_第1张图片

相关代码

方法1

OpenXml读取word内容(二)_第2张图片

 1  static void Main(string[] args)
 2         {
 3             string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
 4             using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
 5             {
 6                 Body body = doc.MainDocumentPart.Document.Body;
 7                 foreach (var table in body.Elements())
 8                {
 9foreach (var tableRow in table.Elements())
10                     {
11                         foreach (var tableCell in tableRow.Elements())
12                         {
13                             Console.Write(tableCell.InnerText);
14                         }
15                     }
16                 }
17             }
18 
19             Console.ReadKey();
20         }

View Code

OpenXml读取word内容(二)_第3张图片

 1  static void Main(string[] args)
 2         {
 3             string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
 4             using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
 5             {
 6                 Body body = doc.MainDocumentPart.Document.Body;
 7                 var tableCellList=body.Elements();
 8                 foreach (var table in body.Elements
()) 9 { 10foreach (var tableRow in table.Elements()) 11 { 12 Console.Write(tableRow.InnerText); 13 } 14 } 15 } 16 Console.ReadKey(); 17 } View Code

OpenXml读取word内容(二)_第4张图片

 1  static void Main(string[] args)
 2         {
 3             string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
 4             using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
 5             {
 6                 Body body = doc.MainDocumentPart.Document.Body;
 7                 var tableCellList=body.Elements();
 8                 foreach (var table in body.Elements
()) 9 { 10 Console.Write(table.InnerText); 11 } 12 } 13 Console.ReadKey(); 14 }View Code

 

方法2

OpenXml读取word内容(二)_第5张图片

 1     static void Main(string[] args)
 2         {
 3             string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
 4             using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
 5             {
 6                 Body body = doc.MainDocumentPart.Document.Body;
 7                 var tableCellList = body.Elements();
 8                 foreach (var inst in tableCellList)
 9                 {
10                     Console.Write(inst.InnerText);
11                 }
12             }
13 
14             Console.ReadKey();
15         }
View Code

注:方法1和方法2使用场景,以后慢慢来介绍;

控制台显示

你可能感兴趣的:(OpenXml读取word内容(二))