Free Support Forum - aspose.com

InsertRows() corrupts sheet with even number of rows

I'm using Aspose.Cells v8.2.1, attempting to add a large number of rows to a template file (at row 11 of 'before.xlsx' in attached zip). I've discovered that inserting an even number of rows results in corruption of the worksheet; all column headings (which are filters) revert to 'Column [x]', and content in the rows 0-9 is removed.

The code I'm investigating looks like this:

var cells = workbook.Worksheets["Results_Detail"].Cells;
var rowNum = 11;

cells.InsertRows(rowNum, resultCount - 2); // Because there are
// 2 rows in the
// 'before' file.

rowNum--;

foreach (var result in results)
{
var cellNum = 2;
var cell = cells.GetCell(rowNum, cellNum);

// Method with call to cell.PutValue()
// Call fails because cell == null

cellNum++;
// continue for more cells...

rowNum++;
}

Where resultCount is anything from one to hundreds of thousands.

My workaround is:

cells.InsertRows(rowNum, resultCount % 2 == 1 ? resultCount - 2 : resultCount - 1);

Then deleting a row at the end.

Incidentally, inserting rows one by one works fine, but takes a prohibitively long time.

Is this something that has been fixed in more recent releases? (The company I'm working for has a licence that is valid for up to v8.2.1.) Does anyone have an idea of the cause? And, as I'm not very familiar with Aspose.Cells, if there's better coding practice for adding content iteratively, I'd be grateful of the advice.

Thanks!


Hi,

Thanks for your posting and using Aspose.Cells.

We have fixed a similar issue recently. Please download and try the latest fix: Aspose.Cells
for .NET v8.5.2.4
. It should fix your issue. Let us know your feedback.

Thanks for your reply, Shakeel.

As stated, the company I’m working for has a licence valid only for releases up to 26 Sep 2014, which I believe means the most recent available to me is v8.2.1. If my workaround is the only option I have, that’s fine, but if you have any other advice it would be gratefully received.


Many thanks.

Hi,

Thanks for your posting and using Aspose.Cells.

Could you please try the latest version: Aspose.Cells
for .NET v8.5.2.4
without setting license. It will help us see if your issue is occurring because of some bug or because of some error in your code. We will look into it and help you asap.

Hi Shakeel,


(Thanks for reminding me to not set the license!)

The same failure occurs under v8.5.2.4. I should have added, yesterday, that after calling InsertRows(), if I then do cell.GetCell(11, 2), the resulting cell == null. This has not changed under the version you provided.

Also, even using my workaround, after inserting the rows the first of the column headings in the ‘before’ file, “Company code”, is replaced with “Column60”. All other headings are maintained.

Hi,

Thanks for your posting and using Aspose.Cells.

Please try the following code with your older and newer version. I have tested this code with the newer version. It basically inserts even number of rows i.e 1000 and it generates correct output excel file which is not corrupt. I have also attached the output excel file generated by the code for your reference.

C#
Workbook workbook = new Workbook(“before.xlsx”);

var cells = workbook.Worksheets[“Results_Detail”].Cells;
var rowNum = 11;
var totalRows = 1000; //even number of rows

cells.InsertRows(rowNum, totalRows);

workbook.Save(“output.xlsx”);

Hi Shakeel,


your code works, but I’m still encountering the problem under certain circumstances. Consider the following:

var cells = workbook.Worksheets[“Results_Detail”].Cells;
var rowNum = 11;
var totalRows = 1472;

cells.InsertRows(rowNum, totalRows);

var cell = cells.GetCell(10, 2);

cell equals null, and the sheet causes a warning when opening the file once Save() has been called. However, it works fine with totalRows set to 1470, 1471, 1473 or 1474.

Hi,

Thanks for your posting and using Aspose.Cells.

I have tested this issue with your code with the latest version, older versions and was not able to replicate this issue. I have attached the sample console application that uses 8.2.1.1 which is just next to 8.2.1.0 used by you and it also did not replicate this issue.

Please download the attached console application and run at your end and also check what version number it prints in the console. Let us know your feedback.

Please also provide us runnable sample application that replicates your issue with your older version for our further investigation.