NPOI必须的
在服务器将数据转换为表格,然后修改一下response的minitype 直接输出即可是excel
用Telerik的吗?是Telerik的话,在view中加入 commands.Custom().Text("导出表格").Action("ExportCsv", "MyAddressBook", new { page = 1, orderBy = "~", filter = "~" });
控制器:
1 public ActionResult ExportCsv(int page, string orderBy, string filter) 2 { 3 ClassRepository rep = new ClassRepository(); 4 5 IEnumerable classs = rep.GetClassOrder().AsQueryable().ToGridModel(page, 10, orderBy, string.Empty, filter).Data; 6 MemoryStream output = new MemoryStream(); 7 StreamWriter writer = new StreamWriter(output, Encoding.UTF8); 8 9 10 writer.Write("产品名称,"); 11 writer.Write("产品识别码,"); 12 writer.Write("出库数量,"); 13 writer.WriteLine(); 14 foreach (Classes c in classs) 15 { 16 writer.Write(c.id); 17 writer.Write(","); 18 19 writer.Write("\""); 20 writer.Write(c.classname); 21 writer.Write("\""); 22 writer.Write(","); 23 24 writer.Write("\""); 25 writer.Write(c.classpersons); 26 writer.Write("\""); 27 writer.Write(","); 28 29 writer.WriteLine(); 30 } 31 32 writer.Flush(); 33 output.Position = 0; 34 35 return File(output, "text/comma-separated-values", "address.csv"); 36 }