1、条件过滤
Expression<Func<T_AH_ARCHIVES, bool>> filter = it => true;
2、查询数据
var MFilelist = ModelHelper.EntityModel.Query<T_AH_ARCHIVES>().Where(filter).OrderByDescending(item => item.FILEDATE).ToList();
3、创建文件表格
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
4、创建栏目
NPOI.SS.UserModel.IRow row = sheet1.CreateRow(0);
row.CreateCell(0).SetCellValue("编号");
row.CreateCell(1).SetCellValue("档案编号");
row.CreateCell(2).SetCellValue("档案名称");
row.CreateCell(3).SetCellValue("档案类型");
5、写入数据
System.IO.MemoryStream ms = new System.IO.MemoryStream();
foreach (var list in MFilelist)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(num);
rowtemp.CreateCell(0).SetCellValue((num).ToString());
rowtemp.CreateCell(1).SetCellValue(list.UNDERVOLUMENAME + "-" + list.CUSTOMIZENUMBER ?? "".ToString());
rowtemp.CreateCell(2).SetCellValue(list.FILENAMES ?? "".ToString());
rowtemp.CreateCell(3).SetCellValue(list.ARCHVESTYPEID ?? "".ToString());
num = num + 1;
}
6、下载出来
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", "管理员归档列表信息" + DateTime.Now.ToString("yyyyMMddHHmmssfff")));
Response.BinaryWrite(ms.ToArray());
book = null;
ms.Close();
ms.Dispose();
return Content("");