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.DAL.YL; using Warehouse.WebApi.Model; namespace Warehouse.WebApi.YL { public class DataQueryNewController : BaseController { DataQueryDal DT = new DataQueryDal(); [Route("api/DataQueryNew/DataQuery")] [HttpPost] public IHttpActionResult Dataquery(DataQueryModel model) { try { var serialNum = ""; if (!string.IsNullOrEmpty(model.SerialNum)) { var serialNumGet = model.SerialNum.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var serialNumList = serialNumGet.Select(x => x.Trim()).Distinct().Where(s => !string.IsNullOrEmpty(s)).ToList(); serialNum = string.Join("','", serialNumList); serialNum = "'" + serialNum + "'"; } var res = DT.Query(model.WorkOrder, model.TestTimeBegin, model.TestTimeEnd, serialNum); //处理数据 var list = new List(); res.ForEach(x => { list.Add(new { 组件编号 = x.组件编号.ToString() ?? "-", 车间编号 = x.车间编号.ToString() ?? "-", 工单类型 = x.工单类型.ToString() ?? "-", 工单号 = x.工单号.ToString() ?? "-", 客户名称 = x.客户名称.ToString() ?? "-", 片源批号 = x.片源批号.ToString() ?? "-", 片源厂商 = x.片源厂商.ToString() ?? "-", //.ToString("yyyy-MM-dd HH:mm:ss") 片源档位 = x.片源档位.ToString() ?? "-", 电池尺寸 = x.电池尺寸.ToString() ?? "-", 分选操作人员 = x.分选操作人员.ToString() ?? "-", 片源规格 = x.片源规格.ToString() ?? "-", 片源用量 = x.片源用量.ToString() ?? "-", 单片功率 = x.单片功率.ToString() ?? "-", 电池种类 = x.电池种类 ?? "-", 焊接时间 = x.焊接时间 ?? "-", 机台编号 = x.机台编号 ?? "-", 助焊剂厂商 = x.助焊剂厂商 ?? "-", 助焊剂批号 = x.助焊剂批号 ?? "-", 操作人员 = x.操作人员 ?? "-", 操作班次 = x.操作班次 ?? "-", 电池片效率 = x.电池片效率 ?? "-", 互联条厂商 = x.互联条厂商 ?? "-", 互联条规格 = x.互联条规格 ?? "-", 叠层时间 = x.叠层时间 ?? "-", 叠层机台编号 = x.叠层机台编号.ToString() ?? "-", 高透EVA规格 = x.高透EVA规格.ToString() ?? "-", 高透EVA批号 = x.高透EVA批号.ToString() ?? "-", 玻璃厂商 = x.玻璃厂商.ToString() ?? "-", 玻璃规格 = x.玻璃规格.ToString() ?? "-", 玻璃批号 = x.玻璃批号.ToString() ?? "-", 汇流条厂商 = x.汇流条厂商.ToString() ?? "-", 汇流条规格 = x.汇流条规格.ToString() ?? "-", 叠层操作人员 = x.叠层操作人员.ToString() ?? "-", 叠层操作班次 = x.叠层操作班次.ToString() ?? "-", EVA厂商 = x.EVA厂商.ToString() ?? "-", 普通EVA规格 = x.普通EVA规格.ToString() ?? "-", 背板规格 = x.背板规格.ToString() ?? "-", 背板厂商 = x.背板厂商.ToString() ?? "-", 背板批号 = x.背板批号.ToString() ?? "-", 汇流条批号 = x.汇流条批号.ToString() ?? "-", 前EL测试时间 = x.前EL测试时间.ToString() ?? "-", 前EL机台编号 = x.前EL机台编号.ToString() ?? "-", 前EL测试结果 = x.前EL测试结果.ToString() ?? "-", 层压时间 = x.层压时间.ToString() ?? "-", 层压机台编号 = x.层压机台编号.ToString() ?? "-", 层压操作人员 = x.层压操作人员.ToString() ?? "-", 层压操作班次 = x.层压操作班次.ToString() ?? "-", 层压后检验时间 = x.层压后检验时间.ToString() ?? "-", 层压后检验操作人员 = x.层压后检验操作人员.ToString() ?? "-", 层压后检验操作班次 = x.层压后检验操作班次.ToString() ?? "-", 装框时间 = x.装框时间.ToString() ?? "-", 装框机台编号 = x.装框机台编号.ToString() ?? "-", 线盒厂家 = x.线盒厂家.ToString() ?? "-", 线盒规格 = x.线盒规格.ToString() ?? "-", 线盒批号 = x.线盒批号.ToString() ?? "-", 线盒胶厂商 = x.线盒胶厂商.ToString() ?? "-", 线盒胶批号 = x.线盒胶批号.ToString() ?? "-", 装框操作人员 = x.装框操作人员.ToString() ?? "-", 装框操作班次 = x.装框操作班次.ToString() ?? "-", 接线盒拍照时间 = x.接线盒拍照时间.ToString() ?? "-", 型材厂商 = x.型材厂商.ToString() ?? "-", 型材规格 = x.型材规格.ToString() ?? "-", 型材批号 = x.型材批号.ToString() ?? "-", 型材胶厂商 = x.型材胶厂商.ToString() ?? "-", 型材胶批号 = x.型材胶批号.ToString() ?? "-", IV机台编号 = x.IV机台编号.ToString() ?? "-", 测试功率 = x.测试功率.ToString() ?? "-", 标称功率 = x.标称功率.ToString() ?? "-", 测试时间 = x.测试时间.ToString() ?? "-", 测试电流 = x.测试电流.ToString() ?? "-", 后EL测试时间 = x.后EL测试时间.ToString() ?? "-", 后EL机台编号 = x.后EL机台编号.ToString() ?? "-", 后EL测试结果 = x.后EL测试结果.ToString() ?? "-", 全检时间 = x.全检时间.ToString() ?? "-", 全检机台编号 = x.全检机台编号.ToString() ?? "-", 全检操作人员 = x.全检操作人员.ToString() ?? "-", }); }); //返回数据 return Success(new { data = list }); } catch (Exception e) { //TODO 记录日志 } return Failure("查询异常"); } #region 下载 [Route("api/DataQueryNew/DownloadDataQueryReportGet")] [HttpPost] public HttpResponseMessage DownloadDataQueryReportGet(DataQueryModel 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 { "组件编号" , "车间编号", " 工单类型" , "工单号" , "客户名称 ", "片源批号" , "片源厂商", "片源档位", "电池尺寸", "分选操作人员", "片源规格", "片源用量", "单片功率", "电池种类 ", "焊接时间 ", "机台编号", "助焊剂厂商", "助焊剂批号", "操作人员", "操作班次", "电池片效率", "互联条厂商", "互联条规格", "叠层时间", "叠层机台编号", "高透EVA规格", "高透EVA批号", "玻璃厂商", "玻璃规格", "玻璃批号", "汇流条厂商", "汇流条规格", "叠层操作人员", "叠层操作班次", " EVA厂商", "普通EVA规格", "背板规格", "背板厂商", "背板批号", "汇流条批号", "前EL测试时间", "前EL机台编号", "前EL测试结果", "层压时间", "层压机台编号", "层压操作班次", " 层压后检验时间", " 层压后检验操作人员", "层压后检验操作班次", "装框时间", "装框机台编号", "线盒厂家", "线盒规格", " 线盒批号", "线盒胶厂商", "线盒胶批号", "装框操作人员", "装框操作班次 ", "接线盒拍照时间", "型材厂商", "型材规格", "型材批号", "型材胶厂商", "型材胶批号", "IV机台编号", "测试功率", "标称功率", "测试时间", "测试电流", "后EL测试时间", "后EL机台编号", "后EL测试结果", "全检时间", "全检机台编号", "全检操作人员", }; //设置第一列列宽 // 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 serialNum = ""; if (!string.IsNullOrEmpty(model.SerialNum)) { var serialNumGet = model.SerialNum.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var serialNumList = serialNumGet.Select(x => x.Trim()).Distinct().Where(s => !string.IsNullOrEmpty(s)).ToList(); serialNum = string.Join("','", serialNumList); serialNum = "'" + serialNum + "'"; } //获取数据 var res = DT.Query(model.WorkOrder, model.TestTimeBegin, model.TestTimeEnd, serialNum); foreach (var item in res.ToList()) { var rowCount1 = sheet.LastRowNum + 1; row = sheet.CreateRow(rowCount1); row.CreateCell(0).SetCellValue(item.车间编号.ToString()); row.GetCell(0).CellStyle = style; row.CreateCell(1).SetCellValue(item.工单类型.ToString()); row.GetCell(1).CellStyle = style; row.CreateCell(2).SetCellValue(item.工单类型.ToString()); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.客户名称.ToString()); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.片源批号.ToString()); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.片源厂商.ToString()); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.片源档位.ToString()); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.电池尺寸.ToString()); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.分选操作人员.ToString()); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.片源规格.ToString()); row.GetCell(9).CellStyle = style; row.CreateCell(10).SetCellValue(item.片源用量.ToString()); row.GetCell(10).CellStyle = style; row.CreateCell(11).SetCellValue(item.单片功率.ToString()); row.GetCell(11).CellStyle = style; row.CreateCell(12).SetCellValue(item.电池种类.ToString()); row.GetCell(12).CellStyle = style; row.CreateCell(13).SetCellValue(item.焊接时间.ToString()); row.GetCell(13).CellStyle = style; row.CreateCell(14).SetCellValue(item.机台编号.ToString()); row.GetCell(14).CellStyle = style; row.CreateCell(15).SetCellValue(item.助焊剂厂商.ToString()); row.GetCell(15).CellStyle = style; row.CreateCell(16).SetCellValue(item.助焊剂批号.ToString()); row.GetCell(16).CellStyle = style; row.CreateCell(17).SetCellValue(item.操作人员.ToString()); row.GetCell(17).CellStyle = style; row.CreateCell(18).SetCellValue(item.操作班次.ToString()); row.GetCell(18).CellStyle = style; row.CreateCell(19).SetCellValue(item.电池片效率.ToString()); row.GetCell(19).CellStyle = style; row.CreateCell(20).SetCellValue(item.互联条厂商.ToString()); row.GetCell(20).CellStyle = style; row.CreateCell(21).SetCellValue(item.互联条规格.ToString()); row.GetCell(21).CellStyle = style; row.CreateCell(22).SetCellValue(item.叠层时间.ToString()); row.GetCell(22).CellStyle = style; row.CreateCell(23).SetCellValue(item.叠层机台编号.ToString()); row.GetCell(23).CellStyle = style; row.CreateCell(24).SetCellValue(item.高透EVA规格.ToString()); row.GetCell(24).CellStyle = style; row.CreateCell(25).SetCellValue(item.高透EVA批号.ToString()); row.GetCell(25).CellStyle = style; row.CreateCell(26).SetCellValue(item.玻璃厂商.ToString()); row.GetCell(26).CellStyle = style; row.CreateCell(27).SetCellValue(item.玻璃规格.ToString()); row.GetCell(27).CellStyle = style; row.CreateCell(28).SetCellValue(item.玻璃批号.ToString()); row.GetCell(28).CellStyle = style; row.CreateCell(29).SetCellValue(item.汇流条厂商.ToString()); row.GetCell(29).CellStyle = style; row.CreateCell(30).SetCellValue(item.汇流条规格.ToString()); row.GetCell(30).CellStyle = style; row.CreateCell(31).SetCellValue(item.叠层操作人员.ToString()); row.GetCell(31).CellStyle = style; row.CreateCell(32).SetCellValue(item.叠层操作班次.ToString()); row.GetCell(32).CellStyle = style; row.CreateCell(33).SetCellValue(item.EVA厂商.ToString()); row.GetCell(33).CellStyle = style; row.CreateCell(34).SetCellValue(item.普通EVA规格.ToString()); row.GetCell(34).CellStyle = style; row.CreateCell(35).SetCellValue(item.背板规格.ToString()); row.GetCell(35).CellStyle = style; row.CreateCell(36).SetCellValue(item.背板厂商.ToString()); row.GetCell(36).CellStyle = style; row.CreateCell(37).SetCellValue(item.背板批号.ToString()); row.GetCell(37).CellStyle = style; row.CreateCell(38).SetCellValue(item.汇流条批号.ToString()); row.GetCell(38).CellStyle = style; row.CreateCell(39).SetCellValue(item.前EL测试时间.ToString()); row.GetCell(39).CellStyle = style; row.CreateCell(40).SetCellValue(item.前EL机台编号.ToString()); row.GetCell(40).CellStyle = style; row.CreateCell(41).SetCellValue(item.前EL测试结果.ToString()); row.GetCell(41).CellStyle = style; row.CreateCell(42).SetCellValue(item.层压时间.ToString()); row.GetCell(42).CellStyle = style; row.CreateCell(43).SetCellValue(item.层压机台编号.ToString()); row.GetCell(43).CellStyle = style; row.CreateCell(44).SetCellValue(item.层压操作班次.ToString()); row.GetCell(44).CellStyle = style; row.CreateCell(45).SetCellValue(item.层压后检验时间.ToString()); row.GetCell(45).CellStyle = style; row.CreateCell(46).SetCellValue(item.层压后检验操作人员.ToString()); row.GetCell(46).CellStyle = style; row.CreateCell(47).SetCellValue(item.层压后检验操作班次.ToString()); row.GetCell(47).CellStyle = style; row.CreateCell(48).SetCellValue(item.装框时间.ToString()); row.GetCell(48).CellStyle = style; row.CreateCell(49).SetCellValue(item.装框机台编号.ToString()); row.GetCell(49).CellStyle = style; row.CreateCell(50).SetCellValue(item.线盒厂家.ToString()); row.GetCell(50).CellStyle = style; row.CreateCell(51).SetCellValue(item.线盒规格.ToString()); row.GetCell(51).CellStyle = style; row.CreateCell(52).SetCellValue(item.线盒批号.ToString()); row.GetCell(52).CellStyle = style; row.CreateCell(53).SetCellValue(item.线盒胶厂商.ToString()); row.GetCell(53).CellStyle = style; row.CreateCell(54).SetCellValue(item.线盒胶批号.ToString()); row.GetCell(54).CellStyle = style; row.CreateCell(55).SetCellValue(item.装框操作人员.ToString()); row.GetCell(55).CellStyle = style; row.CreateCell(56).SetCellValue(item.装框操作班次.ToString()); row.GetCell(56).CellStyle = style; row.CreateCell(57).SetCellValue(item.接线盒拍照时间.ToString()); row.GetCell(57).CellStyle = style; row.CreateCell(58).SetCellValue(item.型材厂商.ToString()); row.GetCell(58).CellStyle = style; row.CreateCell(59).SetCellValue(item.型材规格.ToString()); row.GetCell(59).CellStyle = style; row.CreateCell(60).SetCellValue(item.型材批号.ToString()); row.GetCell(60).CellStyle = style; row.CreateCell(61).SetCellValue(item.型材胶厂商.ToString()); row.GetCell(61).CellStyle = style; row.CreateCell(62).SetCellValue(item.型材胶批号.ToString()); row.GetCell(62).CellStyle = style; row.CreateCell(63).SetCellValue(item.IV机台编号.ToString()); row.GetCell(63).CellStyle = style; row.CreateCell(64).SetCellValue(item.测试功率.ToString()); row.GetCell(64).CellStyle = style; row.CreateCell(65).SetCellValue(item.标称功率.ToString()); row.GetCell(65).CellStyle = style; row.CreateCell(66).SetCellValue(item.测试时间.ToString()); row.GetCell(66).CellStyle = style; row.CreateCell(67).SetCellValue(item.测试电流.ToString()); row.GetCell(67).CellStyle = style; row.CreateCell(68).SetCellValue(item.后EL测试时间.ToString()); row.GetCell(68).CellStyle = style; row.CreateCell(69).SetCellValue(item.后EL机台编号.ToString()); row.GetCell(69).CellStyle = style; row.CreateCell(70).SetCellValue(item.后EL测试结果.ToString()); row.GetCell(70).CellStyle = style; row.CreateCell(71).SetCellValue(item.全检时间.ToString()); row.GetCell(71).CellStyle = style; row.CreateCell(72).SetCellValue(item.全检机台编号.ToString()); row.GetCell(72).CellStyle = style; row.CreateCell(73).SetCellValue(item.全检操作人员.ToString()); row.GetCell(73).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 } }