using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Estsh.Web.Service; using System.Data; using System.Collections; using NPOI.HSSF.UserModel; using System.IO; using Estsh.Web.Util; using Aspose.Cells; namespace Estsh.Core.Web.Controllers { public class TestDataController : Controller { TestDataService service = new TestDataService(); // // GET: /Menu/ public ActionResult Index() { return View(); } string _where = ""; public string SetWhere() { int condition = 0; _where = ""; DateTime startDate = Request.RequestContext.HttpContext.Timestamp.AddDays(-1); DateTime endDate = Request.RequestContext.HttpContext.Timestamp; string starttime = startDate.ToString("yyyy-MM-dd HH:mm:ss"); string endtime = endDate.ToString("yyyy-MM-dd HH:mm:ss"); if (!string.IsNullOrEmpty(Request["txtSN"])) { condition++; _where += " and a.条码 like '%" + Request["txtSN"].ToString().Trim().Replace('|', '#') + "%' "; } if (!string.IsNullOrEmpty(Request["txtStartTime"]) || !string.IsNullOrEmpty(Request["txtEndTime"])) { condition++; _where += " and a.测试完成日期时间 BETWEEN '" + Request["txtStartTime"].Trim() + "' and '" + Request["txtEndTime"].Trim() + "' "; } if (condition == 0) { _where += " and a.测试完成日期时间 BETWEEN '" + starttime + "' and '" + endtime + "' "; } return _where; } /// /// 电检主表 /// /// /// public ActionResult GetDjc(Pager pager) { _where = SetWhere(); int totalCount = 0; DataTable dataHt = service.GetDjc(_where, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// 电检明细表 /// /// /// public ActionResult GetDjcDeatil(Pager pager) { _where = SetWhere(); int totalCount = 0; DataTable dataHt = service.GetDjcDeatil(_where, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// 导出全部 /// /// public ActionResult exportData(Pager pager) { #region 查询电检汇总数据 _where = SetWhere(); //string txtSN = Request["txtSN"].ToString().Trim(); //Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); //string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); //string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); //txtSN = txtSN.Replace("|", "#"); //string whereStr = " where 1=1 "; //if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) //{ // return null; //} //if (string.IsNullOrEmpty(txtSN)) //{ // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr += @" and a.测试完成日期时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } //} //else //{ // whereStr += " and a.条码='" + txtSN + "'"; //} DataTable sumList = service.GetSumDjc(_where); #endregion //string whereStr1 = " where 1=1 "; //if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) //{ // return null; //} //if (string.IsNullOrEmpty(txtSN)) //{ // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr1 += @" and a.测试完成日期时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } //} //else //{ // whereStr1 += " and a.条码='"+txtSN + "'"; //} DataTable detailList = service.GetSumDjcDeatil(_where); HSSFWorkbook workbook = new HSSFWorkbook(); Stream outputStream = Response.OutputStream; HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("汇总"); HSSFSheet sheet2 = (HSSFSheet)workbook.CreateSheet("明细"); try { if (workbook != null) { HSSFRow headRow = (HSSFRow)sheet.CreateRow(0); headRow.CreateCell(0).SetCellValue("设备名称"); headRow.CreateCell(1).SetCellValue("条码"); headRow.CreateCell(2).SetCellValue("零件号"); headRow.CreateCell(3).SetCellValue("类型"); headRow.CreateCell(4).SetCellValue("测试总结果"); headRow.CreateCell(5).SetCellValue("测试时间"); headRow.CreateCell(6).SetCellValue("测试完成日期时间"); headRow.CreateCell(7).SetCellValue("记录保存日期时间"); } for (int i = 0; i < sumList.Rows.Count; i++) { int row = i + 1; HSSFRow dataRow = (HSSFRow)sheet.CreateRow(row); dataRow.CreateCell(0).SetCellValue(sumList.Rows[i]["设备名称"].ToString()); dataRow.CreateCell(1).SetCellValue(sumList.Rows[i]["条码"].ToString()); dataRow.CreateCell(2).SetCellValue(sumList.Rows[i]["零件号"].ToString()); dataRow.CreateCell(3).SetCellValue(sumList.Rows[i]["类型"].ToString()); dataRow.CreateCell(4).SetCellValue(sumList.Rows[i]["测试总结果"].ToString()); dataRow.CreateCell(5).SetCellValue(sumList.Rows[i]["测试时间"].ToString()); dataRow.CreateCell(6).SetCellValue(sumList.Rows[i]["测试完成日期时间"].ToString()); dataRow.CreateCell(7).SetCellValue(sumList.Rows[i]["记录保存日期时间"].ToString()); } if (workbook != null) { HSSFRow headRow = (HSSFRow)sheet2.CreateRow(0); headRow.CreateCell(0).SetCellValue("设备名称"); headRow.CreateCell(1).SetCellValue("条码"); headRow.CreateCell(2).SetCellValue("检测项目名称"); headRow.CreateCell(3).SetCellValue("下限值"); headRow.CreateCell(4).SetCellValue("上限值"); headRow.CreateCell(5).SetCellValue("测试值"); headRow.CreateCell(6).SetCellValue("测试结果"); headRow.CreateCell(7).SetCellValue("测试完成日期时间"); headRow.CreateCell(8).SetCellValue("记录保存日期时间"); } for (int i = 0; i < detailList.Rows.Count; i++) { int row = i + 1; HSSFRow dataRow = (HSSFRow)sheet2.CreateRow(row); dataRow.CreateCell(0).SetCellValue(detailList.Rows[i]["设备名称"].ToString()); dataRow.CreateCell(1).SetCellValue(detailList.Rows[i]["条码"].ToString()); dataRow.CreateCell(2).SetCellValue(detailList.Rows[i]["检测项目名称"].ToString()); dataRow.CreateCell(3).SetCellValue(detailList.Rows[i]["下限值"].ToString()); dataRow.CreateCell(4).SetCellValue(detailList.Rows[i]["上限值"].ToString()); dataRow.CreateCell(5).SetCellValue(detailList.Rows[i]["测试值"].ToString()); dataRow.CreateCell(6).SetCellValue(detailList.Rows[i]["测试结果"].ToString()); dataRow.CreateCell(7).SetCellValue(detailList.Rows[i]["测试完成日期时间"].ToString()); dataRow.CreateCell(8).SetCellValue(detailList.Rows[i]["记录保存日期时间"].ToString()); } Response.Clear(); workbook.Write(outputStream); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=电检测信息" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/vnd.ms-excel"; Response.Flush(); } catch (Exception ex) { } return null; } /// /// 获取二排电检测信息 /// /// /// public ActionResult GetSNCurrentData(Pager pager) { string txtSN = Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); string whereStr = " 1=1 "; if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) { return null; } if (string.IsNullOrEmpty(txtSN)) { if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) { whereStr += " and CONVERT(DATETIME,a.测试时间,120) BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; } } else { whereStr += " and a.型号条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; } int totalCount = 0; DataTable dataHt = service.GetSNCurrentData(whereStr, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); //DataTable dt = null; //dt = service.GetSNCurrentData(whereStr, pager, ref totalCount); //return AsposeExcelTools.DataTableToExcel2(dt); } /// /// 二排拉动力 /// /// /// public ActionResult GetImpetus(Pager pager) { string txtSN = Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); string whereStr = " 1=1 "; if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) { return null; } if (string.IsNullOrEmpty(txtSN)) { if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) { whereStr += " and CONVERT(DATETIME,a.测试时间,120) BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; } } else { whereStr += " and a.型号条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; } int totalCount = 0; DataTable dataHt = service.GetImpetus(whereStr, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// 前排SBR及视觉影像 /// /// /// public ActionResult GetSBR(Pager pager) { string txtSN = Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); string whereStr = " 1=1 "; if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) { return null; } if (string.IsNullOrEmpty(txtSN)) { if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) { whereStr += @" and a.测试完成日期+' '+测试完成时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; } } else { whereStr += " and a.条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; } int totalCount = 0; DataTable dataHt = service.GetSBR(whereStr, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// ///前排推拉力 /// /// /// public ActionResult GetFrontImpetus(Pager pager) { string txtSN = Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); string whereStr = " 1=1 "; if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) { return null; } if (string.IsNullOrEmpty(txtSN)) { if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) { whereStr += @" and a.测试完成日期+' '+测试完成时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; } } else { whereStr += " and a.条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; } int totalCount = 0; DataTable dataHt = service.GetFrontImpetus(whereStr, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// ///前排静音房 /// /// /// public ActionResult GetFrontRoom(Pager pager) { string txtSN = Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); string whereStr = " 1=1 "; if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) { return null; } if (string.IsNullOrEmpty(txtSN)) { if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) { whereStr += @" and a.测试完成日期+' '+测试完成时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; } } else { whereStr += " and a.条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; } int totalCount = 0; DataTable dataHt = service.GetFrontRoom(whereStr, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// QE电检测 /// /// /// public ActionResult GetRSBR(Pager pager) { string txtSN = Request["txtSN"] == null ? "" : Request["txtSN"].ToString().Trim(); string txtStartTime = Request["txtStartTime"] == null ? "" : Request["txtStartTime"].ToString().Trim(); string txtEndTime = Request["txtEndTime"] == null ? "" : Request["txtEndTime"].ToString().Trim(); string whereStr = " 1=1 "; if (string.IsNullOrEmpty(txtSN) && string.IsNullOrEmpty(txtStartTime) && string.IsNullOrEmpty(txtEndTime)) { return null; } if (string.IsNullOrEmpty(txtSN)) { if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) { whereStr += " and CONVERT(DATETIME,a.测试时间,120) BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; } } else { whereStr += " and a.座椅条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; } int totalCount = 0; DataTable dataHt = service.GetRSBR(whereStr, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dataHt)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// 导出Bom 数据 /// /// //public ActionResult exportData(Pager pager) //{ // Workbook workbook = GetSNCurrentData(pager); // //Workbook workbook1 = GetImpetus(pager1); // //Workbook workbook2 = GetSBR(pager2); // //Workbook workbook3 = GetFrontImpetus(pager3); // //Workbook workbook4 = GetFrontRoom(pager4); // if (workbook != null) // { // //直接导出 // Stream outputStream = Response.OutputStream; // workbook.Save(outputStream, FileFormatType.Default); // Response.Buffer = true; // Response.AppendHeader("Content-Disposition", "attachment;filename=产品状态查询.xls"); // Response.ContentEncoding = System.Text.Encoding.UTF8; // Response.ContentType = "application/vnd.ms-excel"; // Response.Flush(); // } // return null; //} ///// ///// 导出全部 ///// ///// //public ActionResult exportData(Pager pager, Pager pager1, Pager pager2, Pager pager3, Pager pager4, Pager pager5, string txtSN, string txtStartTime, string txtEndTime) //{ // // 如果没有数据就直接返回 // //string wheres = GetWhereStr(); // string whereStr = " "; // string whereStr1 = " "; // string whereStr2 = " "; // string whereStr3 = " "; // string whereStr4 = " "; // string whereStr5 = " "; // try // { // if (string.IsNullOrEmpty(txtSN)) // { // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr = " CONVERT(DATETIME,a.测试时间,120) BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } // } // else // { // whereStr = " a.型号条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; // } // int totalCount = 0; // DataTable SNCurrentData = new DataTable(); // if (pager.pageSize > 0) // { // SNCurrentData = service.GetSNCurrentData(whereStr, pager, ref totalCount); //二排电检测 // } // if (string.IsNullOrEmpty(txtSN)) // { // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr1 = "CONVERT(DATETIME,a.测试时间,120) BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } // } // else // { // whereStr1 = " a.型号条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; // } // totalCount = 0; // DataTable Impetus = new DataTable(); // if (pager1.pageSize > 0) // { // Impetus = service.GetImpetus(whereStr1, pager1, ref totalCount); //二排推拉力 // } // if (string.IsNullOrEmpty(txtSN)) // { // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr2 = @" a.测试完成日期+' '+测试完成时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } // } // else // { // whereStr2 = " a.条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; // } // totalCount = 0; // DataTable SBR = new DataTable(); // if (pager2.pageSize > 0) // { // SBR = service.GetSBR(whereStr2, pager2, ref totalCount);//前排SBR及视觉影像 // } // if (string.IsNullOrEmpty(txtSN)) // { // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr3 = @" a.测试完成日期+' '+测试完成时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } // } // else // { // whereStr3 = " a.条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; // } // totalCount = 0; // DataTable FrontImpetus = new DataTable(); // if (pager3.pageSize > 0) // { // FrontImpetus = service.GetFrontImpetus(whereStr3, pager3, ref totalCount);//前排推拉力 // } // if (string.IsNullOrEmpty(txtSN)) // { // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr4 = @" a.测试完成日期+' '+测试完成时间 BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } // } // else // { // whereStr4 = " a.条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; // } // totalCount = 0; // DataTable FrontRoom = new DataTable(); // if (pager4.pageSize > 0) // { // FrontRoom = service.GetFrontRoom(whereStr4, pager4, ref totalCount); //前排静音房 // } // if (string.IsNullOrEmpty(txtSN)) // { // if (!string.IsNullOrEmpty(txtStartTime) && !string.IsNullOrEmpty(txtEndTime)) // { // whereStr5 = " CONVERT(DATETIME,a.测试时间,120) BETWEEN '" + Request["txtStartTime"].ToString().Trim() + "' AND '" + Request["txtEndTime"].ToString().Trim() + "'"; // } // } // else // { // whereStr5 =" a.座椅条码 like '%" + Request["txtSN"].ToString().Trim() + "%'"; // } // totalCount = 0; // DataTable RSBR = new DataTable(); // if (pager5.pageSize > 0) // { // RSBR = service.GetRSBR(whereStr5, pager5, ref totalCount);//QE电检测 // } // Stream outputStream = Response.OutputStream; // string AbsolutePath = Request.UrlReferrer.AbsolutePath; // string url = AbsolutePath.Remove(0, Request.ApplicationPath.Length); // GridColumnService colService = new GridColumnService(); // Models.SysWebGridColumn[] SNCurrent = colService.GetColumnByUrl(url, "二排电检测"); // Models.SysWebGridColumn[] Impetus1 = colService.GetColumnByUrl(url, "二排推拉力"); // Models.SysWebGridColumn[] SBR1 = colService.GetColumnByUrl(url, "前排SBR&视觉"); // Models.SysWebGridColumn[] FrontImpetus1 = colService.GetColumnByUrl(url, "前排推拉力"); // Models.SysWebGridColumn[] FrontRoom1 = colService.GetColumnByUrl(url, "静音房"); // Models.SysWebGridColumn[] SBR2 = colService.GetColumnByUrl(url, "QE电检测"); // HSSFWorkbook workbook = NPOIExcelTools.DataTableToWorkbook(SNCurrent, SNCurrentData, "二排电检测");//将汇总表转换为Workbook // NPOIExcelTools.AddSheet(workbook, Impetus1, Impetus, "二排推拉力"); // NPOIExcelTools.AddSheet(workbook, SBR1, SBR, "前排SBR&视觉"); // NPOIExcelTools.AddSheet(workbook, FrontImpetus1, FrontImpetus, "前排推拉力"); // NPOIExcelTools.AddSheet(workbook, FrontRoom1, FrontRoom, "静音房"); // NPOIExcelTools.AddSheet(workbook, SBR2, RSBR, "QE电检测"); // Response.Clear(); // workbook.Write(outputStream); // } // catch (Exception) // { // return null; // } // Response.Buffer = true; // if (Request.Browser.Type.ToUpper().IndexOf("IE") >= 0) // { // Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("数据检测查询.xls", System.Text.Encoding.UTF8)); // } // else // { // Response.AppendHeader("Content-Disposition", "attachment;filename=数据检测查询.xls"); // } // Response.ContentEncoding = System.Text.Encoding.UTF8; // Response.ContentType = "application/vnd.ms-excel"; // Response.Flush(); // return null; //} } }