Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Tip/Trick

Generate an Excel Spreadsheet from any DataTable (C#)

Rate me:
Please Sign up or sign in to vote.
4.44/5 (8 votes)
11 Jul 2016CPOL 17.3K   15   5
How to generate an Excel spreadsheet from any DataTable using C# and Excel Interop

Quick-and-Dirty Spreadsheet Spread

If you just need the data from a DataTable (such as the result of calling a Stored Procedure or a SQL query) and don't need to gussy it up much, this code will generate a spreadsheet with that data.

First, as for any Excel Interop project, you need to add a reference to the Microsoft.Office.Interop.Excel assembly, and then add a couple of using clauses like so:

C#
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

Then add the Excel objects you will need:

C#
private Excel.Application _xlApp;
private Excel.Workbook _xlBook;
private Excel.Sheets _xlSheets;
private static Excel.Worksheet _xlSheet;

...and the DataTable:

C#
private DataTable dtSPResults;

Then, add a method like this (the code that populates the DataTable is an exercise left to the reader):

C#
private void GenerateAndSaveSpreadsheet()
{
    try
    {
        InitializeExcelObjects();
        AddColumnHeadingRow();
        AddDataRows();
        _xlSheet.Columns.AutoFit();
        WriteSheet();
    }
    finally
    {
        DeinitializeExcelObjects();
    }
}

...with these methods that it calls:

C#
private void InitializeExcelObjects()
{
    _xlApp = new Excel.Application
    {
        SheetsInNewWorkbook = 1,
        StandardFont = "Calibri",
        StandardFontSize = 11
    };
    Thread.Sleep(2000); // if you need this line (I can't recall why I added it),
                        // add "using System.Threading;"

    _xlBook = _xlApp.Workbooks.Add(Type.Missing);

    _xlSheets = _xlBook.Worksheets;
    _xlSheet = (Excel.Worksheet)_xlSheets.Item[1];

    _xlSheet.Name = "BlaSheetName"; // You can replace the name
}

private void AddColumnHeadingRow()
{
    colCount = dtSPResults.Columns.Count;
    List<string> colNames = new List<string>();
    for (int i = 0; i < colCount; i++)
    {
        colNames.Add(dtSPResults.Columns[i].ToString());
    }

    var columnHeaderRowRange = _xlSheet.Range[
        _xlSheet.Cells[1, 1],
        _xlSheet.Cells[1, colCount]];
    columnHeaderRowRange.Interior.Color = Color.LightBlue;
    columnHeaderRowRange.RowHeight = 18;
    columnHeaderRowRange.Font.Bold = true;
    columnHeaderRowRange.Font.Size = 13;

    int rowToPop = 1;
    int currentColumn = 1;
    foreach (string s in colNames)
    {
        var colHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPop, currentColumn];
        colHeaderCell.Value2 = s;
        currentColumn++;
    }

    _xlSheet.Activate();
    _xlSheet.Application.ActiveWindow.SplitRow = 1;
    _xlSheet.Application.ActiveWindow.FreezePanes = true;
}

private void AddDataRows()
{
    int rowToPop = 2;
    foreach (DataRow row in dtSPResults.Rows)
    {
        for (int i = 1; i <= colCount; i++)
        {
            var genericCell = (Excel.Range)_xlSheet.Cells[rowToPop, i];
            var curVal = row[i-1].ToString();
            genericCell.Value2 = curVal;
        }
        rowToPop++;
    }
}

private void WriteSheet()
{
    Directory.CreateDirectory("BlaFolderName")); // Feel free to change this
    string filename = @"C:\BlaFolderName\Whatever.xlsx"); // Feel free to change this
    if (File.Exists(filename))
    {
        File.Delete(filename);
    }
    _xlBook.SaveAs(filename, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing,
                   Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing);
}

public void DeinitializeExcelObjects()
{
    Marshal.ReleaseComObject(_xlSheet);

    _xlBook.Close(false);
    Marshal.ReleaseComObject(_xlBook);

    _xlApp.DisplayAlerts = false;
    _xlApp.Quit();
    Marshal.ReleaseComObject(_xlApp);
    _xlApp = null;
}

That should generate an Excel spreadsheet from the data you feed it via the DataTable with a column header row that is frozen followed by all the raw data.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
SuggestionUse Ranges when export data to improve performance Pin
Alexander Sharykin25-Jul-16 22:49
Alexander Sharykin25-Jul-16 22:49 
QuestionEven faster yet Pin
Orion83212-Jul-16 18:22
Orion83212-Jul-16 18:22 
AnswerRe: Even faster yet Pin
WesMcGJr20-Jul-16 2:58
WesMcGJr20-Jul-16 2:58 
This is a great solution to saving data from SQL query.Thumbs Up | :thumbsup:
QuestionPerformance Pin
djmarcus12-Jul-16 7:15
djmarcus12-Jul-16 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.