We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

creating an excel report using asp.net c#

Sirdots
Sirdots asked
on
Medium Priority
597 Views
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 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
Comment
Watch Question

CERTIFIED EXPERT

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

https://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26918336.html?cid=1065#a35277606
CERTIFIED EXPERT

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

Author

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 ?

CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.