Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Avoid Opening Workbooks Populated with Large Quantities of Data with ExcelApplication

Why What might cause this might to happen:

  • Result of an attempt to get dynamic formatting based on user choices in asp.net.
  • Attempts to autofit column widths after the data has been populated.
  • To add special formatting to cells based on their values.
    Code Block
    /* INCORRECT CODE:
    
    
    ExcelTemplate xlt = new ExcelTemplate();
    xlt.Open(Page.MapPath(@"path\to\code.xlsx"));
    
    /*...*/
    
    xlt.Process();
    
    ExcelApplication xla = new ExcelTemplate();
    Workbook wb = xla.Open(xlt);
    
    xla.Save(wb, "Completed.xlsx"); */
    

Issues with Why this method causes issues:

  • ExcelApplication fills out everything needed for customization, so when already populated files are passed to it, it requires more for it to load them in than it does for ExcelTemplate alone.
  • Also results in a large hit on Time based performance.

...

1.) Referencing cells that don't exist in the input file

Why What might cause this might to happen:

  • Looping through and referencing a large amount of cells, some of which are empty, while looking for something.
  • A search might go past the end of a column or row if the extent of the data is not known.
  • Creating large areas which extend past where the data is located.

Issues with Why this method causes issues:

  • Forces ExcelWriter to create cell (and associated) object(s) for each cell referenced.  For large quantities of unnecessary cells, this can quickly result in performance issues.

...

2.) Empty cells in the input file

Why What might cause this might to happen:

  • Sometimes people create template files using previously populated reports by clearing out values.
  • Templates with large numbers of blank cells can be identified by either the scrollbar, which may be suspiciously small for having no cells with data, or by the size of the file, which may be larger than a normal empty template.

Issues with Why this method causes issues:

  • Just clearing out the values and formatting does not mean the cell has been removed.  ExcelTemplate does not overwrite existing cells while passing values into the template file, it just pushes them down.
  • Template files that previously had lots of values that are not properly cleaned out quickly become bloated.

...

Apply Styles to Row/Columns, Not Cells/Areas

Why What might cause this might to happen:

  • Loop through a large number of cells, setting styles on each cell individually.
  • Set group of cells to an area, then apply that style to the area.

Issues with Why this method causes issues:

  • Styles set to areas still apply to each individual cell.
  • When styles are set on individual cells, the a copy of the style information is kept for every one of the cells. 
  • When styles are set on rows/columns, only one copy of the information is kept for each row/column.

...

When Possible, Use DataReaders Instead of DataTables

Why What might cause this might to happen:

  • Although they are less efficient for simpler tasks, DataTables can do everything that DataReaders can, so someone may not realize and may just use a DataTable when a DataReader could have done everything they needed more efficiently.

Issue with Why this method causes issues:

  • DataTables create an in-memory copy of all the data.  They are useful for manipulating data in memory or updating the database, but tends to be rather inefficient for just pulling data out of a database.
  • Also results in a hit on Time based performance.

...