Free Support Forum - aspose.com

Aspose.Cells Insert and Delete Columns

Hi again,

I’m having this absolutely strange problem… let me see if I can explain it clearly.

I have this worksheet with some styles and data already in it. I open it with Aspose.Cells and I want to edit it, for instance, adding some columns and deleting others, then I want to readjust some column’s width.

The issue arises when I combine all this actions together. Deleting columns after inserting others, completely resets the widths of the columns of the sheet. Although this only happens when the sheet has data, because I tried it with empty sheets and it worked fine. Given the results of my tests, I believe that the problem resides in the action of inserting columns between existing columns with data and then deleting some of those columns. I provided you with a sample file to try it has the code that creates this issue:

Workbook book2 = new Workbook();
book2.Open(“c:\open.xls”);
book2.Worksheets[0].Cells.InsertColumn(7);
book2.Worksheets[0].Cells.InsertColumn(7);
book2.Worksheets[0].Cells.InsertColumn(7);
book2.Worksheets[0].Cells.InsertColumn(7);
book2.Worksheets[0].Cells.DeleteColumn(7);
book2.Worksheets[0].Cells.SetColumnWidth(0, 40);
book2.Save(“c:\result.xls”);

If you run this piece of code with the sheet in attach, you will find that not only the first column was not resized, but the original width of several other columns was reset. Curiously, the code begins to work if you just switch the DeleteColumn method with the InsertColumn methods, like this:

Workbook book2 = new Workbook();

book2.Open(“c:\open.xls”);

book2.Worksheets[0].Cells.DeleteColumn(7);


book2.Worksheets[0].Cells.InsertColumn(7);

book2.Worksheets[0].Cells.InsertColumn(7);

book2.Worksheets[0].Cells.InsertColumn(7);

book2.Worksheets[0].Cells.InsertColumn(7);

//book2.Worksheets[0].Cells.DeleteColumn(7);


book2.Worksheets[0].Cells.SetColumnWidth(0, 40);

book2.Save(“c:\result.xls”);

I made several tests trying to figure out why this happens in order to give you more detailed information, but came to no conclusion after almost 2 hours. Besides the order of the method calls, the only thing I came to realize is the influence of data in the sheet.

Can you please check this bug? For now I can manage with placing the delete method before the inserts but it is most likely that in time I’ll have to remove inserted columns, and then I won’t be able to do workarounds…

Thanks,
Cruz

Hi,

Thanks for the template file with code

Yes, we can reproduce the problem using your code with the template file, We will look into it and figure out the issue soon.

Thank you.

Hi,

Please try this fix. We will set the same width of the new column as the previous column when we insert column.