public async Task <IActionResult> ExamRoomExportAsync([FromForm] IFormCollection fm) { string CLIENTNAME = fm["CLIENTNAME"].ToString(); string DATE1 = fm["DATE1"].ToString(); string DATE2 = fm["DATE2"].ToString(); string FileName = string.Empty; string SheetName = string.Empty; string[] temArr = { }; DataTable boundTable = new DataTable(); SheetName = "维护及时率"; FileName = SheetName + $"{DateTime.Now:yyyyMMddHHmmssfff}.xls"; string[] temArr1 = { "FACTORY", "SN1", "SN2", "COMMAND_CODE", "REPAIRBUGTYPE", "SENDPERSON", "PERSON", "ERR_DESCRIBE", "SCAN_TIME", "POSTION_ITEMVERSION", "SENDDATE", "POSITION_CODE", "BAD_ITEM_CODE", "REPAIRDATE", "SECONDTIME", "SNRESULT", "LINE_CODE", "RECEIVE_PERSON", "SYSDATE" }; temArr = temArr1; boundTable = _repairRepository.GetRepairListTB(CLIENTNAME, DATE1, DATE2); List <string> lstTitle = new List <string>(temArr); IWorkbook book = new HSSFWorkbook(); //ISheet sheet = book.CreateSheet("Sheet1"); ISheet sheet = book.CreateSheet(SheetName); IRow rowTitle = sheet.CreateRow(0); ICellStyle style = book.CreateCellStyle(); style.VerticalAlignment = VerticalAlignment.Center;//垂直居中 //左右位置 CellHorizontalAlignment.位置值 style.Alignment = HorizontalAlignment.Center; style.BorderTop = BorderStyle.Thin; style.BorderBottom = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; ICellStyle headStyle = book.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; IFont font = book.CreateFont(); font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); headStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 headStyle.Alignment = HorizontalAlignment.Center; headStyle.BorderTop = BorderStyle.Thin; headStyle.BorderBottom = BorderStyle.Thin; headStyle.BorderLeft = BorderStyle.Thin; headStyle.BorderRight = BorderStyle.Thin; //sheet.AutoFitColumns();//自动适应所有列宽 //sheet.AutoFitRows();//自动适应所有行高 int rowIndex = 0; for (int i = 0; i < lstTitle.Count; i++) { //sheet.SetColumnWidth(i, 14 * 256); rowTitle.CreateCell(i).SetCellValue(lstTitle[i]); rowTitle.GetCell(i).CellStyle = headStyle; rowIndex = 1; } for (int i = 0; i < boundTable.Rows.Count; i++) { IRow row = sheet.CreateRow(i + 1); DataRow dr = boundTable.Rows[i]; for (int j = 0; j < boundTable.Columns.Count; j++) { ICell cell = row.CreateCell(j); cell.CellStyle = style; cell.SetCellValue(dr[j].ToString()); } rowIndex++; } // // 表尾 // if (true) // { //// HSSFRow headerRow = (HSSFRow)sheet.CreateRow(rowIndex); //// headerRow.Height = 4 * 256; //// headerRow.CreateCell(0).SetCellValue(@"1.记录人快速填写机台代码,开始时间,停机代号,停止时间,签名,责任线长技术员分析停机原因 ////2.停机事项表内部包含代码事件,可单独编码记录并提报(自加编码优先按26英文字符顺序添加,超出从A字母重计加“1”示例:A1.B1….) ////3.自加编码规则要一直沿用前一页标准一致"); //定义表头 //// sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, boundTable.Columns.Count - 1)); //// HSSFCellStyle headStyle1 = (HSSFCellStyle)book.CreateCellStyle(); //// headStyle1.WrapText = true; //// headStyle1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; //// headStyle1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //// headerRow.GetCell(0).CellStyle = headStyle; // HSSFRow headerRow1 = (HSSFRow)sheet.CreateRow(rowIndex + 1); // headerRow1.Height = 1 * 256; // headerRow1.CreateCell(0).SetCellValue(@"送修总数: 维修总数: 送修及时数: "); //定义表头 // sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex + 1, rowIndex + 1, 0, boundTable.Columns.Count - 1)); // HSSFCellStyle headStyle2 = (HSSFCellStyle)book.CreateCellStyle(); // headStyle2.WrapText = true; // headStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; // headStyle2.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; // headerRow1.GetCell(0).CellStyle = headStyle2; // } MemoryStream ms = new MemoryStream(); book.Write(ms); ms.Seek(0, SeekOrigin.Begin); //return File(ms, "application/vnd.ms-excel", "考场总表导出.xls"); return(File(ms, "application/vnd.ms-excel", FileName)); }