c# interop excel 范围查找第一个和最后一个填充值
c# interop excel range find first and last populated value
我有一个范围(一行),我想知道该范围内第一个和最后一个填充值的列位置。这就是我试图计算位置的方式。
var xlRange = (Excel.Range)currentSheet.Cells[10, 1].EntireRow; // 10th row.
long firstColumn= (long)xlRange.get_End(Excel.XlDirection.xlToRight).Column;
Sheet 看起来像这样:
firstColumn returns 8. 这是正确的。但我不知道如何获取最后一个填充单元格的列位置。
还有
long firstColumn= (long)xlRange.get_End(Excel.XlDirection.xlToRight).Column;
return 12 for firstColumn
when excel is setup as below:
我期待得到 1。
有指示吗?
您可以使用 Excel 提供的 SpecialCells 方法。
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-specialcells-method-excel
在您的情况下,您希望找到最后一个单元格:xlCellTypeLastCell
没有价值,即 Type.Missing.
Excel.Range lastCell = workSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range myRange = workSheet.get_Range("B1", lastCell);
lastCell 应给出其行值和列值。
希望这有帮助。
我有一个范围(一行),我想知道该范围内第一个和最后一个填充值的列位置。这就是我试图计算位置的方式。
var xlRange = (Excel.Range)currentSheet.Cells[10, 1].EntireRow; // 10th row.
long firstColumn= (long)xlRange.get_End(Excel.XlDirection.xlToRight).Column;
Sheet 看起来像这样:
firstColumn returns 8. 这是正确的。但我不知道如何获取最后一个填充单元格的列位置。
还有
long firstColumn= (long)xlRange.get_End(Excel.XlDirection.xlToRight).Column;
return 12 for firstColumn
when excel is setup as below:
我期待得到 1。
有指示吗?
您可以使用 Excel 提供的 SpecialCells 方法。 https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-specialcells-method-excel
在您的情况下,您希望找到最后一个单元格:xlCellTypeLastCell 没有价值,即 Type.Missing.
Excel.Range lastCell = workSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range myRange = workSheet.get_Range("B1", lastCell);
lastCell 应给出其行值和列值。
希望这有帮助。