user2075861
user2075861

Reputation: 117

Gembox Spreadsheet add column without remove the other ones

I'm coding in c# and using Gembox Spreadsheet to manipulate excel files. I'd like to know if it's possible to add a column(without remove the other ones) in a pre-existent xls file:

ExcelFile ef = ExcelFile.Load(masterFile);
ExcelWorksheet ws = ef.Worksheets["Peer Review"];
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Standard Deviation", typeof(double));
ws.InsertDataTable(dataTable, new InsertDataTableOptions()
            {
                ColumnHeaders = true,
                StartRow = 0,
                StartColumn = 15
            });
ef.Save(masterFile);

In the way I mentioned I can insert a new column at position "15" but in the same way the 15th old column is removed. So I'd like to insert a column without removing the other ones.

Upvotes: 0

Views: 1753

Answers (1)

GemBox Dev Team
GemBox Dev Team

Reputation: 669

InsertDataTable methods insert the DataTable's data into specified cell range, they do not insert new excel rows nor columns.

So what you can do is explicitly make room for the desired insertion by adding an empty columns, for example:

ws.Columns.InsertEmpty(15, dataTable.Columns.Count);
ws.InsertDataTable(dataTable, new InsertDataTableOptions()
{
    ColumnHeaders = true,
    StartRow = 0,
    StartColumn = 15
});

Upvotes: 2

Related Questions