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; }
lastRow
Quote: Range Line = (Range)worksheet.Rows[newRow]; Line.Insert();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)