Programmatically getting the last filled excel row using C#

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

    As previously discussed, the techniques above (xlCellTypeLastCell etc.) do not always provide expected results. Although it's not difficult to iterate down through a column checking for values, sometimes you may find that there are empty cells or rows with data that you want to consider in subsequent rows. When using Excel directly, a good way of finding the last row is to press CTRL + Down Arrow a couple of times (you'll end up at row 1048576 for an XLSX worksheet) and then press CTRL + Up Arrow which will select the last populated cell. If you do this within Excel while recording a Macro you'll get the code to replicate this, and then it's just a case of tweaking it for C# using the Microsoft.Office.Interop.Excel libraries. For example:

        private int GetLastRow()
        {
            Excel.Application ExcelApp;
            ExcelApp = new Excel.Application();
    
            ExcelApp.Selection.End(Excel.XlDirection.xlDown).Select();
            ExcelApp.Selection.End(Excel.XlDirection.xlDown).Select();
            ExcelApp.Selection.End(Excel.XlDirection.xlDown).Select();
    
            ExcelApp.Selection.End(Excel.XlDirection.xlUp).Select();
    
            return ExcelApp.ActiveCell.Row;
        }
    

    It may not be the most elegant solution (I guess instead you could navigate to the final row within the spreadsheet first directly before using XlUp) but it seems to be more reliable.

    0 讨论(0)
  • 2020-11-27 17:49

    The ActiveSheet.UsedRange.Value returns a 2 dimensional object array of [row, column]. Checking the length of both dimensions will provide the LastRow index and the LastColumn index. The example below is using C#.

    Excel.Worksheet activeSheet;
    Excel.Range activeRange;
    
    public virtual object[,] RangeArray 
    {
        get { return ActiveRange.Value; }
    }
    
    public virtual int ColumnCount 
    {
        get { return RangeArray.GetLength(1); }
    }
    
    public virtual int RowCount
    {
        get { return RangeArray.GetLength(0); }
    }
    
    public virtual int LastRow 
    {
        get { return RowCount; }
    }
    
    0 讨论(0)
  • 2020-11-27 17:51

    Pryank's answer is what worked closest for me. I added a little bit towards the end (.Row) so I am not just returning a range, but an integer.

    int lastRow = wkSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;
    
    0 讨论(0)
  • 2020-11-27 17:51

    This issue is even worse when there are possibly empty cells. But you have to read a row even if only one value is filled. It can take a while when there are a lot of unfilled cells but if the input is close to correct it is rather fast.

    My solution ignores completely empty rows and returns the longest column's row count:

    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 讨论(0)
提交回复
热议问题