问题描述
我想从第一列的单元格在一个表中。在获得异常的Foreach(在rng.Tables单元C [1] .Columns [1] .Cells)
,因为该表包含具有混合单元宽度列。
有例如:在第一行中,有4个细胞,并在第二行中,只有2细胞(2细胞合并在一起)
错误信息:无法访问此集合各列,因为该表已混合单元格宽度
文件沃达柯=打开的Word文档
的foreach(在oDoc.Paragraphs第P)
{
范围RNG = p.Range;
/ *
* /
的foreach(在rng.Tables小区c [1] .Columns [1] .Cells)
{
// ....
}
}
使用foreach循环在你的第二个循环,可以改为使用一个for循环,像这样遍历所有的细胞来代替:
的(INT R = 1;为r = rng.Tables [1] .Row.Count; R ++)
{
对于(INT C = 1; C< = rng.Tables [1] .Columns.Count; C ++)
{
尝试
{
电池单元= table.Cell(R,C);
//你想要的东西在这里与细胞
}
赶上(例外五)
{
如果(e.Message.Contains(集合的请求的成员不存在。))
{
//最有可能的合并单元格的一部分,所以跳过。
}
别人扔;
}
}
}
I am trying to get the cells from 1st column in a table. Getting exception in the "Foreach(Cells c in rng.Tables[1].Columns[1].Cells)
" because the table contains columns that have mixed cell widths.
for eg: in first row, there are 4 cells and in second row, there are only 2 cells (2 cells merged together)
Error Message: "Cannot access individual columns in this collection because the table has mixed cell widths."
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)
{
//....
}
}
Instead of using a foreach loop in your second loop, can you instead use a for loop like so to iterate over all cells:
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;
}
}
}
这篇关于如何访问列有不同的单元格宽度从MS Word中的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!