Search code examples
c#excelinfragistics

How to add a new excel column between two columns in a existing worksheet


I would like to add a column that already contains cells values between two columns (or at the end) of a worksheet of an existing workbook that I load.

So I have a function that sets that "column values" I need :

private static Workbook SetIndicatorsWorkbook()
        {
            var workbook = new Workbook(WorkbookFormat.Excel2007MacroEnabled);
            var worksheet = workbook.Worksheets.Add("Unit & Integration Tests");

//Don't worry about team and jenkinsBuilTeams variables
        foreach (var team in jenkinsBuildTeams)
        {

            worksheet.Rows[posX].Cells[0].Value = lastnbUnitTests + lastnbIntegrationTests;
            posX += 1;
        }

        return workbook;
    }

And then in main function I want to add this column (which is workbook.worksheets[0].Columns[0] ) in a loaded workbook :

private static void Main()
            {
               //The workbook I need to update
                Workbook workbook = Workbook.Load("file.xlsx");
                Workbook temp = SetIndicatorsWorkbook();
                WorksheetColumn wc = temp.Worksheets[0].Columns[0];

                //The issue is that Worksheet's Columns collection has no "Insert" property

                workbook.Save("file.xlsx");         
            }

Solution

  • The Columns collection of the Worksheet has an Insert method that will shift data/formatting just as would happen in Excel. This was added in the 2014 volume 2 version. You can read more about that in the help topic or the api documentation. Note I've linked to the WPF version help but the Insert method is available in the other platforms as well.