Programmatically getting the last filled excel row using C#

前端 未结 10 937
执念已碎
执念已碎 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;
    }
                    
                    
    

提交回复
热议问题