如何访问excel文件的当前行索引?

时间:2012-01-19 12:33:35

标签: c# c#-4.0

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

我想将“cellno”初始化为excel文件的当前行索引,以便不会覆盖数据。是否有任何具体的功能来填充行数?

2 个答案:

答案 0 :(得分:4)

您可以使用

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

答案 1 :(得分:0)

您还可以使用UsedRange属性在工作表中获取已使用的范围。