注意事项

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

word内容

OpenXml读取word内容(二)-LMLPHP

相关代码

方法1

OpenXml读取word内容(二)-LMLPHP

  static void Main(string[] args)
{
string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
{
Body body = doc.MainDocumentPart.Document.Body;
foreach (var table in body.Elements<Table>())
{
foreach (var tableRow in table.Elements<TableRow>())
{
foreach (var tableCell in tableRow.Elements<TableCell>())
{
Console.Write(tableCell.InnerText);
}
}
}
} Console.ReadKey();
}

OpenXml读取word内容(二)-LMLPHP

  static void Main(string[] args)
{
string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
{
Body body = doc.MainDocumentPart.Document.Body;
var tableCellList=body.Elements<OpenXmlElement>();
foreach (var table in body.Elements<Table>())
{
foreach (var tableRow in table.Elements<TableRow>())
{
Console.Write(tableRow.InnerText);
}
}
}
Console.ReadKey();
}

OpenXml读取word内容(二)-LMLPHP

  static void Main(string[] args)
{
string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
{
Body body = doc.MainDocumentPart.Document.Body;
var tableCellList=body.Elements<OpenXmlElement>();
foreach (var table in body.Elements<Table>())
{
Console.Write(table.InnerText);
}
}
Console.ReadKey();
}

方法2

OpenXml读取word内容(二)-LMLPHP

     static void Main(string[] args)
{
string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\openxml读取表格内容.docx";
using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
{
Body body = doc.MainDocumentPart.Document.Body;
var tableCellList = body.Elements<OpenXmlElement>();
foreach (var inst in tableCellList)
{
Console.Write(inst.InnerText);
}
} Console.ReadKey();
}

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

控制台显示

OpenXml读取word内容(二)-LMLPHP

05-11 11:30