Home » excel » excel – C# Open Exel Template, copy row Formatting and insert new data into each new row

excel – C# Open Exel Template, copy row Formatting and insert new data into each new row

Posted by: admin May 14, 2020 Leave a comment

Questions:

I wish to loop through all the Files in a folder and add their Filename into a row in Excel. To do this, I have a Template Excel file I wish to use and start adding data at a specific row which contains formulas.

I use the below data and I am able to loop through the files but I cannot copy the row and insert another before pasting the data as I get an error

    string rootfolder = Path.GetDirectoryName(path);
    string folder = Path.GetFileName(Path.GetDirectoryName(path));

    var excelApp = new Application();
    var workbook = excelApp.Workbooks.Open(fileName);
    var mWorkSheets = workbook.Worksheets;
    var worksheet = (Worksheet)mWorkSheets.get_Item("Invoice");

    excelApp.Visible = true;

    int x = 13;  // <- The row to copy which has formulas
        Range RngToCopy = worksheet.get_Range("b13" , "l13").EntireRow;
        foreach (var item in files)
        {

            Range RngToInsert = worksheet.get_Range("b" + x, Type.Missing).EntireRow;
            RngToInsert.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, RngToInsert.Copy(Type.Missing));  // --< Throws the error

            worksheet.Cells[x, 2].Value2 = item.date;
            worksheet.Cells[x, 3].Value2 = item.reference;
            worksheet.Cells[x, 4].Value2 = item.source;
            worksheet.Cells[x, 5].Value2 = item.count;
            worksheet.Cells[x, 6].Value2 = item.costPerWord;
            worksheet.Cells[x, 7].Value2 = item.STBY;
            worksheet.Cells[x, 8].Value2 = item.costSTBY;
            worksheet.Cells[x, 9].Value2 = item.SCH;
            worksheet.Cells[x, 10].Value2 = item.costSCH;

            x++;
        }

The Error

//System.Runtime.InteropServices.COMException: ‘This won’t work
because it would move cells in a table on your worksheet.’

How to&Answers:

I guess that depends on your Template.
The error message appears when you try to edit a formatted table or linked table (odbc for example) in Excel.
Can you try your code with a blank excel sheet to see if thats working if so its a problem with your template itself and not the code.

The code itself looks fine

Answer:

I ended up simply adding a formula directly into the cell

    int x = 13;

    Range line = (Range)worksheet.Rows[13].EntireRow;
    foreach (var item in files)
    {

        worksheet.Cells[x, 2].Value2 = item.date;
        worksheet.Cells[x, 3].Value2 = item.reference;
        worksheet.Cells[x, 4].Value2 = item.source;
        worksheet.Cells[x, 5].Value2 = item.count;
        worksheet.Cells[x, 6].Value2 = item.costPerWord;
        worksheet.Cells[x, 7].Value2 = item.STBY;
        worksheet.Cells[x, 8].Value2 = item.costSTBY;
        worksheet.Cells[x, 9].Value2 = item.SCH;
        worksheet.Cells[x, 10].Value2 = item.costSCH;

        worksheet.Cells[x, 11].Formula = string.Format("=SUM(F{0}+H{0}+J{0})", x);