註意事項 上一篇已經說明,這次就不一一說了,直接來正文; word內容 相關代碼 方法1 1 static void Main(string[] args) 2 { 3 string wordPathStr = @"C:\Users\user\Desktop\新建文件夾 (2)\openxml讀取表 ...
註意事項
上一篇已經說明,這次就不一一說了,直接來正文;
word內容
相關代碼
方法1
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<Table>()) 8 { 9 foreach (var tableRow in table.Elements<TableRow>()) 10 { 11 foreach (var tableCell in tableRow.Elements<TableCell>()) 12 { 13 Console.Write(tableCell.InnerText); 14 } 15 } 16 } 17 } 18 19 Console.ReadKey(); 20 }View Code
或
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<OpenXmlElement>(); 8 foreach (var table in body.Elements<Table>()) 9 { 10 foreach (var tableRow in table.Elements<TableRow>()) 11 { 12 Console.Write(tableRow.InnerText); 13 } 14 } 15 } 16 Console.ReadKey(); 17 }View Code
或
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<OpenXmlElement>(); 8 foreach (var table in body.Elements<Table>()) 9 { 10 Console.Write(table.InnerText); 11 } 12 } 13 Console.ReadKey(); 14 }View Code
方法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 var tableCellList = body.Elements<OpenXmlElement>(); 8 foreach (var inst in tableCellList) 9 { 10 Console.Write(inst.InnerText); 11 } 12 } 13 14 Console.ReadKey(); 15 }View Code
註:方法1和方法2使用場景,以後慢慢來介紹;