-
Notifications
You must be signed in to change notification settings - Fork 891
Where to use the using keyword
Francois Botha edited this page Oct 22, 2016
·
3 revisions
Note: The "using" keyword is not needed if you turn off event tracking on the workbook. Please see Turning off events for more details.
You should use the "using" keyword whenever you get a hold of a range-like object inside a loop. That includes workbook, worksheet, row, rows, column, and columns.
The following recommendations assume you're dealing with big files.
Not so good:
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Sheet1");
for (int ro = 1; ro <= 1000000; ro++)
{
ws.Row(ro).FirstCell().Value = ro;
}
Best way:
using (var wb = new XLWorkbook())
{
// No need to put the worksheet inside a "using" block because
// the workbook will dispose of the sheets. The worksheet is not
// created inside a loop and the workbook's dispose is being
// called immediately after using the worksheet.
var ws = wb.Worksheets.Add("Sheet1");
for (int ro = 1; ro <= 1000000; ro++)
{
// Dispose of the row once we're done with it
using(var row = ws.Row(ro))
row.FirstCell().Value = ro;
}
}
Not so good:
var wb = new XLWorkbook();
for (int wsNum = 1; wsNum <= 10; wsNum++)
{
var ws = wb.Worksheets.Add("Sheet" + wsNum);
// Add stuff to the worksheet...
}
Best way:
using (var wb = new XLWorkbook())
{
for (int wsNum = 1; wsNum <= 10; wsNum++)
{
// Dispose the worksheet right away
// (Don't wait for the workbook to do so)
using (var ws = wb.Worksheets.Add("Sheet" + wsNum))
{
// Add stuff to the worksheet...
}
}
}
- 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