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.Http; using Warehouse.DAL.Report; using Warehouse.WebApi.Model; namespace Warehouse.WebApi { public class WorkLotNewController : BaseController { //工单条码 WorkLotDal DT = new WorkLotDal(); [Route("api/WorkLotNew/WorkLot")] [HttpPost] public IHttpActionResult QueryInfo(WorkLotModel model) { try { var res = DT.WorkLot(model.TestTimeBegin, model.TestTimeEnd, model.WorkShop, model.Sales, model.WorkOrder); var list = new List(); res.ForEach(x => { list.Add(new { descriptions = x.descriptions.ToString() ?? "-", sale_order = x.sale_order.ToString() ?? "-", workorder = x.workorder.ToString() ?? "-", sn_qty = x.sn_qty.ToString() ?? "-", start_serial = x.start_serial.ToString() ?? "-", end_serial = x.end_serial.ToString() ?? "-", OEMcustomer = x.OEMcustomer.ToString() ?? "-", create_date = x.create_date.ToString() ?? "-", wo_create_date = x.wo_create_date.ToString() ?? "-", product_code = x.product_code.ToString() ?? "-", }); }); //返回数据 return Success(new { data = list }); } catch (Exception e) { //TODO 记录日志 } return Failure("查询异常"); } #region 下载 [HttpPost, Route("api/WorkLotNew/WorkLotNewReportGet")] public HttpResponseMessage WorkLotNewReportGet(WorkLotModel 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 { "车间", "订单", "工单总数量", "生码数量", "起始序列号", "最终序列号", "客户", "生码时间", "工单创建时间", "产品族", }; //设置第一列列宽 // 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 //获取数据 var res = DT.WorkLot(model.TestTimeBegin, model.TestTimeEnd, model.WorkShop, model.Sales, model.WorkOrder); foreach (var item in res.ToList()) { var rowCount1 = sheet.LastRowNum + 1; row = sheet.CreateRow(rowCount1); row.CreateCell(0).SetCellValue(item.descriptions.ToString()); row.GetCell(0).CellStyle = style; row.CreateCell(1).SetCellValue(item.sale_order.ToString()); row.GetCell(1).CellStyle = style; row.CreateCell(2).SetCellValue(item.workorder.ToString()); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.sn_qty.ToString()); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.start_serial.ToString()); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.end_serial.ToString()); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.OEMcustomer.ToString()); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.create_date.ToString()); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.wo_create_date.ToString()); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.product_code.ToString()); row.GetCell(9).CellStyle = style; } MemoryStream ms = new MemoryStream(); //创建内存流用于写入文件 hssfworkbook.Write(ms);//将Excel写入流 ms.Flush(); ms.Position = 0; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(ms); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "工单条码登记报表.xls" }; return result; } catch (Exception) { } return new HttpResponseMessage(HttpStatusCode.NoContent); } #endregion } }