call today Call Today 320.281.0605

Create Excel file dynamically (Simple Way)

May 16, 2010    Author: Joel

Scenerio: I’ve recently had a a few projects that I’ve created forms for.  I have given the client a login to view all the form submissions, but in the end they want this data in an Excel file so they can easily manage it.

Research: I have found there a quite a few ways to achieve this.  Some take advantage of the Excel interop, some create a .csv, some use third party tools, some use xml. Here is a good discussion I found on the topic at Stack Overflow, http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c.
All have pros and cons.

Solution: Here is what I have found to be the simplest way to read your dataset in an Excel file. First off, this doesn’t product a true Excel file, but it is something Excel can read into a table which often is all you need.

What I found is you can write the contents of a datagrid to an output stream that can be saved as a .xls file.

code snippet: I am using linq to bind to a datagrid

var registrations = from reg in db.Registrations
                    where reg.JumpPageId == int.Parse((String)e.CommandArgument)
                    orderby reg.TimeStamp descending
                    select reg;
HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = "";

// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename=registrants.xls");

// create a string writer
using (StringWriter sw = new StringWriter())
{
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
        // instantiate a datagrid
        DataGrid dg = new DataGrid();
        dg.DataSource = registrations;
        dg.DataBind();
        dg.RenderControl(htw);
        response.Write(sw.ToString());
        response.End();
    }
}
 

LIke I said, this doesn’t output a true Excel file so when you go to open it, you will be prompted a warning:

excelwarning

It is saying the contents of the file are in a different format than what the .xls extension says it is. 

Click Yes and it will open.

If you were to open this file in a text editor you will see that it really is just the datagrid html output.

Not the most elegant solution but it is a quick way to throw an Export to Excel button on a report page.

My next blog post will show how to create an Excel file using a third party tool.

Share
  • http://topsy.com/trackback?utm_source=pingback&utm_campaign=L1&url=http://blog.dahlindevelopment.com/2010/05/create-excel-file-dynamically-simple-way/ Tweets that mention Create Excel file dynamically (Simple Way) « Dahlin Development – Blog — Topsy.com

    [...] This post was mentioned on Twitter by Joel Dahlin. Joel Dahlin said: Just blogged about how to export data to an Excel file dynamically; a simple approach. http://bit.ly/csBS9p [...]

  • http://www.bootoutletstore.co.uk cheap uggs

    Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    ajf 2
    If its common it might be difficult to get, however, be creative and you can usually find something.

Agent Cody Banks 2 Destination London full lenght movie download Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie