using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Web; using System.Web.Http; using Warehouse.WebApi.Model; namespace Warehouse.WebApi.Tools { [RoutePrefix("api/download")] public class ExcelDownload : BaseController { [HttpPost, Route("DownloadFinalPowerTestReport")] public IHttpActionResult DownloadFinalPowerTestReport(DownloadFinalPowerTestReportModel model) { try { HSSFWorkbook hssfworkbook = new HSSFWorkbook(); hssfworkbook.CreateSheet("最终功率测试报表"); ICellStyle style = hssfworkbook.CreateCellStyle(); //设置单元格的样式:水平对齐居中 style.Alignment = HorizontalAlignment.Center; ISheet sheet = null; sheet = hssfworkbook.GetSheetAt(0); IRow row = null; #region 添加表头 var thList = new List { "柜号", "托盘号", "包装顺序", "工单号", "订单号", "组件序列号", "测试日期时间", "测试机台号", "功率档位", "电流档位", "外观等级", "EL等级", "最终等级", "Pmax", "ISC", "VOC", "IPM", "VPM", "FF", "RS", "RSH", "EFF", "Envtmp", "Tmod", "电池片厂家", "单片功率", "电池片颜色", "电池片等级", "电池片类型", "电池片效率档", "托盘状态", "片源用量", "额定功率", "投入产出比", "晶体类型", "片源规格" }; //设置第一列列宽 sheet.SetColumnWidth(0, 20 * 256); //设置第一列之后的列宽 for (int i = 1; i < thList.Count; i++) { sheet.SetColumnWidth(i, 10 * 256); } var rowCount = sheet.LastRowNum; row = sheet.CreateRow(rowCount); //添加表头并设置样式字体居中 for (int i = 0; i < thList.Count; i++) { row.CreateCell(i).SetCellValue(thList[i]); row.GetCell(i).CellStyle = style; } #endregion foreach (var item in model.DownloadDataList.ToList()) { var rowCount1 = sheet.LastRowNum + 1; row = sheet.CreateRow(rowCount1); row.CreateCell(0).SetCellValue(item.Name); row.GetCell(0).CellStyle = style; row.CreateCell(1).SetCellValue(item.ManagerName); row.GetCell(1).CellStyle = style; row.CreateCell(2).SetCellValue(item.IndustryName); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.TodayMessage); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.TodayUseScore); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.MonthUseScore); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.AllUseScore); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.RemainScore); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.RemainMessage); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.MonthGetScore); row.GetCell(9).CellStyle = style; row.CreateCell(10).SetCellValue(item.AllGetScore); row.GetCell(10).CellStyle = style; row.CreateCell(11).SetCellValue(item.RemainNoPScore); row.GetCell(11).CellStyle = style; } var date = DateTime.Now.ToString("yyyy-MM-dd"); Random r = new Random(); int num = r.Next(100); var path = HttpContext.Current.Server.MapPath($"~/最终功率测试报表/{date}最终功率测试报表{num}.xls"); //创建excel文件 if (Directory.Exists(HttpContext.Current.Server.MapPath($"~/最终功率测试报表"))) { FileStream file = new FileStream(path, FileMode.Create); //把数据写入excel文件中 hssfworkbook.Write(file); file.Close(); } else { var files = Directory.CreateDirectory(HttpContext.Current.Server.MapPath($"~/最终功率测试报表")); FileStream file = new FileStream(path, FileMode.Create); //把数据写入excel文件中 hssfworkbook.Write(file); file.Close(); } //下载 string fileName = Path.GetFileName(path); var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; //类型:下载 resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; //application/octet-stream --扩展名全类型格式 resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); resp.Content.Headers.ContentLength = stream.Length; //return resp; } catch (Exception) { } return Failure("下载失败"); } } }