Skip to main content
Post Made Community Wiki by Jamal
Source Link
Volearix
  • 829
  • 3
  • 9
  • 22

Completed the new code, runs much faster than before without switching to XML options. This is almost instantaneous creation even with large files.

public static string Export(string excelFileName, 
                            string[] excelWorksheetName, 
                            string tableStyle, 
                            params System.Data.DataTable[] dt)
{
    Application excel = new Application();
    excel.DisplayAlerts = false;
    excel.Visible = false;
    excel.ScreenUpdating = false;

    Workbooks workbooks = excel.Workbooks;
    Workbook workbook = workbooks.Add(Type.Missing);

    // Count of data tables provided.
    int iterator = dt.Length;
    for (int i = 0; i < iterator; i++)
    {
        Sheets worksheets = workbook.Sheets;
        Worksheet worksheet = (Worksheet)worksheets[i + 1];
        worksheet.Name = excelWorksheetName[i];

        int rows = dt[i].Rows.Count;
        int columns = dt[i].Columns.Count;
        // Add the +1 to allow room for column headers.
        var data = new object[rows + 1, columns];

        // Insert column headers.
        for (var column = 0; column < columns; column++)
        {
            data[0, column] = dt[i].Columns[column].ColumnName;
        }

        // Insert the provided records.
        for (var row = 0; row < rows; row++)
        {
            for (var column = 0; column < columns; column++)
            {
                data[row + 1, column] = dt[i].Rows[row][column];
            }
        }

        // Write this data to the excel worksheet.
        Range beginWrite = (Range)worksheet.Cells[1, 1];
        Range endWrite = (Range)worksheet.Cells[rows + 1, columns];
        Range sheetData = worksheet.Range[beginWrite, endWrite];
        sheetData.Value2 = data;

        // Additional row, column and table formatting.
        worksheet.Select();
        sheetData.Worksheet.ListObjects.Add(XlListObjectSourceType.xlSrcRange,
                                           sheetData,
                                           System.Type.Missing,
                                           XlYesNoGuess.xlYes,
                                           System.Type.Missing).Name = excelWorksheetName[i];
        sheetData.Select();
        sheetData.Worksheet.ListObjects[excelWorksheetName[i]].TableStyle = tableStyle;
        excel.Application.Range["2:2"].Select();
        excel.ActiveWindow.FreezePanes = true;
        excel.ActiveWindow.DisplayGridlines = false;
        excel.Application.Cells.EntireColumn.AutoFit();
        excel.Application.Cells.EntireRow.AutoFit();

        // Select the first cell in the worksheet.
        excel.Application.Range["$A$2"].Select();
    }

    // Turn off alerts to prevent asking for 'overwrite existing' and 'save changes' messages.
    excel.DisplayAlerts = false;

    // Save our workbook and close excel.
    string SaveFilePath = string.Format(@"{0}.xls", excelFileName);
    workbook.SaveAs(SaveFilePath, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    workbook.Close(false, Type.Missing, Type.Missing);
    excel.Quit();

    // Release our resources.
    Marshal.ReleaseComObject(workbook);
    Marshal.ReleaseComObject(workbooks);
    Marshal.ReleaseComObject(excel);
    Marshal.FinalReleaseComObject(excel);

    return SaveFilePath;
}