We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Infinite loop applying column styles


We have a file that gets stuck in an infinite loop when we apply styles to the columns.

From my testing it appears that there is always one more column than the one we just modified and so as we loop over them, rather than altering what is there, we end up creating new columns.

I’m reporting it in case it points to some larger problem parsing this file.

Code to reproduce the issue is below using version 8.7.1.
try (InputStream inputStream = Files.newInputStream(Paths.get(“9882654.xls.xlsx”)))
LoadOptions loadOptions = new LoadOptions();
Workbook workbook = new Workbook(inputStream, loadOptions);

int worksheetCount = workbook.getWorksheets().getCount();
for (int i = 0; i < worksheetCount; i++)
Worksheet worksheet = workbook.getWorksheets().get(i);

Cells cells = worksheet.getCells();

// Before we auto fit the columns, make sure no text is rotated as this seems to confuse it.
Style noRotation = new Style();
StyleFlag setStyles = new StyleFlag();
for (int c = 0; c < cells.getColumns().getCount(); c++)
cells.applyColumnStyle(c, noRotation, setStyles);

Hi Nicholas,

Thank you for contacting support. We have tested your Excel file against the latest version of Aspose.Cells for Java API and managed to replicate the error of invalid index of the column. It occurs due to the infinite iterations. It has been logged under ticket ID CELLSJAVA-42238 in our bug tracking system. Your post has also been linked to this ticket. We shall keep you informed regarding any available updates. We are sorry for the inconvenience caused.

Thank you for accepting the bug.

Can you prioritise it with our Enterprise Support. Its causing quite allot of problems for us because it hangs and stops the application.

Hi Nicholas,

Thank you for the inquiry. As a workaround, please use getMaxDataColumn() method, it returns a maximum column index of cell which contains data.

// get the max column index
int mcol = cells.getMaxDataColumn(); for (int c = 0; c < mcol; c++) { cells.applyColumnStyle(c, noRotation, setStyles); }

I made that change and found a regression in one of our tests. This test was explicitly checking for the results of adjusting the columns to make sure we get all text no matter what orientation or column width has been set in the file.

What I’ve noticed is that there is a big difference between what getMaxDataColumn() and getColumns().getCount() return.

Looking over some of our test data I see the following
columnsCount = 19 MaxData = 18
columnsCount = 10 MaxData = 10
columnsCount = 4 MaxData = 7
columnsCount = 5 MaxData = 9
columnsCount = 19 MaxData = 18
columnsCount = 11 MaxData = 10
columnsCount = 8 MaxData = 7
columnsCount = 10 MaxData = 9
columnsCount = 19 MaxData = 18
columnsCount = 11 MaxData = 10
columnsCount = 8 MaxData = 7
columnsCount = 10 MaxData = 9
columnsCount = 256 MaxData = 5

What worries me is that at least one example can’t be explained by index + 1.

I just want to be sure I’m not missing any data.
Hi Nicholas,

In reference to the ticket ID CELLSJAVA-42238, our product team has completed the analysis phase and come to the conclusion that it is not a bug, but the misunderstanding of the ColumnCollection. ColumnCollection contains only those Column objects that has been initialized, not all columns that have data. When you are using ColumnCollection [index] to get one Column object, if the Column object with given index has not been initialized, then it will be created and added into the collection. The same behavior for Cells.ApplyColumnStyle(). It is very possible that the index (position) of the item in ColumnCollection is not same with column index of the corresponding item. To manipulate existing Column objects in the collection by the position of every item, you can use ColumnCollection.GetColumnByIndex (index). Please modify the code as below:

for (int c = 0; c < cells.getColumns().getCount(); c++) { cells.getColumns().getColumnByIndex(c).applyStyle(noRotation, setStyles); }

Thank you for the update.

I have tested your suggestion and it seems to be working.


Thanks for your feedback.

Good to know that the suggested API works for your needs. Feel free to contact us any time if you have further comments or questions, we will be happy to assist you soon.

Thank you.