Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

I'm a little bit stuck here. I'm building somewhat of a data base with excel. Now I want the user to be able to add new data to it whenever necessary. For that I have created a program so far that gets specific data from excel files and writes those to a datable. This works just fine. Now I want to add the data of the datatable to my excel data base. But I always want the new data to be placed into the nexr free row. My code is not working correctly though. It over writes the headers of the excel file. Maybe I missunderstand the get_Range method, but right now I'm clueless.
This is the code I have so far:
C#
private void datatableToMasterfile(DataTable drToEx)
{
    string masterfileName = @"C:\Users\q371298\Desktop\Datenbank\Datenbank_Reifen_080415.xls";

    // open Masterfile
    Excel.Application xl = new Excel.Application();
    Excel.Workbook wb = xl.Workbooks.Open(masterfileName);
    Excel.Worksheet wsDatenBank = wb.Sheets.get_Item(1);

    //Check for last filled row
    Excel.Range dbRange = (Excel.Range)wsDatenBank.Cells[wsDatenBank.Rows.Count, 1];
    int lastRow = (int)dbRange.get_End(Excel.XlDirection.xlUp).Row+3; 
    int newRow = lastRow + 1; 
    
    //Create Array to hold the data of DataTable
    object[,] arr = new object[drToEx.Rows.Count, drToEx.Columns.Count];

    //Fill DataTable in Array
    for (int r = 0; r < drToEx.Rows.Count; r++)
    {
        DataRow dr1 = drToEx.Rows[r];
        for (int c =0; c < drToEx.Columns.Count; c++)
        {
            arr[r, c] = dr1[c];
        }
    }

    //Set Excel Range to paste the data
    Excel.Range startCell = (Excel.Range)wsDatenBank.Cells[newRow, 1];
    Excel.Range endCell = (Excel.Range)wsDatenBank.Cells[1 + drToEx.Rows.Count - 1, drToEx.Columns.Count];
    Excel.Range range = wsDatenBank.get_Range(startCell, endCell);
   
    //Fill array in Excel
    range.Value = arr;
    
    xl.Visible = true;    
}

I would really appreciate the help, as I am still fairly new to this.

Thanx a lot!

Susie
Posted
Updated 17-May-15 23:43pm
v2
Comments
Richard MacCutchan 18-May-15 5:54am    
The code for getting lastRow above does not look quite correct. I suggest stepping through your code with the debugger to see exactly what values you get at each step.
Susie Cantu 18-May-15 6:07am    
Thank you for your input. I did what you suppested and the values are correct. The problem is, that in my test I just added one row of data and I need it to be written in my excel spreadsheet row no. 5, last row 4; and new row = 5. That should be correct. But the one row I want to add is written in the excel Spread sheet from row 1 to row 5. The same data in each row. I hope I explained it ok ;-)

1 solution

I think you miss this one:

Quote:

Range Line = (Range)worksheet.Rows[newRow];
Line.Insert();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900