[英]Get UsedRange for one column only in Excel Interop
使用Microsoft的Excel Interop,我正在閱讀電子表格。 我沒有得到整個工作表的使用范圍的問題,但是我只想獲取一列使用的行數。 這是我的工作表示例:
如您所見,列A的使用范圍僅為3行,而列B的使用范圍為5行。 當我使用當前代碼時:
int rows = sheet.UsedRange.Rows.Count;
..它總是返回“ 5”。 如何指定我只想要一個單元格的使用范圍?
Excel中的UsedRange適用於工作表,而不適用於各個列。 看這里 。 您可以使用以下方法獲取特定列的最后一行:
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#派生的,因此您將需要處理任何必需的轉換。 但認為這可能會有所幫助。
要獲取列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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.