0

I am working on a web application which consists of some reports. These report is generated on a location wise data

The web page (Report)

The report should have a feature to be extracted to an excel file. For which I use render control.

private void ExprotToExcel()
    {
        Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=ExportData.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        mainReport.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();

    }

The code works fine. Now the problem is that the user should also be able to export the report based on multiple locations. What I mean is that suppose there are 2 locations for which the report needs to be extracted. The report should be exported to the excel file directly in multiple sheets on the same workbook. I can use the above code to do that but I need multiple sheets and not multiple workbook.

Can anyone please guide me on the right path?

Edit : Forgot to mention that I have used multiple gridviews, data view and label controls on the webpage.

Ankit Suhail
  • 2,045
  • 21
  • 32
  • If there are not a lot of locations, you can present them with a checkbox list instead of drop down for locations. – StingyJack Jun 02 '14 at 13:45
  • @StingyJack yes, Its been done.I am using checkbox list for the same.Here, I just presented the report which has been prepared and it's bounded to use just one location. – Ankit Suhail Jun 02 '14 at 13:46

1 Answers1

0

You can use a library for this:

http://epplus.codeplex.com/

Beware of NOT use Office Interop libraries in a web environment.

http://support.microsoft.com/kb/257757

Oscar
  • 13,594
  • 8
  • 47
  • 75
  • I tried the library, But because of my limited asp.net skills I am unaware on how to print the webpage to the excel sheet. The examples that I got for EEPLUS were for extracting a datatable, whereas I am recalling my webpage again and again with different parameters and exporting to excel on each iteration. It will be really helpful if you can just provide me with a small hint on how to accomplish this task. – Ankit Suhail Jun 02 '14 at 13:50
  • 2
    You don't have to get the data from your UI controls, but from your bussines layer. Create a method in your app domain code which extract the desired data and then create the file. This function will be triggered from UI. – Oscar Jun 02 '14 at 13:52
  • Alright, Got your point. I can get the data from the BAL but how can I apply the formatting to the exported data. Any links for this? – Ankit Suhail Jun 02 '14 at 13:55
  • You can read the documentation of EPPLUS library for more info about how to work with the library. BTW, this is just one of many available for free in the web. – Oscar Jun 02 '14 at 14:03