asp.net MVC 要匯出Excel有幾種方式
這邊介紹兩種
1.運用Gridview
Dim grid As New GridView()
grid.DataSource = srObjList 'obj List or Datatable
grid.DataBind()
Response.ClearContent()
Response.AddHeader("Content-Type", "application/vnd.ms-excel")
Response.AddHeader("content-disposition", "attachment; filename=Totalinventory.xls")
Response.ContentType = "application/excel"
Response.Charset = "big5"
Dim sw As New System.IO.StringWriter()
Dim htw As New HtmlTextWriter(sw)
grid.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()
return view()
2.使用寫好的view.asp
在controaller中
Response.ClearContent()
Response.AddHeader("Content-Type", "application/vnd.ms-excel")
Response.AddHeader("content-disposition", "attachment; filename=ExcelFile.xls")
Response.ContentType = "application/excel"
Response.Charset = "big5"
Return View("excel", srObjList)
在view excel.asp中就照你要將model怎麼處理同樣的寫法
之後就會產生ExcelFile.xls
不過要注意不要引用masterpage
留言