Click here to Skip to main content
14,210,385 members
Click here to Skip to main content
Posted 4 Oct 2012


9 bookmarked

A Simple Way to Export DataGridViews/Lists to Excel

Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
1 Nov 2012     CPOL    
This article focuses on a Very simple and Robust way to export Data to EXCEL


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.


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)
        file2exp += "<TR><TD>" + cnt++ + "</TD>";
        foreach (DataGridViewCell cell in row.Cells)
            if (!cell.Visible)

            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.


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


About the Author

Pasan Eeriyagama
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
memberTimothy Ayodele21-Nov-12 22:04 
AnswerRe: Missing Method Pin
Pasan Eeriyagama21-Nov-12 22:23
memberPasan Eeriyagama21-Nov-12 22:23 
GeneralRe: Missing Method Pin
Timothy Ayodele22-Nov-12 4:14
memberTimothy Ayodele22-Nov-12 4:14 
GeneralRe: Missing Method Pin
Pasan Eeriyagama22-Nov-12 6:12
memberPasan Eeriyagama22-Nov-12 6:12 
Generalsimpler way Pin
stefanveliki5-Oct-12 7:34
memberstefanveliki5-Oct-12 7:34 
GeneralRe: simpler way Pin
Pasan Eeriyagama5-Oct-12 19:34
memberPasan 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190618.1 | Last Updated 1 Nov 2012
Article Copyright 2012 by Pasan Eeriyagama
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid