获取 Word 文档的 rowspan 和 cospan table
Get rowspan and cospan of Word document table
我正在尝试获取 wordprocessingML 中 table 的 colspan 和 rowspan 值。
谁能解释一下我如何使用 openXML 获取这两个值?
终于找到解决办法了,
private static int CalcColspan(XElement cell)
{
if (cell.Name != W.tc)
return 0;
return cell.Element(W.tcPr).Element(W.gridSpan) == null ? 1 :
Convert.ToInt32(cell.Element(W.tcPr).Element(W.gridSpan).Attribute(W.val).Value);
}
private static int CalcRowspan(XElement cell)
{
if (cell.Name != W.tc)
return 0;
int rowspan = 1, colNum = 0;
XElement currentRow = cell.Parent;
foreach (XElement tc in cell.NodesBeforeSelf())
{
colNum += CalcColspan(tc);
}
bool endOfSpan = false;
foreach (XElement row in currentRow.NodesAfterSelf())
{
int currentColNum = 0;
foreach (XElement tc in row.Nodes())
{
if (tc.Name != W.tc)
continue;
if (currentColNum == colNum)
{
if (tc.Element(W.tcPr).Element(W.vMerge) != null)
{
if ((string)tc.Element(W.tcPr).Element(W.vMerge).Attribute(W.val) != "restart")
{
rowspan++;
break;
}
}
endOfSpan = true;
}
currentColNum += CalcColspan(tc);
}
if (endOfSpan)
break;
}
return rowspan;
}
我正在尝试获取 wordprocessingML 中 table 的 colspan 和 rowspan 值。
谁能解释一下我如何使用 openXML 获取这两个值?
终于找到解决办法了,
private static int CalcColspan(XElement cell)
{
if (cell.Name != W.tc)
return 0;
return cell.Element(W.tcPr).Element(W.gridSpan) == null ? 1 :
Convert.ToInt32(cell.Element(W.tcPr).Element(W.gridSpan).Attribute(W.val).Value);
}
private static int CalcRowspan(XElement cell)
{
if (cell.Name != W.tc)
return 0;
int rowspan = 1, colNum = 0;
XElement currentRow = cell.Parent;
foreach (XElement tc in cell.NodesBeforeSelf())
{
colNum += CalcColspan(tc);
}
bool endOfSpan = false;
foreach (XElement row in currentRow.NodesAfterSelf())
{
int currentColNum = 0;
foreach (XElement tc in row.Nodes())
{
if (tc.Name != W.tc)
continue;
if (currentColNum == colNum)
{
if (tc.Element(W.tcPr).Element(W.vMerge) != null)
{
if ((string)tc.Element(W.tcPr).Element(W.vMerge).Attribute(W.val) != "restart")
{
rowspan++;
break;
}
}
endOfSpan = true;
}
currentColNum += CalcColspan(tc);
}
if (endOfSpan)
break;
}
return rowspan;
}