Click here to Skip to main content
15,878,809 members
Articles / Productivity Apps and Services / Microsoft Office / Office Interop
Tip/Trick

A Simple Way to Export DataGridViews/Lists to Excel

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Nov 2012CPOL 21.2K   9   6
This article focuses on a Very simple and Robust way to export Data to EXCEL

Introduction 

This article describes exporting to Excel without using any 3rd party DLLs, Office Interops, or any other tool. This is a simple method to get the exporting done and it's a robust way. This is a suggestive solution for all who are involved in developing similar tools/utilities to do similar stuff.

Background 

This mechanism is inspired by the XML/HTML compatible Excel engine.

Using the Code 

The below method is written for the exporting functionality. You can simply get this copied into your apps. 

C#
private static string ExportDataGridView2EXCEL(DataGridView pDataGridView, string pFilePath)
{
    String file2exp = "<HTML><BR/><TABLE BORDER = 1 " + 
           "><TR><TH style= background-color:#E4E2E2>#</TH>";

    // Headers
    foreach (DataGridViewColumn col in pDataGridView.Columns)
    {
        if (col.Visible)
        {
            file2exp += "<TH style= background-color:#E4E2E2>" + 
                        col.HeaderText + "</TH>";
        }
    }
    file2exp += "</TR>";
    
    int cnt = 1; // row counter

    foreach (DataGridViewRow row in pDataGridView.Rows)
    {
        if (!row.Visible)
            continue;
        file2exp += "<TR><TD>" + cnt++ + "</TD>";
        foreach (DataGridViewCell cell in row.Cells)
        {
            if (!cell.Visible)
                continue;

            file2exp += "<TD>" + 
                 GetFormattedValue(cell) + "</TD>";
        }
        file2exp += "</TR>";
    }

    file2exp += "</TABLE></HTML>";

    File.WriteAllText(pFilePath, file2exp);

    return "Y";
}  

And just call this method wherever you want to export DataGridViews. 

Points of Interest

This is a cool way that is suggested for use for exporting data to Excel. Further this can be enhanced by formatting cell by cell for many purposes. Hope you enjoy this.

License

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


Written By
Software Developer (Senior)
Australia Australia
• Salesforce certified Consultant
• C# Developer since 2005
• SAP/ABAP Technical Consultant since Sep. 2010
• Has experience in .NET Framework, SQL, VFP

Comments and Discussions

 
QuestionMissing Method Pin
Timothy Ayodele21-Nov-12 22:04
Timothy Ayodele21-Nov-12 22:04 
AnswerRe: Missing Method Pin
Pasan Eeriyagama21-Nov-12 22:23
Pasan Eeriyagama21-Nov-12 22:23 
GeneralRe: Missing Method Pin
Timothy Ayodele22-Nov-12 4:14
Timothy Ayodele22-Nov-12 4:14 
GeneralRe: Missing Method Pin
Pasan Eeriyagama22-Nov-12 6:12
Pasan Eeriyagama22-Nov-12 6:12 
Generalsimpler way Pin
stefanveliki5-Oct-12 7:34
stefanveliki5-Oct-12 7:34 
GeneralRe: simpler way Pin
Pasan Eeriyagama5-Oct-12 19:34
Pasan Eeriyagama5-Oct-12 19:34 

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.