2014-09-24 54 views
1

使用Microsoft的Excel Interop我正在閱讀電子表格。我沒有問題得到整個工作表的使用範圍,但我只想得到用於一列的行數。這是我的片材的一個示例:僅在Excel Interop中爲一列獲取UsedRange

enter image description here

正如你可以看到,對於列A所使用的區域是隻有3行,而對於列B是5行。當我用我現在的代碼:

int rows = sheet.UsedRange.Rows.Count; 

..它總是返回「5」。我如何指定我只想要一個單元格的使用範圍?

回答

0

Excel中的UsedRange適用於工作表而不是單個列。見here。 可以使用這樣的事情讓最後一排的一個特定列:

With yourExcelSheet 
    lrow = .Cells(.Rows.Count, lrCol).End(Excel.XlDirection.xlUp).Row 
End With 

其中「yourExcelSheet」是工作,需要申報/分配,「lrow」是您所需要的最後一排和「lrcol '是列號(A = 1,B = 2等)

Apols,但這個片段是從vb.net/vba派生的,而不是c#,所以你需要處理任何需要的轉換;但認爲它可能有幫助。

0

要獲得在A列中使用的細胞可以試試這個方法

// Get the last cell used in the Column A 
var lastCell = sheet.Range["A:A"].SpecialCells(
    Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell 
) 
    // get the lastColumn used 
    int lastcolumn = lastCell.column; 

    // get the lastRow used 
    int lastRow = lastCell.Row; 
+0

這也給UsedRange的最後一行值。 @內存 – Gokul 2017-06-19 06:33:52