Gembox Spreadsheet add column without remove the o

2019-09-12 01:01发布

问题:

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.

回答1:

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
});