1 //<summary> 2 //导出考试成绩表 3 //</summary> 4 //<param name="ksid">考试ID</param> 5 //<returns></returns> 6 public FileResult exportExam(int ksid) 7 { 8 var Service = new ExamPeopleService(); 9 var ksStatistics = new ScorePeopleManageDTO(); 10 11 //创建Excel文件对象 12 var book = new NPOI.HSSF.UserModel.HSSFWorkbook(); 13 //添加一个sheet 14 var sheet1 = book.CreateSheet("sheet1"); 15 16 var row1 = sheet1.CreateRow(0); 17 ICell cell = row1.CreateCell(0); 18 ICellStyle style = book.CreateCellStyle(); 19 //设置单元格的样式:水平对齐居中 20 style.Alignment = HorizontalAlignment.Center; 21 //新建一个字体样式对象 22 IFont font = book.CreateFont(); 23 //设置字体加粗样式 24 font.Boldweight = short.MaxValue; 25 //使用SetFont方法将字体样式添加到单元格样式中 26 style.SetFont(font); 27 //将新的样式赋给单元格 28 cell.CellStyle = style; 29 row1.Height = 30 * 20; 30 31 cell.SetCellValue("姓名"); 32 sheet1.SetColumnWidth(0, 10 * 256); 33 cell = row1.CreateCell(1); 34 cell.CellStyle = style; 35 cell.SetCellValue("登录名(学号)"); 36 sheet1.SetColumnWidth(1, 30 * 256); 37 cell = row1.CreateCell(2); 38 cell.CellStyle = style; 39 cell.SetCellValue("班级"); 40 sheet1.SetColumnWidth(2, 30 * 256); 41 cell = row1.CreateCell(3); 42 cell.CellStyle = style; 43 cell.SetCellValue("成绩"); 44 sheet1.SetColumnWidth(3, 10 * 256); 45 cell = row1.CreateCell(4); 46 cell.CellStyle = style; 47 cell.SetCellValue("排名"); 48 sheet1.SetColumnWidth(4, 10 * 256); 49 var result = Service.DoExamExportToStream(ksid); 50 var tempPath = Request.MapPath("/Content/temp" + DateTime.Now.ToString("yyyyMMddHHmmss考试导出") + ".xlsx"); 51 for (int i = 0; i < result.Count; i++) 52 { 53 if (result[i].score == null) 54 result[i].score = 0.00M; 55 if (result[i].ranking == null) 56 result[i].ranking = 0; 57 var rowtemp = sheet1.CreateRow(i + 1); 58 rowtemp.CreateCell(0).SetCellValue(result[i].userName); 59 rowtemp.CreateCell(1).SetCellValue(result[i].userLoginName); 60 rowtemp.CreateCell(2).SetCellValue(result[i].deptName); 61 rowtemp.CreateCell(3).SetCellValue(result[i].score.ToString()); 62 rowtemp.CreateCell(4).SetCellValue(result[i].ranking.ToString()); 63 } 64 //写入客户端 65 var ms = new System.IO.MemoryStream(); 66 book.Write(ms); 67 ms.Seek(0, System.IO.SeekOrigin.Begin); 68 return File(ms, "application/vnd.ms-excel", result[0].ksmc + "成绩" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); 69 }
时间: 2024-10-06 04:51:44