• 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.

Thanks.
public  void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "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)
                {
                    GrdviewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);               
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GrdviewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GrdviewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
            
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

Open in new window

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

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26918336.html?cid=1065#a35277606
0
 
MlandaTCommented:
I knw you not necessarilty interested in Excel macros but the same options are valid for you as well.
0
 
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 ?

0
 
MlandaTCommented:
"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.
0

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