using BLL; using Newtonsoft.Json.Linq; using NPOI.SS.Util; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; namespace FineUIPro.Web.TestRun.DriverPrepare { public partial class SchemePlan : PageBase { #region 加载 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower(); BindGrid(); } } #endregion #region 数据绑定 //加载列表 public void BindGrid() { string strSql = @"select * from DriverPrepare_SchemePlanItem chec" + @" left join DriverPrepare_SchemePlan u on u.SchemePlanId = chec.SchemePlanId where u.ProjectId=@projectId"; List listStr = new List(); listStr.Add(new SqlParameter("@projectId", this.CurrUser.LoginProjectId)); SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; //tb = GetFilteredTable(Grid1.FilteredData, tb); var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); var solutionType = (from x in Funs.DB.DriverPrepare_SchemePlan join y in Funs.DB.DriverPrepare_SchemePlanItem on x.SchemePlanId equals y.SchemePlanId where y.SolutionType != null && x.ProjectId == this.CurrUser.LoginProjectId select y).FirstOrDefault(); if (solutionType == null) { this.Grid1.Columns[1].Hidden = true; } else { this.Grid1.Columns[1].Hidden = false; } } #endregion #region 编辑 protected void btnMenuModify_Click(object sender, EventArgs e) { EditData(); } /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { EditData(); } /// /// 编辑 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("SchemePlanItemAdd.aspx?SchemePlanItemId={0}", Grid1.SelectedRowID, "编辑 - "))); } #endregion #region 删除 protected void btnMenuDel_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { BLL.DriverPrepareSchemePlanItemService.DeleteSchemePlanItemById(Grid1.SelectedRowID); } BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } } protected void btnDel_Click(object sender, EventArgs e) { var plan = (from x in Funs.DB.DriverPrepare_SchemePlan where x.ProjectId == this.CurrUser.LoginProjectId select x).FirstOrDefault(); if (plan == null) { Alert.ShowInTop("项目无记录,无需删除!", MessageBoxIcon.Warning); return; } BLL.DriverPrepareSchemePlanItemService.DeleteSchemePlanItemByschemePlanId(plan.SchemePlanId); BLL.DriverPrepareSchemePlanService.DeleteDriverPlan(plan.SchemePlanId); BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } #endregion #region 权限设置 /// /// 权限设置 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.SchemePlanMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuModify.Hidden = false; this.Grid1.EnableRowDoubleClickEvent = true; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnDel.Hidden = false; this.btnMenuDel.Hidden = false; } } } #endregion #region 搜索 /// /// 搜索 /// /// /// protected void btnNew_Click(object sender, EventArgs e) { string window = String.Format("SchemePlanItemAdd.aspx", "新增 - "); PageContext.RegisterStartupScript(Window2.GetSaveStateReference(hdItemsString.ClientID) + Window2.GetShowReference(window)); } #endregion #region 关闭弹出窗口 /// /// 关闭弹出窗口 /// /// /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } #endregion /// /// 选择事件 /// /// /// protected void btnSelect_Click(object sender, EventArgs e) { //JArray mergedData = Grid1.GetMergedData(); //string names = string.Empty; //foreach (JObject mergedRow in mergedData) //{ // string status = mergedRow.Value("status"); // JObject values = mergedRow.Value("values"); // names += values.Value("SolutionName").ToString() + ","; //} //if (!string.IsNullOrEmpty(names)) //{ // hdItemsString.Text = names.Substring(0, names.Length - 1); //} Model.DriverPrepare_SchemePlan plan = BLL.DriverPrepareSchemePlanService.GetDriverPlanByProjectId(this.CurrUser.LoginProjectId); string schemePlanId = string.Empty; if (plan != null) { schemePlanId = plan.SchemePlanId; } string window = String.Format("SchemePlanItemSelect.aspx?SchemePlanId={0}", schemePlanId, "选择 - "); PageContext.RegisterStartupScript(Window1.GetShowReference(window)); } #region 导出按钮 /// 导出按钮 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string initTemplatePath = string.Empty; string uploadfilepath = string.Empty; string newUrl = string.Empty; string filePath = string.Empty; var solutionType = (from x in Funs.DB.DriverPrepare_SchemePlan join y in Funs.DB.DriverPrepare_SchemePlanItem on x.SchemePlanId equals y.SchemePlanId where y.SolutionType != null && x.ProjectId == this.CurrUser.LoginProjectId select y).FirstOrDefault(); if (solutionType == null) { initTemplatePath = Const.SchemePlanTemplateUrl; } else { initTemplatePath = Const.SchemePlan2TemplateUrl; } uploadfilepath = rootPath + initTemplatePath; Model.DriverPrepare_SchemePlan plan = BLL.DriverPrepareSchemePlanService.GetDriverPlanByProjectId(this.CurrUser.LoginProjectId); if (plan != null) { string projectName = BLL.ProjectService.GetProjectNameByProjectId(plan.ProjectId); if (solutionType == null) { newUrl = uploadfilepath.Replace(".xlsx", "(" + projectName + ")" + ".xlsx"); File.Copy(uploadfilepath, newUrl); // 第一步:读取文件流 NPOI.SS.UserModel.IWorkbook workbook; using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); } // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; var font = workbook.CreateFont(); font.FontHeightInPoints = 11; cellStyle.SetFont(font); NPOI.SS.UserModel.ICellStyle cellStyle0 = workbook.CreateCellStyle(); cellStyle0.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle0.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; var font0 = workbook.CreateFont(); font0.FontHeightInPoints = 12; font0.IsBold = true; cellStyle0.SetFont(font0); // 第二步:创建新数据行 NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); NPOI.SS.UserModel.IRow row = sheet.GetRow(0); NPOI.SS.UserModel.ICell cell; cell = row.GetCell(0); cell.CellStyle = cellStyle0; cell.SetCellValue(projectName + "试车方案"); var items = BLL.DriverPrepareSchemePlanItemService.GetSchemePlanItemByschemePlanId(plan.SchemePlanId); int i = 2; foreach (var item in items) { // 第二步:创建新数据行 row = sheet.CreateRow(i); // 添加数据 cell = row.CreateCell(0); cell.CellStyle = cellStyle; cell.SetCellValue((i - 1).ToString()); cell = row.CreateCell(1); cell.CellStyle = cellStyle; cell.SetCellValue(item.SolutionName); cell = row.CreateCell(2); cell.CellStyle = cellStyle; cell.SetCellValue(string.Empty); cell = row.CreateCell(3); cell.CellStyle = cellStyle; cell.SetCellValue(string.Empty); i++; } // 第三步:写入文件流 using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) { workbook.Write(stream); workbook.Close(); } string fileName = Path.GetFileName(newUrl); FileInfo info = new FileInfo(newUrl); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(newUrl, 0, fileSize); Response.Flush(); Response.Close(); File.Delete(newUrl); } else { newUrl = uploadfilepath.Replace("2.xlsx", "(" + projectName + ")" + ".xlsx"); File.Copy(uploadfilepath, newUrl); // 第一步:读取文件流 NPOI.SS.UserModel.IWorkbook workbook; using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); } // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; var font = workbook.CreateFont(); font.FontHeightInPoints = 11; cellStyle.SetFont(font); NPOI.SS.UserModel.ICellStyle cellStyle0 = workbook.CreateCellStyle(); cellStyle0.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle0.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle0.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; var font0 = workbook.CreateFont(); font0.FontHeightInPoints = 12; font0.IsBold = true; cellStyle0.SetFont(font0); // 第二步:创建新数据行 NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); NPOI.SS.UserModel.IRow row = sheet.GetRow(0); NPOI.SS.UserModel.ICell cell; cell = row.GetCell(0); cell.CellStyle = cellStyle0; cell.SetCellValue(projectName + "试车方案"); var items = BLL.DriverPrepareSchemePlanItemService.GetSchemePlanItemByschemePlanId(plan.SchemePlanId); int i = 2; string type = string.Empty; int firstRow = 2; foreach (var item in items) { if (string.IsNullOrEmpty(type)) { type = item.SolutionType; } // 第二步:创建新数据行 row = sheet.CreateRow(i); // 添加数据 cell = row.CreateCell(0); cell.CellStyle = cellStyle; cell.SetCellValue((i - 1).ToString()); cell = row.CreateCell(1); cell.CellStyle = cellStyle; cell.SetCellValue(item.SolutionType); cell = row.CreateCell(2); cell.CellStyle = cellStyle; cell.SetCellValue(item.SolutionName); cell = row.CreateCell(3); cell.CellStyle = cellStyle; cell.SetCellValue(string.Empty); cell = row.CreateCell(4); cell.CellStyle = cellStyle; cell.SetCellValue(string.Empty); if (type != item.SolutionType) { type = item.SolutionType; sheet.AddMergedRegion(new CellRangeAddress(firstRow, i - 1, 1, 1)); firstRow = i; } else { if (i == 1 + items.Count) { sheet.AddMergedRegion(new CellRangeAddress(firstRow, i, 1, 1)); } } i++; } // 第三步:写入文件流 using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) { workbook.Write(stream); workbook.Close(); } string fileName = Path.GetFileName(newUrl); FileInfo info = new FileInfo(newUrl); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(newUrl, 0, fileSize); Response.Flush(); Response.Close(); File.Delete(newUrl); } } else { Alert.ShowInTop("当前无记录,无法导出!", MessageBoxIcon.Warning); } } #endregion } }