creating an excel report using c#

Posted on 2011-04-19
Last Modified: 2012-05-11
I have a method which generates a csv file when I pass in a gridview to it. I will want to create a report that looks like the attached screen shot. How can I do this to get the same look of the report?
I have also added the code that is generating the csv file. Is there a way to program excel in that can get me a dataset and display this result in an excel file that is already formatted.

public  void Export(string fileName, GridView gv)
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                //  Create a table to contain the grid
                Table table = new Table();

                //  include the gridline settings
                table.GridLines = gv.GridLines;
                //  add the header row to the table
                if (gv.HeaderRow != null)

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)

                //  add the footer row to the table
                if (gv.FooterRow != null)

                //  render the table into the htmlwriter

                //  render the htmlwriter into the response

Open in new window

Question by:Sirdots
    LVL 30

    Expert Comment

    for various options working with Excel in ASP.NET, have a look at these comments I made before:
    LVL 30

    Expert Comment

    I knw you not necessarilty interested in Excel macros but the same options are valid for you as well.

    Author Comment

    Thanks MiandaT. I want my users to be able to save an excel generated file on there computer and be able to save multiple excel files like 4 files on the same computer generated with the click on a button. Do you think I can achieve this via the web. Also, if I understand this very well, you are recommending a third party tool right ?

    LVL 30

    Accepted Solution

    "my users to be able to save an excel generated file"
    Yes. Using the tools above, you can generate the Excel file on the server from whatever data you have. How to send a file to the browser from the server:, For Excel files, the MIME type which you put into Content-Disposition is "application/"

    "be able to save multiple excel files like 4 files on the same computer generated with the click on a button"
    sending multiple files to the user will be tricky using a single link. As far as I know... you can send only one file at a time in the response stream. you may consider 4 links OR otherwise, zipping the 4 files on the server before sending a single zip file to the user.

    you are recommending a third party tool right
    Indeed. It's easier to let someone else focus on the nitty gritties of the various excel file formats and all that. you focus on what your applicaiton should be providing to the user.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now