获取Word文档表中的最大行数和列数。
问题描述:
各位大家好!,
我很擅长使用这个库,我正在开发一个工具来从Word文档中的表中提取信息。
我想知道是否有任何方法可以获得表格的最大行数和列数。
I am quite new using this library and I am developing a tool to extract information from tables in a Word document.
I would like to know if is there any way to get the maximum number of rows and columns of the table.
非常感谢您的帮助。
答
嗨Chema,
Hi Chema,
根据你的描述,我认为你想要的是得到一个表的行/列数,对吗?
你可以通过Elements< TableRow>()得到行计数.Count()和通过Elements< GridColumn>()获取列.Count()
According to your describtion,i think what you want is getting row/column count of the a table,right?
You could get row count by Elements<TableRow>().Count() and get column by Elements<GridColumn>().Count()
这是一个例子。
Here is an example.
using (WordprocessingDocument doc =
WordprocessingDocument.Open(filepath, true))
{
// Find the first table in the document.
Table table =
doc.MainDocumentPart.Document.Body.Elements<Table>().First();
// Find the second row in the table.
int row = table.Elements<TableRow>().Count();
int col = table.Elements<TableGrid>().First().Elements<GridColumn>().Count();
MessageBox.Show("RowCount:" + row + "\r" +
"ColmunCount:" + col);
}
最好的问候,
Edward