Bug: extremely slow AutoFitColumns performance

The AutoFitColumns() method is extremely slow when working on a worksheet with a significant amount of data. I wrote a short sample console program in VB to reproduce the problem. Here’s the output I get when I run it:

Getting DataTable…
Done! (20 columns, 25000 rows, 15 characters per cell)

AutoFitting columns…
Done! 22.268 seconds

Press any key to continue . . .

In Excel 2003, if I run “Format/Column/AutoFit” on a worksheet with the exact same data, the operation completes in like half a second. 22.26 seconds vs. 0.5 seconds is a very big performance gap. Here’s the code:

Imports Aspose.Cells
Imports System.IO

Module AutoFitColumnsDemo

Const _numOfRows As Integer = 25000
Const _numOfColumns As Integer = 20
Const _cellContents As String = “XXXXXXXXXXXXXXX”


Sub Main()

’ set Aspose Cells license
Dim asposeCellsLicense As New Aspose.Cells.License
asposeCellsLicense.SetLicense(New MemoryStream(My.Resources.Aspose_Total_License))

’ generate sample datatable
Console.WriteLine(String.Empty)
Console.WriteLine(“Getting DataTable…”)
Dim dt As DataTable = GetSampleData()
Console.WriteLine(String.Format(“Done! ({0} columns, {1} rows, {2} characters per cell)”, dt.Columns.Count, dt.Rows.Count, _cellContents.Length))
Console.WriteLine(String.Empty)

’ paste data onto worksheet
Dim wb As New Aspose.Cells.Workbook
wb.Worksheets(0).Cells.ImportDataTable(dt, True, “A1”)

Dim stopWatch As New Stopwatch

Console.WriteLine(“AutoFitting columns…”)
stopWatch.Reset()
stopWatch.Start()
wb.Worksheets(0).AutoFitColumns()
stopWatch.Stop()
Console.WriteLine(String.Format(“Done! {0} seconds”, stopWatch.ElapsedMilliseconds / 1000))

Console.WriteLine(String.Empty)
Console.Write(“Press any key to continue . . .”)
Console.ReadKey(True)

End Sub

‘’’


‘’’ creates and returns a datatable with sample data
‘’’

Function GetSampleData() As DataTable

Dim dt As New DataTable
For c As Integer = 0 To _numOfColumns - 1
dt.Columns.Add(“column” & c)
Next

For rowCount As Integer = 0 To _numOfRows - 1
Dim dr As DataRow = dt.Rows.Add
For i As Integer = 0 To _numOfColumns - 1
dr.Item(i) = _cellContents
Next
Next

Return dt

End Function

End Module

I’ve also attached an Excel 2007 spreadsheet that contains the same data that would be generated by the sample program. The columns haven’t been autofit yet, so you can open it up in Excel and compare the speeds of Excel’s column autofitting and Aspose.Cell’s AutoFitColumns.

Hi,

Thank you for considering Aspose.

Yes, we have tested your scenario and found out that AutoFitColumns() method is taking some time in execution. We will look into it and see if we can optimize the AutoFitColumns() method.

Thank You & Best Regards,

Thank you!

Hi,

Thank you for considering Aspose.

Please try the attached latest fix of Aspose.Cells . We have improved the performance of AutoFitColumns API.

Thank You & Best Regards,

This fix worked for the code pasted above. Thank you! But it’s still performing slow (12 secs) when I open this one workbook and autofit the columns. Here’s the code:

Imports Aspose.Cells
Imports System.IO

Module AutoFitColumnsDemo2

Const _fileName As String = “C:\workbook.xlsx”


Sub Main()

’ set Aspose Cells license
Dim asposeCellsLicense As New Aspose.Cells.License
asposeCellsLicense.SetLicense(New MemoryStream(My.Resources.Aspose_Total_License))

Dim wb As New Workbook
wb.Open(_fileName)
Console.WriteLine(String.Format(“Opened: {0}”, _fileName))

Dim stopWatch As New Stopwatch

Console.WriteLine(“AutoFitting columns…”)
stopWatch.Reset()
stopWatch.Start()
wb.Worksheets(0).AutoFitColumns()
stopWatch.Stop()
Console.WriteLine(String.Format(“Done! {0} seconds”, stopWatch.ElapsedMilliseconds / 1000))

Console.WriteLine(String.Empty)
Console.Write(“Press any key to continue . . .”)
Console.ReadKey(True)

End Sub

End Module

I’ve attached the Excel 2007 spreadsheet file used in the code to this message.

Hi,

Thank you for sharing the template file.

Yes, we have found your mentioned issue. We will look into it and get back to you soon.

Thank You & Best Regards,

Hi,

Thank you for considering Aspose.

Please try the attached latest fix. We have improved the performance of AutoFitColumns API.

Thank You & Best Regards,

Excellent. It’s rare to find such good support. Since we use this product extensively, this helps us greatly. Thank you very much.

The issues you have found earlier (filed as 9192) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Note: Just for your knowledge. In the new release v4.8.0, we have merged Aspose.Grid suite to Aspose.Cells for .NET msi installer as Aspose.Grid would be no longer offered as a separate product now. You need to install the installer (uninstall older one first if you have) and use only Aspose.Cells.dll library in your project for your need. You may also take advantage using Aspose.Cells Grid suite though.