public FileResult ExcelFile() {
// new new standby list List <Student> = allList new new List <Student> ();
// Get all data in the out var slist = bll.GetAllList (); Model Student = new new Student ();
// determines whether there is a recording IF (slist =! Null && slist.Count () =! 0 ) {
// If the record exists for each record will be transmitted to the target entity, and then added to the list allList the foreach ( var Item in slist) { model.StudentName = item.StudentName ; model.Sex = item.Sex; model.Birthday = item.Birthday; allList.Add(model); } }
// Create an Excel workbook HSSFWorkbook Book = new new HSSFWorkbook ();
// create a file called Sheet1 of the Excel tables in the workbook ISheet sheet = book.CreateSheet ( " Sheet1 " );
// in the sheet in a new new objects of the first row, and three data padding the IRow row = sheet.CreateRow ( 0 ); row.CreateCell(0).SetCellValue("姓名"); row.CreateCell(1).SetCellValue("性别"); row.CreateCell ( 2 ) .SetCellValue ( " Date of Birth " );
// get the list to add all the data into the allList the sheet for ( int I = 0 ; I <allList.Count; I ++ ) { IRow rowtemp = sheet.CreateRow(i+1); rowtem.CreateCell(0).SetCellValue(allList[i].StudentName.ToString()); rowtemp.CreateCell(2).SetCellValue(allList[i].Sex.ToString()=="0"?"男":"女"); rowtemp.CreateCell(3).SetCellValue(allList[i].Birthday.ToString()); }
// Create a memory stream the MemoryStream MS = new new the MemoryStream ();
save // the default path book.write (MS);
// set the position of the current stream ms.Seek ( 0 , SeekOrigin.Begin); DateTime dt = DateTime.Now;
//确定文件名 string dateTime = dt.ToString("yyyyMMddHHmmss"); string fileName = "结果"+dateTime+".xls"; return File(ms,"application/vnd.ms-excel",fileName); }