none
C#: Copy last row of an excel to another RRS feed

  • Question

  • Is it possible to copy last row in from one excel file  to another. I don't how is it possible. If anyone have any sample or demo file please if you can share. 

    I would appreciate your help.

    Thanks

    Monday, March 14, 2016 8:34 PM

Answers

All replies

  • Hi Jazz,

    Based on your description, your case more related to Excel for developers, I will move your case to that forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 15, 2016 7:36 AM
  • Hi,

    According to your description, I build a C# solution and it works, check the following codes:

    //Load the source excel file
    Workbook workbook = new Workbook();
    workbook.LoadFromFile("Sample.xlsx");
    Worksheet worksheet = workbook.Worksheets[0];
    
    //create a new excel file
    Workbook workbook2 = new Workbook();
    Worksheet worksheet2 = workbook2.Worksheets[0];
    
    //Copy the last row of the source excel to the new excel file
    int i = 1;
    int colunmCount = worksheet.Columns.Count();
    foreach(CellRange range in worksheet.Columns[0])
    {
        CellRange sourceRange = worksheet.Range[worksheet.LastRow, 1, worksheet.LastRow, colunmCount];
        CellRange destRange = worksheet2.Range[i,1,i,colunmCount];
        worksheet.Copy(sourceRange,destRange,true);
    }
    
    //Save and launch the file
    workbook2.SaveToFile("NewFile.xlsx",ExcelVersion.Version2010);
    System.Diagnostics.Process.Start("NewFile.xlsx");

    But in this solution, I used a free excel tool, if you do not mind, you can have a try.

    Hope my code helps.

    Regards,

    Leon

    Tuesday, March 15, 2016 10:06 AM
  • My Excel works like a database. And my front end works is in C#. So question should not be under "Excel for developers."

    • Edited by jazz1110 Tuesday, March 15, 2016 9:33 PM
    Tuesday, March 15, 2016 9:33 PM
  • >>>My Excel works like a database. And my front end works is in C#. So question should not be under "Excel for developers."

    According to your description, please correct me if I have any misunderstandings on your question. It seems that you want to use ADO.NET to Retrieve and Modify Records in an Excel Workbook, if so, I suggest that you could refer to

    How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET

    or

    How to query and display excel data by using ASP.NET, ADO.NET, and Visual C# .NET

    • Marked as answer by David_JunFeng Thursday, March 24, 2016 8:46 AM
    Friday, March 18, 2016 6:13 AM