Click here to Skip to main content
13,597,186 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 15 Apr 2012
Licenced CPOL

Export GridView Data to Excel using OpenXml

, 15 Apr 2012
Rate this:
Please Sign up or sign in to vote.
ASP.NET GridView to Excel conversion.


This article tells how to export ASP.NET GridView data to an Excel file using Office OpenXML SDK 2.0.


Exporting GridView data to Excel is a common functionality you would have come across while working in web forms. There are many ways to do this. Indeed, couple of approaches require Microsoft ACE engine to be installed on the server. I thought why not we try do this using Office OpenXML?

Using the code

So I started with creating an ASPX page (would look like the below one…)

An Export button click event would do:

DataTable table = new DataTable();   // create an empty data table
CreateTable(grdvTest, ref table);    // fill it with grid view data 
// ExportToExcel(table);
// copy this file from Temp directory to the current working directory
// Response.Redirect(localCopy); 
CreateTable(grdvTest, ref table);    // fill it with grid view data 
// ExportToExcel(table);
// copy this file from Temp directory to the current working directory 
// Response.Redirect(localCopy);

Let’s see what these method internally do!

  1. CreateTable will create columns and populates the row.
  2. for (int i = 0; i < grdvTest.HeaderRow.Cells.Count; i++)
    // fill rows             
    foreach (GridViewRow row in grdvTest.Rows)             
        DataRow dr;                 
        dr = table.NewRow();                 
        for (int i = 0; i < row.Cells.Count; i++)
              dr[i] = row.Cells[i].Text.Replace("&nbsp;", "");
  3. Export to Excel will create a file in systems temporary location and returns the file name.
  4. string excelfile = Path.GetTempPath() +
                            Guid.NewGuid().ToString() + ".xlsx";            
    using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Create(
          CreateExcelParts(excelDoc, table);
    return excelfile;
  5. Once the temporary file ready, stream it to client using Response.Redirect. The Excel file would look like:

Points of Interest

This method actually creates an Excel file on server (without using interops) and streams the file to client.


This article can further be extended to include style information during data export.


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


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionWhat is inside CreateExcelParts(excelDoc, table); ? Pin
onkar31184-May-16 2:09
memberonkar31184-May-16 2:09 
QuestionI want to ask for extra sheet is not active Pin
Member 1058710413-Feb-14 16:23
memberMember 1058710413-Feb-14 16:23 
QuestionMessage Closed Pin
9-Dec-13 7:02
membernilesh319-Dec-13 7:02 
QuestionMake XL Cells readonly Pin
$aikumar$ Cilamkoti18-Nov-13 21:20
member$aikumar$ Cilamkoti18-Nov-13 21:20 
QuestionHow to add sheets??? Pin
fense5-Mar-13 5:41
memberfense5-Mar-13 5:41 
Questionloop thru dropdownlist of gridview Pin
sayedalishakeel20-Apr-12 3:37
membersayedalishakeel20-Apr-12 3:37 
AnswerRe: loop thru dropdownlist of gridview Pin
pramodhegde8821-Apr-12 0:04
memberpramodhegde8821-Apr-12 0:04 
GeneralRe: loop thru dropdownlist of gridview Pin
sayedalishakeel23-Apr-12 19:22
membersayedalishakeel23-Apr-12 19:22 

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
Web04-2016 | 2.8.180621.3 | Last Updated 15 Apr 2012
Article Copyright 2012 by pramod.hegde
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid