DharaPPatel
DharaPPatel

Reputation: 12753

Export data into multiple worksheet in MVC 3

i am working on MVC application i have to export my table data and i am using following code :

     public ActionResult ExportData()
    {
        GridView gv = new GridView();
        gv.DataSource = db.Studentrecord.ToList();
        gv.DataBind();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gv.RenderControl(htw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();

        return RedirectToAction("StudentDetails");
    }

this creates single worksheet i want another worksheet where i will have other table data.Please help how to export data in multiple worksheet ?

Upvotes: 0

Views: 2866

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78555

How about using a 3rd party library instead? This is free: https://code.google.com/p/excellibrary/

string file = "C:\\newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
Worksheet worksheet2 = new Worksheet("Second Sheet");
workbook.Worksheets.Add(worksheet);
workbook.Worksheets.Add(worksheet2);
workbook.Save(file);

This provides a much more fluid user experience as you are actually creating a real Excel file, not just a HTML file which Excel happens to read.

Upvotes: 2

Related Questions