using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using Warehouse.DAL.QC; using OfficeOpenXml; using System.Data; using Dapper; using System.Net.Http.Headers; //using System.Web.Mvc; using System.Data.SqlClient; using System.IO; using Warehouse.WebApi.Model; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; namespace Warehouse.WebApi { [RoutePrefix("api/QCNew")] public class QCCNewController : BaseController { static DbUtility dbhelp = new DbUtility(System.Configuration.ConfigurationManager.ConnectionStrings["mesConn"].ToString(), DbProviderType.SqlServer); QC qc = new QC(); [HttpPost,Route("QueryQCInfoNew")] public IHttpActionResult QueryQCInfoNew(QCModel model) { try { var res = qc.QueryQCInfonew(model); var list = new List(); res.ForEach(x => { list.Add(new { serial_nbr = x.serial_nbr.ToString() ?? "-", sale_order = x.sale_order.ToString() ?? "-", schedule_nbr = x.schedule_nbr.ToString() ?? "-", dqstatus = x.dqstatus.ToString() ?? "-", final_grade = x.final_grade.ToString() ?? "-", process_code_desc = x.process_code_desc.ToString() ?? "-", defect_position = x.defect_position.ToString() ?? "-", remark = x.remark.ToString() ?? "-", according_std = x.according_std.ToString() ?? "-", hstatus = x.hstatus.ToString() ?? "-", hperson = x.hperson.ToString() ?? "-", htime = x.htime.ToString() ?? "-", rstatus = x.rstatus.ToString() ?? "-", rperson = x.rperson.ToString() ?? "-", rtime = x.rtime.ToString() ?? "-", }); }); //返回数据 return Success(new { data = list }); } catch (Exception e) { //TODO 记录日志 } return Failure("查询异常"); } #region 下载 [HttpPost, Route("QCReportGet")] public HttpResponseMessage DownloadQCtReportGet(QCModel 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 = qc.QueryQCInfonew(model); foreach (var item in res.ToList()) { var rowCount1 = sheet.LastRowNum + 1; row = sheet.CreateRow(rowCount1); row.CreateCell(0).SetCellValue(item.serial_nbr.ToString()); row.GetCell(0).CellStyle = style; row.CreateCell(1).SetCellValue(item.sale_order.ToString()); row.GetCell(1).CellStyle = style; row.CreateCell(2).SetCellValue(item.schedule_nbr.ToString()); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.dqstatus.ToString()); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.final_grade.ToString()); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.process_code_desc.ToString()); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.defect_position.ToString()); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.remark.ToString()); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.according_std.ToString()); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.hstatus.ToString()); row.GetCell(9).CellStyle = style; row.CreateCell(10).SetCellValue(item.hperson.ToString()); row.GetCell(10).CellStyle = style; row.CreateCell(11).SetCellValue(item.htime.ToString()); row.GetCell(11).CellStyle = style; row.CreateCell(12).SetCellValue(item.rstatus.ToString()); row.GetCell(12).CellStyle = style; row.CreateCell(13).SetCellValue(item.rperson.ToString()); row.GetCell(13).CellStyle = style; row.CreateCell(14).SetCellValue(item.rtime.ToString()); row.GetCell(14).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 [Route("QueryStatus")] [HttpPost] public IHttpActionResult QueryStatus() { var res = qc.QueryStatus(); return Json(res); } [HttpPost, Route("QueryStatusNew")] public IHttpActionResult QueryStatusNew() { var res = qc.QueryStatus(); var keyList = new List(); keyList.Add(new { name = "全部", id = "" }); res.ForEach(x => { keyList.Add(new { name = x.visit_type_desc, id = x.visit_type }); }); return Success(new { keyList }); } [Route("ExportExcel")] [HttpPost] public HttpResponseMessage ExportExcel() { var stream = new MemoryStream(); // processing the stream. string sql = @"SELECT TOP 1000 * FROM[mes_main].[dbo].[assembly_basis]"; DataTable dt = dbhelp.ExecuteDataTable(sql, null); using (ExcelPackage pck = new ExcelPackage()) { ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts"); ws.Cells["A1"].LoadFromDataTable(dt, true); pck.SaveAs(stream); } var result = new HttpResponseMessage(HttpStatusCode.OK) { //Content = new ByteArrayContent(stream.ToArray()) Content = new StreamContent(stream) }; result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = "exceltst.xlsx" }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); return result; } } }