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

A Simple Way to Export DataGridViews/Lists to Excel

, 1 Nov 2012
Rate this:
Please Sign up or sign in to vote.
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. 

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

Comments and Discussions

 
QuestionMissing Method PinmemberTimothy Ayodele21-Nov-12 22:04 
AnswerRe: Missing Method PinmemberPasan Indeewara21-Nov-12 22:23 
GeneralRe: Missing Method PinmemberTimothy Ayodele22-Nov-12 4:14 
GeneralRe: Missing Method PinmemberPasan Indeewara22-Nov-12 6:12 
Generalsimpler way Pinmemberstefanveliki5-Oct-12 7:34 
GeneralRe: simpler way PinmemberPasan Indeewara5-Oct-12 19:34 

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

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

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