//获取数据库数据
var list = new BLL.Wbll().Show().ToList();
//创建Excel文件的对象
HSSFWorkbook book = new HSSFWorkbook();
//添加一个文件薄
ISheet sheet = book.CreateSheet("sheet1");
//设置初始行
IRow row = sheet.CreateRow(0);
//给文件薄添加头部标题
row.CreateCell(0).SetCellValue("编号");
row.CreateCell(1).SetCellValue("文件名称");
row.CreateCell(2).SetCellValue("文件格式");
row.CreateCell(3).SetCellValue("上传时间");
//将数据逐步写入sheet行
for (int i = 0; i <list.Count ; i++)
{
IRow row1 = sheet.CreateRow(i + 1);
row1.CreateCell(0).SetCellValue(list[i].Wid.ToString());
row1.CreateCell(1).SetCellValue(list[i].Wname);
row1.CreateCell(2).SetCellValue(list[i].Wtype);
row1.CreateCell(3).SetCellValue(list[i].Wtime.ToString());
}
//创建写入流
MemoryStream ms = new MemoryStream();
//写入流
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", "导出数据.xls");
转载于:https://www.cnblogs.com/w-pengchao/p/7845167.html