-
Notifications
You must be signed in to change notification settings - Fork 892
Performance Guide
Jahav edited this page Sep 22, 2022
·
1 revision
This page should be a guide to avoid common performance pitfalls.
If you need to insert or delete more than one row or column or cell, don't do it in a loop. Use a range to perform the operations.
Example:
ws.Range("A1:A3000").Delete()
ClosedXML has to keep the internal state consistent. Inserting/deleting cells shifts other cells and they must be updated. If you delete a row, all rows below it must be updated to reflect the removed row, e.g.
- cell numbers have to be updated
- formulas that referenced the row must be shifted one row up (e.g.
A5*4
turns intoA4*4
)
Looping means the housekeeping must be done individually for each deleted/inserted row. That leads to O(n^2) complexity.
for (var row = 1; row < 3000; ++row)
ws.Cell(row, 1).Value = 1;
// First loop must update 3000 rows, second loop 2999 rows... The whole loop must update cells of 4 501 500 rows.
for (var row = 1; row < 3000; ++row)
ws.Row(row).Delete();
- How do I deliver an Excel file in ASP.NET?
- Does it support Excel 2003 and prior formats (.xls)?
- How can I insert an image?
- Text with numbers are getting converted to numbers, what's up with that?
- How do I get the result of a formula?
- Data Types
- Creating Multiple Worksheets
- Organizing Sheets
- Loading and Modifying Files
- Using Lambda Expressions
- Cell Values
- Workbook Properties
- Using Formulas
- Evaluating Formulas
- Creating Rows And Columns Outlines
- Hide Unhide Rows And Columns
- Freeze Panes
- Copying Worksheets
- Using Hyperlinks
- Data Validation
- Hide Worksheets
- Sheet Protection
- Tab Colors
- Conditional Formatting
- Pivot Table example
- Sparklines
- Copying IEnumerable Collections
- Inserting Data
- Inserting Tables
- Adding DataTable as Worksheet
- Adding DataSet
- Styles - Alignment
- Styles - Border
- Styles - Fill
- Styles - Font
- Styles - NumberFormat
- NumberFormatId Lookup Table
- Style Worksheet
- Style Rows and Columns
- Using Default Styles
- Using Colors
- ClosedXML Predefined Colors
- Excel Indexed Colors
- Using Rich Text
- Using Phonetics
- Defining Ranges
- Merging Cells
- Clearing Ranges
- Deleting Ranges
- Multiple Ranges
- Shifting Ranges
- Transpose Ranges
- Named Ranges
- Accessing Named Ranges
- Copying Ranges
- Using Tables
- Sorting Data
- Selecting Cells and Ranges
- Row Height and Styles
- Selecting Rows
- Inserting Rows
- Inserting and Deleting Rows
- Adjust Row Height and Column Width to Contents
- Row Cells
- Column Width and Styles
- Selecting Columns
- Inserting Columns
- Inserting and Deleting Columns
- Adjust Row Height and Column Width to Contents
- Column Cells
- Pages Tab
- Paper Size Lookup Table
- Margins Tab
- Headers and Footers Tab
- Sheet Tab
- Print Areas and Page Breaks