#region 构建Excel文档 //创建Excel文件的对象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.CellStyle style = book.CreateCellStyle(); style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; ////设置边框格式 style.BorderTop = NPOI.SS.UserModel.CellBorderType.THIN; style.BorderLeft = NPOI.SS.UserModel.CellBorderType.THIN; style.BorderRight = NPOI.SS.UserModel.CellBorderType.THIN; style.BorderBottom = NPOI.SS.UserModel.CellBorderType.THIN; //添加一个sheet NPOI.SS.UserModel.Sheet sheet1 = book.CreateSheet("Sheet1"); //获取list数据 NPOI.SS.UserModel.Row row1 = sheet1.CreateRow(0); SetColcumns(book, sheet1, style, 0, row1, "编号"); SetColcumns(book, sheet1, style, 1, row1, "人"); SetColcumns(book, sheet1, style, 2, row1, "是否本人"); for (int i = 0; i < 34; i++) { sheet1.SetColumnWidth(i, 20 * 256); } //将数据逐步写入sheet1各个行 int num = 0; foreach (var item in cooperates) { NPOI.SS.UserModel.Row rowtemp = sheet1.CreateRow(num + 1); SetColcumns(book, sheet1, style, 0, rowtemp, item.ID.ToString()); SetColcumns(book, sheet1, style, 1, rowtemp, item.UserName); SetColcumns(book, sheet1, style, 2, rowtemp, item.IsSelfStrc); num++; } byte[] data = null; using (MemoryStream ms = new MemoryStream()) { book.Write(ms); ms.Flush(); ms.Position = 0; data = ms.GetBuffer(); } return File(data, "application/vnd.ms-excel", "信息" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); #endregion