2011-10-06 67 views
18

我想以編程方式使用Microsoft.interop.Excel庫和C#獲取Excel表的最後一行。我想這樣做,因爲我負責循環查看Excel電子表格的所有記錄並對它們執行某種操作。具體來說,我需要最後一行的實際編號,因爲我會將此編號放入函數中。任何人有任何想法如何做到這一點?以編程方式獲取最後一個使用C填充excel行#

回答

39

夫婦方式,

using Excel = Microsoft.Office.Interop.Excel; 

Excel.ApplicationClass excel = new Excel.ApplicationClass(); 
Excel.Application app = excel.Application; 
Excel.Range all = app.get_Range("A1:H10", Type.Missing); 

OR

Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing); 
Excel.Range range = sheet.get_Range("A1", last); 

int lastUsedRow = last.Row; 
int lastUsedColumn = last.Column; 
+1

不知道第一個選項如何工作......但第二個選項看起來很有趣。我怎樣才能把它轉換成int值? – SoftwareSavant

+0

我編輯了我的代碼,我還沒有測試過這個代碼,但希望能給你一個你想要實現的想法。 – Priyank

+4

此答案爲您提供工作表UsedRange中的最後一個單元格。但問題是,UsedRange從不合同。如果你在A1和AA100中有一個值,那麼刪除AA100的內容,UsedRange仍然是A1:AA100。 AA100仍將被視爲最後一個單元格。 – CtrlDot

14

這是Excel中的一個常見問題。

下面是一些C#代碼:

// Find the last real row 
nInLastRow = oSheet.Cells.Find("*",System.Reflection.Missing.Value, 
System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious, false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row; 

// Find the last real column 
nInLastCol = oSheet.Cells.Find("*", System.Reflection.Missing.Value,  System.Reflection.Missing.Value,System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious, false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Column; 

here發現

,或者使用SpecialCells

Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing); 
Excel.Range range = sheet.get_Range("A1", last); 

[編輯]類似的線程:

+0

這是工作,我一直在努力獲得正確的行。 – NWessel

7

對於涉及Excel對象模型的問題,它往往更容易嘗試一下在VBA,然後再翻譯成C#是相當平凡的。

在這種情況下,一個做在VBA的方法是:

Worksheet.UsedRange.Row + Worksheet.UsedRange.Rows.Count - 1 
9

Pryank's answer是什麼工作對我來說最接近的一次。我在結尾添加了一點點(.Row),所以我不只是返回range,而是integer

int lastRow = wkSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; 
6

唯一的辦法我能得到它在所有情況下工作(除受保護的張):

它支持:

  • 掃描隱藏的行/列

  • 忽略無數據/公式的格式化單元格

代碼:

// Unhide All Cells and clear formats 
sheet.Columns.ClearFormats(); 
sheet.Rows.ClearFormats(); 

// Detect Last used Row - Ignore cells that contains formulas that result in blank values 
int lastRowIgnoreFormulas = sheet.Cells.Find(
       "*", 
       System.Reflection.Missing.Value, 
       InteropExcel.XlFindLookIn.xlValues, 
       InteropExcel.XlLookAt.xlWhole, 
       InteropExcel.XlSearchOrder.xlByRows, 
       InteropExcel.XlSearchDirection.xlPrevious, 
       false, 
       System.Reflection.Missing.Value, 
       System.Reflection.Missing.Value).Row; 
// Detect Last Used Column - Ignore cells that contains formulas that result in blank values 
int lastColIgnoreFormulas = sheet.Cells.Find(
       "*", 
       System.Reflection.Missing.Value, 
       System.Reflection.Missing.Value, 
       System.Reflection.Missing.Value, 
       InteropExcel.XlSearchOrder.xlByColumns, 
       InteropExcel.XlSearchDirection.xlPrevious, 
       false, 
       System.Reflection.Missing.Value, 
       System.Reflection.Missing.Value).Column; 

// Detect Last used Row/Column - Including cells that contains formulas that result in blank values 
int lastColIncludeFormulas = sheet.UsedRange.Columns.Count; 
int lastColIncludeFormulas = sheet.UsedRange.Rows.Count; 
+1

明智的答案(以及唯一爲我工作的)。 –

1

的ActiveSheet.UsedRange.Value返回[行,列]的2維對象陣列。檢查兩個維度的長度將提供LastRow索引和LastColumn索引。下面的例子使用C#。

 Excel.Worksheet activeSheet; 
 
\t \t Excel.Range activeRange; 
 

 

 
     public virtual object[,] RangeArray { 
 
\t \t \t get { return ActiveRange.Value; } 
 
\t \t } 
 

 
\t \t public virtual int ColumnCount { 
 
\t \t \t get { return RangeArray.GetLength(1); } 
 
\t \t } 
 

 

 
\t \t public virtual int RowCount { 
 
\t \t \t get { return RangeArray.GetLength(0); } 
 
\t \t } 
 

 
\t \t public virtual int LastRow { 
 
\t \t \t get { return RowCount; } 
 
\t \t }

0

這個問題時,有可能是空的細胞更是雪上加霜。但即使只填充了一個值,也必須讀取一行。當有很多未填充的單元格時,可能需要一段時間,但如果輸入接近正確,則速度非常快。

我的解決辦法忽略完全空行並返回最長列的行數:

private static int GetLastRow(Worksheet worksheet) 
    { 
     int lastUsedRow = 1; 
     Range range = worksheet.UsedRange; 
     for (int i = 1; i < range.Columns.Count; i++) 
     { 
      int lastRow = range.Rows.Count; 
      for (int j = range.Rows.Count; j > 0; j--) 
      { 
       if (lastUsedRow < lastRow) 
       { 
        lastRow = j; 
        if (!String.IsNullOrWhiteSpace(Convert.ToString((worksheet.Cells[j, i] as Range).Value))) 
        { 
         if (lastUsedRow < lastRow) 
          lastUsedRow = lastRow; 
         if (lastUsedRow == range.Rows.Count) 
          return lastUsedRow - 1; 
         break; 
        } 
       } 
       else 
        break; 
      } 
     } 
     return lastUsedRow; 
    } 
0

對於那些誰使用SpecialCells方法,(我不知道其他人),請注意,如果您的最後一個單元格是合併,您將無法使用Range.Row和Range.Column獲取最後一行和列號作爲數字獲取最後一行和一列。 你需要首先取消合併你的範圍,然後再次獲得最後一個單元格。 這花了我很多。

private int[] GetLastRowCol(Ex.Worksheet ws) 
    { 
     Ex.Range last = ws.Cells.SpecialCells(Ex.XlCellType.xlCellTypeLastCell, Type.Missing); 
     bool isMerged = (bool)last.MergeCells; 
     if (isMerged) 
     { 
      last.UnMerge(); 
      last = ws.Cells.SpecialCells(Ex.XlCellType.xlCellTypeLastCell, Type.Missing); 
     } 
     return new int[2] { last.Row, last.Column }; 
    } 
相關問題