将数据导出到MVC 3中的多个工作表中

时间:2013-07-16 11:05:47

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 asp.net-mvc-2

我正在研究MVC应用程序我必须导出我的表数据,我正在使用以下代码:

     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");
    }

这创建了单个工作表我想要另一个工作表,我将有其他表数据。请帮助如何在多个工作表中导出数据?

1 个答案:

答案 0 :(得分:2)

如何使用第三方库?这是免费的: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);

当您实际创建一个真实的Excel文件时,这提供了更加流畅的用户体验,而不仅仅是Excel恰好阅读的HTML文件。