我正在尝试从表格的第一列获取单元格。在“中获取异常Foreach(Cells c in rng.Tables[1].Columns[1].Cells)
" 因为该表包含具有混合单元格宽度的列。
例如:第一行有 4 个单元格,第二行只有 2 个单元格(2 个单元格合并在一起)
错误信息: ”无法访问此集合中的各个列,因为该表具有混合的单元格宽度。"
Document oDoc = open word document
foreach (Paragraph p in oDoc.Paragraphs)
{
Range rng = p.Range;
/*
*/
foreach (Cell c in rng.Tables[1].Columns[1].Cells)
{
//....
}
}
您可以使用像这样的 for 循环来迭代所有单元格,而不是在第二个循环中使用 foreach 循环:
for (int r = 1; r <= rng.Tables[1].Row.Count; r++)
{
for (int c = 1; c <= rng.Tables[1].Columns.Count; c++)
{
try
{
Cell cell = table.Cell(r, c);
//Do what you want here with the cell
}
catch (Exception e)
{
if (e.Message.Contains("The requested member of the collection does not exist."))
{
//Most likely a part of a merged cell, so skip over.
}
else throw;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)