Programmatically getting the last filled excel row using C#

前端 未结 10 893
执念已碎
执念已碎 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:24

    As CtrlDot and Leo Guardian says, it is not very acuarate the method, there some files where formats affect the "SpecialCells".

    So I used a combination of that plus a While.

    Range last = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
    Range range = sheet.get_Range("A1", last);
    int lastrow = last.Row;
    // Complement to confirm that the last row is the last
    string textCell= "Existe";
    while (textCell != null)
    {
     lastrow++;
     textCell = sheet.Cells[lastrow + 1, 1].Value;
    }
                    
                    
    
    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-11-27 17:31

    Couple ways,

    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;
    
    0 讨论(0)
  • 2020-11-27 17:33

    For those who use SpecialCells method, (I'm not sure about others), Please Note in case your last cell is merged, you won't be able to get last row and column number using Range.Row and Range.Column to get the last row and column as numbers. you need to first Unmerge your range and then Again get the last cell. It cost me a lot.

    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 };
        }
    
    0 讨论(0)
  • 2020-11-27 17:47

    The only way I could get it to work in ALL scenarios (except Protected sheets):

    It supports:

    • Scanning Hidden Row / Columns

    • Ignores formatted cells with no data / formula

    Code:

    // 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;
    
    0 讨论(0)
  • 2020-11-27 17:48

    For questions involving the Excel object model, it's often easier to try it out in VBA first, then translating to C# is fairly trivial.

    In this case one way to do it in VBA is:

    Worksheet.UsedRange.Row + Worksheet.UsedRange.Rows.Count - 1
    
    0 讨论(0)
提交回复
热议问题