mvc中导出Excel的第二种方法
controller里正常返回actionresult。
在界面上则写:
@{ Layout = null; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=report.xls"); //Content-Disposition is defined in RFC-2183 } <?xml version="1.0" encoding="utf-16"?> <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> <ss:Worksheet ss:Name="基础数据"> <ss:Table> <ss:Row> <ss:Cell><ss:Data ss:Type="String">代表</ss:Data></ss:Cell> <ss:Cell><ss:Data ss:Type="String">医院</ss:Data></ss:Cell> <ss:Cell><ss:Data ss:Type="String">医生</ss:Data></ss:Cell> <ss:Cell><ss:Data ss:Type="String">患者ID</ss:Data></ss:Cell> @foreach(var question in ViewBag.questionList) { <ss:Cell><ss:Data ss:Type="String">@question.content</ss:Data></ss:Cell> } </ss:Row> <ss:Row> <ss:Cell><ss:Data ss:Type="String">N21</ss:Data></ss:Cell> <ss:Cell><ss:Data ss:Type="String">Nam3213e 2</ss:Data></ss:Cell> <ss:Cell><ss:Data ss:Type="String">Na3213me 3</ss:Data></ss:Cell> <ss:Cell><ss:Data ss:Type="String">Nam132131e 4</ss:Data></ss:Cell> </ss:Row> </ss:Table> </ss:Worksheet> </ss:Workbook>
这样的好处是可以在一个文件中加多个sheet。但问题是,现在无法给excel增加样式。