/// <summary>
/// 导出
/// </summary>
/// <returns></returns>
public FileResult xiazai()
{
//查询到数据
var result = bll.showinfo();
if (result != null || result.Count > 0)
{
/*HSSFWorkbook:
*引用文件 NPOI.dll
引用命名空间 NPOI.HSSF.UserModel*/
//创建Excel文件的对象
HSSFWorkbook book = new HSSFWorkbook();
/*ISheet:
*引用文件 NPOI.dll
引用命名空间 NPOI.SS.UserModel*/
//添加一个sheet
ISheet sheet = book.CreateSheet("sheet1");
//给sheet1添加第一行的头部标题
IRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("编号");
row.CreateCell(1).SetCellValue("名称");
row.CreateCell(2).SetCellValue("类型");
row.CreateCell(3).SetCellValue("时间");
//将数据逐步写入sheet1各个行
for (int i = 0; i < result.Count; i++)
{
IRow rows = sheet.CreateRow(i + 1);
rows.CreateCell(0).SetCellValue(result[i].ID);
rows.CreateCell(1).SetCellValue(result[i].Name);
rows.CreateCell(2).SetCellValue(result[i].Infotype);
rows.CreateCell(3).SetCellValue(result[i].Infotime);
}
//写入到客户端
//添加命名空间 System.IO
MemoryStream rs = new MemoryStream();
book.Write(rs);
rs.Seek(0, SeekOrigin.Begin);
return File(rs, "application/ved-excel", "学生信息.xls");
}
else
{
MemoryStream rs = new MemoryStream();
rs.Seek(0, SeekOrigin.Begin);
return File(rs, "application/ved-excel", "学生信息.xls");
}
}