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.Text.RegularExpressions; using System.Web.Http; using Warehouse.DAL.Report; using Warehouse.WebApi.Model; namespace Warehouse.WebApi.QCReport { public class YongzhenJinkeNewController : BaseController { YongzhenJinkeDal DT = new YongzhenJinkeDal(); [Route("api/YongzhenJinkeNew/Search")] [HttpPost] public IHttpActionResult Search(YongzhenJinkeModel model) { try { var PalletNum = ""; if (!string.IsNullOrEmpty(model.PalletNum)) { var palletNumGet = model.PalletNum.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var palletNumList = palletNumGet.Select(x => x.Trim()).Distinct().Where(s => !string.IsNullOrEmpty(s)).ToList(); PalletNum = string.Join("','", palletNumList); PalletNum = "'" + PalletNum + "'"; } //获取数据 var res = DT.Search(model.TestTimeBegin, model.TestTimeEnd, PalletNum); //处理数据 var list = new List(); res.ForEach(x => { list.Add(new { wks_visit_date = x.wks_visit_date.ToString() ?? "-", serial_nbr = x.serial_nbr.ToString() ?? "-", workorder = x.workorder.ToString() ?? "-", ivwks_id = x.ivwks_id.ToString() ?? "-", env_temp = x.env_temp?? "-", sun_ref = x.sun_ref?? "-", ff = x.ff?? "-", //.ToString("yyyy-MM-dd HH:mm:ss") eff = x.eff?? "-", isc = x.isc?? "-", ipm = x.ipm ?? "-", voc = x.voc ?? "-", vpm = x.vpm?? "-", surf_temp = x.surf_temp ?? "-", attr_value = x.attr_value ?? "-", ctm = x.ctm ?? "-", rs = x.rs ?? "-", rsh = x.rsh ?? "-", cell_supplier_code = x.cell_supplier_code ?? "-", serial_status = x.serial_status ?? "-", elwks_id = x.elwks_id ?? "-", ivfile_path = x.ivfile_path ?? "-", pallet_nbr = x.pallet_nbr ?? "-", final_grade = x.final_grade ?? "-", cir_no = x.cir_no ?? "-", sale_order_no = x.sale_order_no.ToString() ?? "-", power_grade = x.power_grade.ToString() ?? "-", contract_no = x.contract_no.ToString() ?? "-", customer_name = x.customer_name.ToString() ?? "-", }); }); //返回数据 return Success(new { data = list }); }catch(Exception e) { } return Failure("查询异常"); } #region 下载 [ Route("api/YongzhenJinkeNew/YongzhenJinkeNewGet")] [HttpPost] public HttpResponseMessage DownloadFinalPowerTestReportGet(YongzhenJinkeModel 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 { "TTIME", "LOT_NUM", "WORK_ORDER_NO", "DEVICENUM", "AMBIENTTEMP", "INTENSITY", "FF", "EFF", "ISC", "IPM", "VOC", "'VPM", "SENSORTEMP", "VC_CELLEFF", "DEC_CTM", "RS", "RSH", "SUPPLIER_NAME", "RESULT", "TYPE", "IV_TEST_KEY", "PALLET_NO", "PRO_LEVEL", "CIR_NO", "SALE_ORDER_NO", "MODEL_NAME", "CONTRACT_NO", "CUSTOMER_NAME", }; //设置第一列列宽 sheet.SetColumnWidth(0, 20 * 256); //设置第一列之后的列宽 for (int i = 1; i < thList.Count; i++) { sheet.SetColumnWidth(i, 10 * 600); } 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 PalletNum = ""; if (!string.IsNullOrEmpty(model.PalletNum)) { var PalletNumGet = model.PalletNum.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var PalletNumList = PalletNumGet.Select(x => x.Trim()).Distinct().Where(s => !string.IsNullOrEmpty(s)).ToList(); PalletNum = string.Join("','", PalletNumList); PalletNum = "'" + PalletNum + "'"; } //获取数据 var res = DT.Search(model.TestTimeBegin, model.TestTimeEnd, PalletNum); foreach (var item in res.ToList()) { var rowCount1 = sheet.LastRowNum + 1; row = sheet.CreateRow(rowCount1); row.CreateCell(0).SetCellValue(item.wks_visit_date.ToString()); row.GetCell(0).CellStyle = style; row.CreateCell(1).SetCellValue(item.serial_nbr.ToString()); row.GetCell(1).CellStyle = style; row.CreateCell(2).SetCellValue(item.workorder.ToString()); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.ivwks_id.ToString()); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.env_temp.ToString()); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.sun_ref.ToString()); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.ff.ToString()); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.eff.ToString()); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.isc.ToString()); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.ipm.ToString()); row.GetCell(9).CellStyle = style; row.CreateCell(10).SetCellValue(item.voc.ToString()); row.GetCell(10).CellStyle = style; row.CreateCell(11).SetCellValue(item.vpm.ToString()); row.GetCell(11).CellStyle = style; row.CreateCell(12).SetCellValue(item.surf_temp.ToString()); row.GetCell(12).CellStyle = style; row.CreateCell(13).SetCellValue(string.IsNullOrEmpty(item.attr_value.ToString()) ? "" : item.pmax.ToString("0")); row.GetCell(13).CellStyle = style; row.CreateCell(14).SetCellValue(string.IsNullOrEmpty(item.ctm.ToString()) ? "" : item.isc.ToString("0")); row.GetCell(14).CellStyle = style; row.CreateCell(15).SetCellValue(string.IsNullOrEmpty(item.rs.ToString()) ? "" : item.voc.ToString("0")); row.GetCell(15).CellStyle = style; row.CreateCell(16).SetCellValue(string.IsNullOrEmpty(item.rsh.ToString()) ? "" : item.ipm.ToString("0")); row.GetCell(16).CellStyle = style; row.CreateCell(17).SetCellValue(string.IsNullOrEmpty(item.cell_supplier_code.ToString()) ? "" : item.vpm.ToString("0")); row.GetCell(17).CellStyle = style; row.CreateCell(18).SetCellValue(string.IsNullOrEmpty(item.serial_status.ToString()) ? "" : item.ff.ToString("0")); row.GetCell(18).CellStyle = style; row.CreateCell(19).SetCellValue(string.IsNullOrEmpty(item.elwks_id.ToString()) ? "" : item.rs.ToString("0")); row.GetCell(19).CellStyle = style; row.CreateCell(20).SetCellValue(string.IsNullOrEmpty(item.ivfile_path.ToString()) ? "" : item.rsh.ToString("0")); row.GetCell(20).CellStyle = style; row.CreateCell(21).SetCellValue(string.IsNullOrEmpty(item.pallet_nbr.ToString()) ? "" : item.eff.ToString("0")); row.GetCell(21).CellStyle = style; row.CreateCell(22).SetCellValue(string.IsNullOrEmpty(item.final_grade.ToString()) ? "" : item.env_temp.ToString("0")); row.GetCell(22).CellStyle = style; row.CreateCell(23).SetCellValue(string.IsNullOrEmpty(item.cir_no.ToString()) ? "" : item.tmod.ToString("0")); row.GetCell(23).CellStyle = style; row.CreateCell(24).SetCellValue(item.sale_order_no.ToString()); row.GetCell(24).CellStyle = style; row.CreateCell(25).SetCellValue(item.power_grade.ToString()); row.GetCell(25).CellStyle = style; row.CreateCell(26).SetCellValue(item.contract_no.ToString()); row.GetCell(26).CellStyle = style; row.CreateCell(27).SetCellValue(item.customer_name.ToString()); row.GetCell(27).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 } }