Programmatically getting the last filled excel row using C#

前端 未结 10 921
执念已碎
执念已碎 2020-11-27 16:52

I am trying to get the last row of an excel sheet programatically using the Microsoft.interop.Excel Library and C#. I want to do that, because I am charged with looping thr

10条回答
  •  北海茫月
    2020-11-27 17:25

    This is a common issue in Excel.

    Here is some C# code:

    // 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;
    

    found here

    or using SpecialCells

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

    [EDIT] Similar threads:

    • VB.NET - Reading ENTIRE content of an excel file
    • How to get the range of occupied cells in excel sheet

提交回复
热议问题