question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slow performance for AdjustToContents() on large row numers

See original GitHub issue

Read and complete the full issue template

Do not randomly delete sections. They are here for a reason.

Do you want to request a feature or report a bug?

  • Bug
  • Feature
  • Question

Did you test against the latest CI build?

  • Yes
  • No

If you answered No, please test with the latest development build first.

Version of ClosedXML 0.95.4

What is the current behavior? If I execute the code worksheet.Columns().AdjustToContents() with 10’000 rows it takes more than 20 minutes (I stopped it).

What is the expected behavior or new feature? It should takes about an half seconds (as Excel does).

Is this a regression from the previous version? I don’t know.

Regressions get higher priority. Test against the latest build of the previous minor version. For example, if you experience a problem on v0.95.3, check whether it the problem occurred in v0.94.2 too.

Reproducibility

This is an important section. Read it carefully. Failure to do so will cause a ‘RTFM’ comment.

Without a code sample, it is unlikely that your issue will get attention. Don’t be lazy. Do the effort and assist the developers to reproduce your problem. Code samples should be minimal complete and verifiable. Sample spreadsheets should be attached whenever applicable. Remove sensitive information.

Code to reproduce problem:

public void Main()
{
            var workbook = new XLWorkbook();
            var worksheet = workbook.Worksheets.Add("Dati");

            DataGenerationWithBigData();

            worksheet.Columns().AdjustToContents();

            worksheet.PageSetup.PageOrientation = XLPageOrientation.Portrait;
            worksheet.PageSetup.PaperSize = XLPaperSize.A4Paper;

            workbook.CalculateMode = XLCalculateMode.Auto;

            workbook.SaveAs(workbookFile);
}
  • I attached a sample spreadsheet. (You can drag files on to this issue)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Pitterlingcommented, Jan 19, 2021

@Netclick19 … you know you could limit that by yourself …

//startRow=1, endRow=100
.Columns().AdjustToContents(1,100);
1reaction
SirMeowingtoncommented, Mar 1, 2023

I just want to add that the AdjustToContents() method still takes a seemingly exorbitant amount of time. 10,000 rows with 32 columns (most of them populated with varying lengths of text) takes the AdjustToContents() method approximately 1 minute 17 seconds, or 130 rows per second. 250,000 rows took 31 minutes 15 seconds (133 rows per second).

This was measured by a System.Diagnostics.Stopwatch object that was started immediately before and stopped immediately after the AdjustToContents() method was called.

using (XLWorkbook wb = new())
{
     IXLWorksheet employeeSheet = wb.Worksheets.Add("Employee");
     IXLRange employeeDataRange = employeeSheet.Cell(1, 1).InsertTable(resultsFile.EmployeeRecords); //EmployeeRecords is an IEnumerable<T> collection which only takes a couple seconds for the InsertTable() method to handle.
     Stopwatch sw = new();
     sw.Start();
     employeeSheet.Columns().AdjustToContents();
     sw.Stop();
     wb.SaveAs("TestBook.xlsx");
}

In case it’s helpful: Visual Studio 2022 .NET 6.0 ClosedXML v0.100.3 ExcelNumberFormat v1.1.0 OS: Windows Server 2019 CPU: Dual 3.00 GHz Intel Xeon® Gold 6284R RAM: 64 GB HDD: Solid state

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using ClosedXML how to adjust row height to content?
Rows().AdjustToContents();. The text has been moved by words, but row height is not changed. How to adjust row height to cell content?
Read more >
Creating Excel document is very slow
Attached is a generic code I wrote to create an Excel file with x number of worksheets. The problem I am having is...
Read more >
Slow performance from Worksheet.AutoFitRows with large ...
I'm calling Worksheet.AutoFitRows() on some worksheets that can have a very large number of rows (over 100,000). I know that this is a...
Read more >
[Solved] Reading large record from Excel using ...
Hi sirearlier i asked to import excel to database using closed xml its workingbut very slow working when i go for 10 Lakh...
Read more >
Commented Issue - ClosedXML - The easy way to OpenXML
It seems the problem is in the AdjustToContents() method. The full exception is: ---> System.InvalidOperationException: Object is currently in use elsewhere.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found