Comments

Your code has way too many comments, mostly redundant ones - some are even obsolete!

Good comments should say why, not what.


// Create our new excel application and add our workbooks/worksheets
Workbooks workbooks = xls.Workbooks;
Workbook workbook = workbooks.Add();



This comment says what the code is doing, and it's lying - you're not creating a new Excel application, you're using an existing instance. Remove it.


// Hide our excel object if it's visible.
xls.Visible = false;



This comment adds no value, and is lying to a certain extent: you don't actually care whether it's already visible or not. Better just remove it.


// Turn off calculations if set to automatic; this can help prevent memory leaks.
xls.Calculation = xls.Calculation == XlCalculation.xlCalculationAutomatic ? XlCalculation.xlCalculationManual : XlCalculation.xlCalculationManual;



This one is accurate and informative - it says why you're turning off automatic calculations.. but it also says what the code is doing. Better rephrase it:

// turning off automatic calculations improves performance and can help prevent memory leaks.
xls.Calculation = (xls.Calculation == XlCalculation.xlCalculationAutomatic) 
                                    ? XlCalculation.xlCalculationManual 
                                    : XlCalculation.xlCalculationManual;


Notice how readability is improved by splitting the ternary operation into 3 lines. It appears the condition is moot, since both ends produce the same assignation. The instruction should be rewritten as simply:

xls.Calculation = XlCalculation.xlCalculationManual;



// Turn off screen updating so our export will process more quickly.
xls.ScreenUpdating = false;



Again, says what - the why makes it closely related to the previous statement. Thus:

// turning off automatic calculations and screen updating 
// improves performance and can help prevent memory leaks.
xls.Calculation = XlCalculation.xlCalculationManual;
xls.ScreenUpdating = false;



// Create an excel table and fill it will our query table.



Remove. This one adds no value.


for (int i = 0; i &lt;= iterator; i++)
{
    // Turn off calculations if set to automatic; this can help prevent memory leaks.
    Worksheet worksheet = (Worksheet)xls.Worksheets[i + 1];
    worksheet.Name = excelWorksheetName[i];
    worksheet.Select();



Apparently the code under that comment was moved, but the comment remained.



As for performance, @Will's comment is accurate - COM interop is hurting you here. What's slowing it down is the many calls to the Excel object model (COM interop in itself incurs a performance penalty, but Excel interop somehow makes it even worse). The only way is to limit the number of times you're accessing the Excel object model.

An alternative approach could be to write the data in a .csv file, and use the Excel object model to import the .csv data all in one shot (rather than looping into rows and columns to write the data "manually"), and then to format the workbook and save it in .xlsx format.

Creating Excel document is very slow
When I visit a page that does not exist, I expect to still see the primary and secondary links as on any other page, but what I get is no navigation links in the 404 page. See also No navigation links on 404 pages.

I have found a solution, but I am happy if something else can fix this (possibly without using extra modules).
