Add new columns to excel using c

To add new columns to an Excel file using C#, you can use the EPPlus library, which is a.NET library that allows you to create and manipulate Excel files. Here's an example of how you can do it:

using OfficeOpenXml;
using OfficeOpenXml.Style;

// Load the Excel file
using (var package = new ExcelPackage(new FileInfo("example.xlsx")))
{
    // Get the worksheet
    var worksheet = package.Workbook.Worksheets["Sheet1"];

    // Add a new column
    worksheet.InsertColumn(2, 1); // Add a new column at position 2 (B column)

    // Set the column width
    worksheet.Column(2).Width = 20;

    // Set the column header
    worksheet.Cells[1, 2].Value = "New Column Header";

    // Save the changes
    package.Save();
}

This code loads an Excel file named "example.xlsx", gets the worksheet named "Sheet1", adds a new column at position 2 (B column), sets the column width to 20, sets the column header to "New Column Header", and saves the changes.

You can also use the InsertColumn method to add a new column at a specific position, and the Column property to get a reference to the new column.

Alternatively, you can use the OpenXml library, which is a part of the.NET Framework, to manipulate the Excel file. Here's an example:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

// Load the Excel file
using (var package = new Package())
{
    // Get the workbook
    var workbook = package.GetPackage("workbook") as Workbook;

    // Get the worksheet
    var worksheet = workbook.Worksheets["Sheet1"] as Worksheet;

    // Add a new column
    worksheet.InsertColumn(2, 1); // Add a new column at position 2 (B column)

    // Set the column width
    worksheet.Column(2).Width = 20;

    // Set the column header
    worksheet.Cells[1, 2].Value = "New Column Header";

    // Save the changes
    package.Save();
}

This code loads an Excel file, gets the worksheet named "Sheet1", adds a new column at position 2 (B column), sets the column width to 20, sets the column header to "New Column Header", and saves the changes.

Note that the OpenXml library is more low-level than EPPlus, and requires more manual manipulation of the Excel file's XML structure.