MVC3 C# Export to Excel

前端 未结 2 1404
傲寒
傲寒 2021-01-26 08:15

I am trying to export data from database to excel 2007 file.

I just want to change the header of html file to excel 2007 file.

I format the data into a table a

相关标签:
2条回答
  • 2021-01-26 08:54

    How about just generating a .csv? Why do you need a full-blown native Excel file? Another easy option is to just generate HTML content and then set the output content type to excel. Excel will open it and format it, its a quick shortcut, but it works.

    0 讨论(0)
  • 2021-01-26 08:56

    I use this form :

    using(System.IO.MemoryStream ms = /*Include Excel File*/) {
      ControllerContext.HttpContext.Response.Clear();
      ControllerContext.HttpContext.Response.AddHeader("cache-control", "private");
      ControllerContext.HttpContext.Response.AddHeader("Content-disposition", "attachment; filename=" + filename + ";");
      ControllerContext.HttpContext.Response.AddHeader("Content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      ms.WriteTo(ControllerContext.HttpContext.Response.OutputStream);
    }
    return null;
    
    0 讨论(0)
提交回复
热议问题