Click here to Skip to main content
Click here to Skip to main content

A Simple Way to Export DataGridViews/Lists to Excel

By , 1 Nov 2012
 

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. 

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)

About the Author

Pasan Indeewara
Software Developer (Senior)
Sri Lanka Sri Lanka
• C# Developer since 2005
• SAP/ABAP Technical Consultant since Sep. 2010
• Has experience in .NET Framework, SQL, VFP
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMissing MethodmemberTimothy Ayodele21-Nov-12 22:04 
It has a missing method GetFormattedValue, pls post that.
 
Thanks
 
Timotech
AnswerRe: Missing MethodmemberPasan Indeewara21-Nov-12 22:23 
It's kept to do a user specific formatting.. I had only one line in it..
 
return cell.FormattedValue.ToString();

GeneralRe: Missing MethodmemberTimothy Ayodele22-Nov-12 4:14 
Thanks for the reply
GeneralRe: Missing MethodmemberPasan Indeewara22-Nov-12 6:12 
Sure, You're welcome mate!
Generalsimpler waymemberstefanveliki5-Oct-12 7:34 
all you need is empty page with gridview on it, then in the back end just override the render method, change the content type to excel, set the inline file name, and render the gridview to the writer of the page render method... about... 8 lines of code
 
same method can be used to export to word and csv
GeneralRe: simpler waymemberPasan Indeewara5-Oct-12 19:34 
Our suggested method can be used for both on web and desktop.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 1 Nov 2012
Article Copyright 2012 by Pasan Indeewara
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid