1.先去官网:http://npoi.codeplex.com/下载需要引入dll 然后在网站中添加引用。 2.Asp.Net mvc导出方法 ////// 导出Excel /// ///public FileResult Excel() { var alist = WishWallServices.QueryWhere(c => c.status == 1).OrderByDescending(c=>c.WishThumbup.Where(q=>q.status==1).Count()).ToList(); //创建Excel文件的对象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); //添加一个sheet NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("心愿墙"); //给sheet添加第一行的头部标题 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("微信号"); row1.CreateCell(1).SetCellValue("姓名"); row1.CreateCell(2).SetCellValue("心愿标题"); row1.CreateCell(3).SetCellValue("心愿内容"); row1.CreateCell(4).SetCellValue("点赞数"); row1.CreateCell(5).SetCellValue("联系方式"); row1.CreateCell(6).SetCellValue("联系地址"); //....N行 //将数据逐步写入sheet1各个行 for (int i = 0; i < alist.Count; i++) { NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(alist[i].WeiXinInfo.WeiXinName); rowtemp.CreateCell(1).SetCellValue(alist[i].name); rowtemp.CreateCell(2).SetCellValue(alist[i].title); rowtemp.CreateCell(3).SetCellValue(alist[i].content); rowtemp.CreateCell(4).SetCellValue(alist[i].WishThumbup.Where(q=>q.status==1).Count()); rowtemp.CreateCell(5).SetCellValue(alist[i].phone); //....N行 } System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); ms.Seek(0, SeekOrigin.Begin); return File(ms, "application/vnd.ms-excel", "心愿墙.xls"); }