using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using QMAPP.Common.Web.Controllers; using QMFrameWork.WebUI.Attribute; using QMAPP.FJC.Web.Models.Operation; using QMFrameWork.Data; using QMAPP.ServicesAgent; using QMAPP.FJC.Entity.Operation; using QMAPP.Entity; using QMFrameWork.WebUI.DataSource; using QMFrameWork.Common.Serialization; using NPOI.SS.UserModel; namespace QMAPP.FJC.Web.Controllers { /// /// 抽检 /// 20180610 /// public class ProductCheckController : QController { #region 获取列表 /// /// 加载列表 /// /// 结果 [HandleException] public ActionResult List(bool? callBack) { ProductCheckModel seachModel = new ProductCheckModel(); seachModel.CREATEDATESTART = DateTime.Now.Date.AddDays(-10).ToString("yyyy-MM-dd HH:mm:ss"); seachModel.CREATEDATEEND = DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"); if (callBack == true) TryGetSelectBuffer(out seachModel); seachModel.rownumbers = false; seachModel.url = "/ProductCheck/GetList"; return View("ProductCheckList", seachModel); } /// /// 获取列表 /// /// 是否回调 /// 列表 [HandleException] public ActionResult GetList(bool? callBack) { ProductCheckModel seachModel = null; DataPage page = null; ServiceAgent wcfAgent = this.GetServiceAgent(); ProductCheck condition = null; DataResult pageResult = new DataResult(); try { //获取查询对象 seachModel = GetModel(); #region 获取缓存值 if (callBack != null) { TryGetSelectBuffer(out seachModel); } else { //保存搜索条件 SetSelectBuffer(seachModel); } #endregion //获取前台分页设置信息 page = this.GetDataPage(seachModel); condition = CopyToModel(seachModel); #region wcf服务统一接口 pageResult = wcfAgent.InvokeServiceFunction>("ProductCheckBLL_GetList", condition, page); if (pageResult.IsSuccess == false) { SetMessage(pageResult.Msg); return List(true); } DateGridResult result = new DateGridResult(); result.Total = pageResult.Result.RecordCount; result.Rows = JsonConvertHelper.GetDeserialize>(pageResult.Result.Result.ToString()); #endregion return Content(result.GetJsonSource()); } catch (Exception ex) { throw ex; } } #endregion #region 加载详细信息 /// /// 加载详细信息 /// /// 结果 [HandleException] public ActionResult GetProductCheckItemList() { ServiceAgent wcfAgent = this.GetServiceAgent(); string pid = Request.Params["PID"]; ProductCheck pc = wcfAgent.InvokeServiceFunction("ProductCheckBLL_Get", new ProductCheck { PID = pid }); List list = new List(); CheckWithMaterial condition = new CheckWithMaterial(); condition.PRODUCTCHECK_PID = pid; condition.MATERIAL_CODE = pc.MATERIAL_CODE; #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>>("ProductCheckBLL_GetProductCheckItemList", condition); //通过返回dataResult判断 if (dataResult.Ex != null) { throw dataResult.Ex; } else if (dataResult.IsSuccess) { list = dataResult.Result; } List objlist = new List(); foreach (var o in list) { objlist.Add(new { PID = o.PID, PRODUCTCHECK_PID = o.PRODUCTCHECK_PID, CHECKITEMCODE = o.CHECKITEMCODE, CHECKITEMNAME=o.CHECKITEMNAME, INPUTVALUE = o.INPUTVALUE, STANDERDVALUE_UPPER=o.STANDERDVALUE_UPPER, STANDERDVALUE_DOWN = o.STANDERDVALUE_DOWN, CREATEUSERNAME = o.CREATEUSERNAME, CREATEDATE = o.CREATEDATE.ToString() }); } return Content(JsonConvertHelper.GetSerializes(objlist)); #endregion } #endregion /// /// 导出EXCEL /// /// /// [HandleException] public ActionResult ExportExcel() { string ids = Request.Params["selectKey"] + ""; string[] idarray = ids.Split(':'); if (idarray.Length > 8 || idarray.Length < 1) { SetMessage("请选择1至8条抽检记录进行导出操作"); return List(true); } ServiceAgent wcfAgent = this.GetServiceAgent(); string pid = Request.Params["PID"]; var result = wcfAgent.InvokeServiceFunction>("ProductCheckBLL_GetExportData", (object)idarray); if (!result.IsSuccess) { SetMessage(result.Msg); return List(true); } string tempfile = Server.MapPath(string.Format("~/Temp/{0}.xlsx", Guid.NewGuid().ToString())); try { GenarateExcel(result.Result, tempfile); return File(System.IO.File.Open(tempfile,System.IO.FileMode.Open), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", string.Format("注塑首件记录表-{0:yyyy-MM-dd}.xlsx", DateTime.Now)); } catch (Exception ex) { SetMessage(ex.Message); return List(true); } finally { if (System.IO.File.Exists(tempfile)) { //System.IO.File.Delete(tempfile); } } } /// /// 生成EXCEL /// /// /// private void GenarateExcel(ProductCheckExport data, string filename) { System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/App_Data/Excel/ProductCheck.xlsx"), System.IO.FileMode.Open); IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs); fs.Close(); var sheet = workbook.GetSheet("首件检查记录表"); //var s= sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(17,19,0,0)); //for (int i = 0; i < 10; i++) //{ // var row = sheet.GetRow(i + 6); // var cell1 = row.GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK); // var cell2 = row.GetCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK); // cell1.SetCellValue("A" + i); // cell2.SetCellValue("B" + i); //} int itemstartcolumn = 2; int rowindex = 2; #region 标题行 var titlerow = sheet.GetRow(rowindex); var materilaNameCell = titlerow.GetCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK); materilaNameCell.SetCellValue(data.MaterialInfo.MATERIAL_NAME); var materilaCodeCell = titlerow.GetCell(6, MissingCellPolicy.CREATE_NULL_AS_BLANK); materilaCodeCell.SetCellValue(data.MaterialInfo.MATERIAL_CODE); var datecell = titlerow.GetCell(9, MissingCellPolicy.CREATE_NULL_AS_BLANK); datecell.SetCellValue(DateTime.Now); #endregion rowindex = 5; #region 抽检时间行 var checktimerow = sheet.GetRow(rowindex); for (int i = 0; i < data.CheckRecords.Count; i++) { var timecell = checktimerow.GetCell(itemstartcolumn + (i * 2), MissingCellPolicy.CREATE_NULL_AS_BLANK); timecell.SetCellValue(data.CheckRecords[i].CREATEDATE); } #endregion rowindex = 6; #region 循环检查记录行 foreach (var checkGroup in data.CheckItems.GroupBy(p => p.CHECKITEMTYPE)) { if (checkGroup.Count() > 1) { sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowindex, rowindex + checkGroup.Count() - 1, 0, 0)); } var typerow = sheet.GetRow(rowindex); var typecell = typerow.GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK); typecell.SetCellValue(checkGroup.Key); foreach (var checkitem in checkGroup) { var checkvaluerow = sheet.GetRow(rowindex); var checkitemcell = checkvaluerow.GetCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK); checkitemcell.SetCellValue(checkitem.CHECKITEMNAME); for (int i = 0; i < data.CheckRecords.Count; i++) { var valuecell=checkvaluerow.GetCell(itemstartcolumn + (i*2), MissingCellPolicy.CREATE_NULL_AS_BLANK); var value = data.CheckValueItems.FirstOrDefault(p => p.CHECKITEMCODE == checkitem.CHECKITEMCODE && p.PRODUCTCHECK_PID == data.CheckRecords[i].PID); valuecell.SetCellValue(value != null ? value.INPUTVALUE : ""); } rowindex++; } } #endregion using (System.IO.Stream filestream = System.IO.File.Create(filename)) { workbook.Write(filestream); filestream.Close(); } } } }