• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

creating an excel report using asp.net c#

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 asp.net 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

  • 3
1 Solution
for various options working with Excel in ASP.NET, have a look at these comments I made before:

I knw you not necessarilty interested in Excel macros but the same options are valid for you as well.
SirdotsAuthor Commented:
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 ?

"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: http://www.xefteri.com/articles/show.cfm?id=8, http://www.west-wind.com/weblog/posts/76293.aspx. For Excel files, the MIME type which you put into Content-Disposition is "application/vnd.ms-excel"

"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. http://www.vbknowledgebase.com/?Id=156&Desc=Asp.Net-Zip-Multiple-Files

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now