NIlesh Lanke
NIlesh Lanke

Reputation: 1243

How to access current row index of excel file?

public bool StoreInExecel(String name,String csFIleName)
{
    int cellno = ;//Initialize to current row no
    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
    Worksheet worksheet;

    // Opening excel file
    workbook = excelApp.Workbooks.Open(fileName, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

    // Get first Worksheet
    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1);

    // Setting cell values

    ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[cellno,"B"]).Value2 = "5";
    ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[cellno,"B"]).Value2 = "7";

    workbook.Save();
    workbook.Close(0, 0, 0);
    //excelApp.Quit();   
    return true;
}

I want to initialize "cellno" to current row index of excel file so that data should not get overridden. Is there any specific function to get filled row count?

Upvotes: 3

Views: 7747

Answers (2)

Adrian Fâciu
Adrian Fâciu

Reputation: 12552

You can also get the used range in a worksheet using the UsedRange property.

Upvotes: 0

Marco
Marco

Reputation: 57573

You can use

Excel.Range range = (Excel.Range)excelApp.ActiveCell;
int cellno = range.Row;

Upvotes: 5

Related Questions