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 BatchRateNewController :BaseController { BatchRateDal DT = new BatchRateDal(); [Route("api/BatchRateNew/BatchRateNew")] [HttpPost] public IHttpActionResult Repair(BatchRateModel model) { try { var res = DT.BatchRate(model.Sales, model.WorkOrder, model.WorkShop); var list = new List(); res.ForEach(x => { list.Add(new { descriptions = x.descriptions.ToString() ?? "-", sale_order = x.sale_order.ToString() ?? "-", OEMcustomer = x.OEMcustomer.ToString() ?? "-", workorder = x.workorder.ToString() ?? "-", plan_qty=x.plan_qty.ToString()??"-", qty = x.qty.ToString() ?? "-", rk= x.rk.ToString() ?? "-", zjbf= x.zjbf.ToString() ?? "-", smbf = x.smbf.ToString() ?? "-", sm = x.sm.ToString() ?? "-", hj= x.hj.ToString() ?? "-", dc = x.dc.ToString() ?? "-", cy = x.cy.ToString() ?? "-", CYHJY = x.CYHJY.ToString() ?? "-", zk= x.zk.ToString() ?? "-", qx = x.qx.ToString() ?? "-", QJ = x.QJ.ToString() ?? "-", bz = x.bz.ToString() ?? "-", rkl= x.rkl.ToString() ?? "-", minT= x.minT.ToString() ?? "-", finish_time = x.finish_time.ToString() ?? "-", maxT = x.maxT.ToString() ?? "-", }); }); return Success(new { data = list }); } catch(Exception e) { } return Failure("查询异常"); } #region 下载 [ Route("api/BatchRateNew/BatchReportGet")] [HttpPost] public HttpResponseMessage DownloadBatchReportGet(BatchRateModel 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 * 500); } 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.BatchRate(model.Sales, model.WorkOrder, model.WorkShop); 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.OEMcustomer.ToString()); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.workorder.ToString()); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.plan_qty.ToString()); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.qty.ToString()); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.rk.ToString()); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.zjbf.ToString()); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.smbf.ToString()); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.sm.ToString()); row.GetCell(9).CellStyle = style; row.CreateCell(10).SetCellValue(item.hj.ToString()); row.GetCell(10).CellStyle = style; row.CreateCell(11).SetCellValue(item.dc.ToString()); row.GetCell(11).CellStyle = style; row.CreateCell(12).SetCellValue(item.cy.ToString()); row.GetCell(12).CellStyle = style; row.CreateCell(13).SetCellValue(item.CYHJY.ToString()); row.GetCell(13).CellStyle = style; row.CreateCell(14).SetCellValue(item.zk.ToString()); row.GetCell(14).CellStyle = style; row.CreateCell(15).SetCellValue(item.qx.ToString()); row.GetCell(15).CellStyle = style; row.CreateCell(16).SetCellValue(item.QJ.ToString()); row.GetCell(16).CellStyle = style; row.CreateCell(16).SetCellValue(item.bz.ToString()); row.GetCell(16).CellStyle = style; row.CreateCell(17).SetCellValue(item.rkl.ToString()); row.GetCell(17).CellStyle = style; row.CreateCell(18).SetCellValue(item.minT.ToString()); row.GetCell(18).CellStyle = style; row.CreateCell(19).SetCellValue(item.finish_time.ToString()); row.GetCell(19).CellStyle = style; row.CreateCell(20).SetCellValue(item.maxT.ToString()); row.GetCell(20).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 } }