using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Estsh.BLL; using System.Collections; using System.Data; using Estsh.Web.Util; using System.IO; using Estsh.Web.Service; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; namespace Estsh.Core.Web.Controllers { public class YYReWorkQueryController : Controller { // YYReWorkQueryService Service = new YYReWorkQueryService(); public ActionResult Index() { return View(); } string _where = ""; public string SetWhere() { int condition = 0; 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["txtStartTime"]) && !string.IsNullOrEmpty(Request["txtEndTime"])) { condition++; _where += " and a.create_ymd+' '+a.create_hms >= '" + Request["txtStartTime"].Trim() + "'"; _where += " and a.create_ymd+' '+a.create_hms <= '" + Request["txtEndTime"].Trim() + "'"; } if (!string.IsNullOrEmpty(Request["cbStageName"])) { condition++; _where += " and e.stage_name= '" + Request["cbStageName"].ToString().Trim() + "'"; } if (!string.IsNullOrEmpty(Request["cmbTypeName"])) { condition++; _where += " and c.type_name = '" + Request["cmbTypeName"].Trim() + "'"; } if (!string.IsNullOrEmpty(Request["cbModelName"])) { condition++; _where += " and b.model_name = '" + Request["cbModelName"].Trim() + "'"; } if (condition == 0) { _where += " and a.create_ymd+' '+a.create_hms >= '" + starttime + "'"; _where += " and a.create_ymd+' '+a.create_hms <= '" + endtime + "'"; } return _where; } /// /// 一次通过率 /// /// /// public ActionResult YYReWorkData(Pager pager) { _where = SetWhere(); int totalCount = 0; DataTable dtQuery = Service.YYReWorkData(_where, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dtQuery)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// 不良率 /// /// /// public ActionResult YYReWorkDataDeatil(Pager pager) { _where = SetWhere(); int totalCount = 0; DataTable dtQuery = Service.YYReWorkDataDeatil(_where, pager, ref totalCount); Hashtable result = new Hashtable(); result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dtQuery)); result.Add("pager.totalRows", totalCount); return Json(result); } /// /// 区段集合 /// /// 数据集 public ActionResult GetStageName() { Hashtable resault = new Hashtable(); ArrayList list = Service.GetStageName(); resault.Add("list", list); return Json(resault, JsonRequestBehavior.AllowGet); } /// /// 不良类型 /// /// /// public ActionResult GetDefectName(string stageName) { Hashtable ht = new Hashtable(); ArrayList alModel = Service.GetDefectName(stageName); ht.Add("list", alModel); return Json(ht, JsonRequestBehavior.AllowGet); } /// /// 车型 /// /// public ActionResult GetTypeName() { Hashtable ht = new Hashtable(); ArrayList alType = Service.GetTypeName(); ht.Add("list", alType); return Json(ht, JsonRequestBehavior.AllowGet); } /// /// 配置 /// /// /// public ActionResult GetModelList(string typeName) { Hashtable ht = new Hashtable(); ArrayList alModel = Service.GetModelList(typeName); ht.Add("list", alModel); return Json(ht, JsonRequestBehavior.AllowGet); } /// /// 导出数据 /// /// /// /// /// /// /// public ActionResult exportData(Pager pager, int a, String sort, String direction, String isPage) { _where = SetWhere(); int totalCount = 0; DataTable dataHt = Service.YYReWorkData(_where, pager, ref totalCount); DataTable dataHt1 = Service.YYReWorkDataDeatil(_where, pager, ref totalCount); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook1 = new HSSFWorkbook(); Stream outputStream = Response.OutputStream; // Stream outputStream1 = Response.OutputStream; HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("FTT报表"); ISheet sheet1 = 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("FTT"); } for (int i = 0; i < dataHt.Rows.Count; i++) { int row = i + 1; HSSFRow dataRow = (HSSFRow)sheet.CreateRow(row); dataRow.CreateCell(0).SetCellValue(dataHt.Rows[i]["type_name"].ToString()); dataRow.CreateCell(1).SetCellValue(dataHt.Rows[i]["model_name"].ToString()); dataRow.CreateCell(2).SetCellValue(dataHt.Rows[i]["stage_name"].ToString()); dataRow.CreateCell(3).SetCellValue(dataHt.Rows[i]["qty"].ToString()); dataRow.CreateCell(4).SetCellValue(dataHt.Rows[i]["cqty"].ToString()); dataRow.CreateCell(5).SetCellValue(dataHt.Rows[i]["per"].ToString()); } if (workbook != null) { HSSFRow headRow = (HSSFRow)sheet1.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 < dataHt1.Rows.Count; i++) { int row = i + 1; HSSFRow dataRow1 = (HSSFRow)sheet1.CreateRow(row); dataRow1.CreateCell(0).SetCellValue(dataHt1.Rows[i]["type_name"].ToString()); dataRow1.CreateCell(1).SetCellValue(dataHt1.Rows[i]["model_name"].ToString()); dataRow1.CreateCell(2).SetCellValue(dataHt1.Rows[i]["stage_name"].ToString()); dataRow1.CreateCell(3).SetCellValue(dataHt1.Rows[i]["defect_desc"].ToString()); dataRow1.CreateCell(4).SetCellValue(dataHt1.Rows[i]["item_defect_desc"].ToString()); dataRow1.CreateCell(5).SetCellValue(dataHt1.Rows[i]["ncqty"].ToString()); dataRow1.CreateCell(6).SetCellValue(dataHt1.Rows[i]["qty"].ToString()); dataRow1.CreateCell(7).SetCellValue(dataHt1.Rows[i]["per"].ToString()); } Response.Clear(); workbook.Write(outputStream); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=合格率查询报表_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/vnd.ms-excel"; Response.Flush(); } catch (Exception e) { } finally { workbook = null; } return null; } //public ActionResult exportData(Pager pager, Pager pager1, int a) //{ // int totalCount = 0; // int totalCount2 = 0; // string where = SetWhere(); // DataTable dtQuery = Service.YYReWorkData(where, pager, ref totalCount); // DataTable dtDetailQuery = Service.YYReWorkDataDeatil(where, pager1, ref totalCount2); // Stream outputStream = Response.OutputStream; // string AbsolutePath = Request.UrlReferrer.AbsolutePath; // string url = AbsolutePath.Remove(0, Request.ApplicationPath.Length); // GridColumnService colService = new GridColumnService(); // Models.SysWebGridColumn[] detailGridItems = colService.GetColumnByUrl(url, "汇总");//查询汇总GtidColumn] // Models.SysWebGridColumn[] SNCurrent = colService.GetColumnByUrl(url, "明细"); // HSSFWorkbook workbook = NPOIExcelTools.DataTableToWorkbook(detailGridItems, dtQuery, "汇总信息");//将汇总表转换为Workbook // NPOIExcelTools.AddSheet(workbook, SNCurrent, dtDetailQuery, "明细信息");//添加明细Sheet // Response.Clear(); // workbook.Write(outputStream); // Response.Buffer = true; // if (a == 1) // { // if (Request.Browser.Type.ToUpper().IndexOf("IE") >= 0) // { // Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("合格率_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls", System.Text.Encoding.UTF8)); // } // else // { // Response.AppendHeader("Content-Disposition", "attachment;filename=合格率_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"); // } // } // else // { // if (Request.Browser.Type.ToUpper().IndexOf("IE") >= 0) // { // Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("合格率_当前页_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls", System.Text.Encoding.UTF8)); // } // else // { // Response.AppendHeader("Content-Disposition", "attachment;filename=合格率_所有页_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"); // } // } // Response.ContentEncoding = System.Text.Encoding.UTF8; // Response.ContentType = "application/vnd.ms-excel"; // Response.Flush(); // return null; //} } }