using BLL; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Data.SqlClient; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; using System.IO; namespace FineUIPro.Web.BaseInfo { public partial class Discipline : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower();//按钮权限 //this.drpCategoryId.DataTextField = "Category"; //this.drpCategoryId.DataValueField = "CategoryId"; //this.drpCategoryId.DataSource = BLL.DisciplineCategoryService.GetDisciplineCategoryList(); //this.drpCategoryId.DataBind(); //Funs.FineUIPleaseSelect(this.drpCategoryId); //this.drpCategoryId.SelectedIndex = 0; ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 BindGrid(); } } /// /// 绑定数据 /// private void BindGrid() { string strSql = @"SELECT dep.DisciplineId,dep.Remark, (CASE WHEN dep.Discipline IS NULL THEN dep.DisciplineCN WHEN dep.DisciplineCN IS NULL THEN dep.Discipline ELSE dep.Discipline+dep.DisciplineCN END) as Discipline FROM dbo.Base_Discipline AS dep WHERE 1=1"; // 2.获取当前分页数据 List listStr = new List(); if (!string.IsNullOrEmpty(txtDisciplineS.Text)) { strSql += " AND (dep.Discipline LIKE @Discipline OR dep.DisciplineCN LIKE @Discipline)"; listStr.Add(new SqlParameter("@Discipline", "%" + txtDisciplineS.Text.Trim() + "%")); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { Grid1.PageIndex = e.NewPageIndex; BindGrid(); } /// /// /// /// /// protected void btnSelect_Click(object sender, EventArgs e) { BindGrid(); } #endregion #region 分页下拉选择 /// /// 分页下拉选择 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } #endregion #region 删除 /// /// 删除 /// /// /// protected void btnDelete_Click(object sender, EventArgs e) { BLL.DisciplineService.DeleteDisciplineById(hfFormID.Text); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete Discipline"); // 重新绑定表格,并模拟点击[新增按钮] BindGrid(); //PageContext.RegisterStartupScript("onNewButtonClick();"); } /// /// 右键删除事件 /// /// /// protected void btnMenuDelete_Click(object sender, EventArgs e) { this.DeleteData(); } /// /// 删除方法 /// private void DeleteData() { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); BLL.DisciplineService.DeleteDisciplineById(rowID); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete Discipline"); } BindGrid(); //PageContext.RegisterStartupScript("onNewButtonClick();"); } } #endregion #region 编辑 /// /// 右键编辑事件 /// /// /// protected void btnMenuEdit_Click(object sender, EventArgs e) { this.EditData(); } /// /// 编辑数据方法 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("Please select at least one record!", MessageBoxIcon.Warning); return; } string Id = Grid1.SelectedRowID; var discipline = BLL.DisciplineService.GetDisciplineById(Id); if (discipline != null) { this.txtDiscipline.Text = discipline.Discipline; this.txtDisciplineCN.Text = discipline.DisciplineCN; this.txtRemark.Text = discipline.Remark; //if (!string.IsNullOrEmpty(discipline.CategoryId)) //{ // this.drpCategoryId.SelectedValue = discipline.CategoryId; //} hfFormID.Text = Id; this.btnDelete.Enabled = true; } } #endregion #region 保存 /// /// 保存按钮 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { string strRowID = hfFormID.Text; if (!BLL.DisciplineService.IsExitDiscipline(this.txtDiscipline.Text.Trim(),strRowID)) { //if (this.drpCategoryId.SelectedValue == BLL.Const._Null && !string.IsNullOrEmpty(this.drpCategoryId.SelectedValue)) //{ // ShowAlert("Please select category!", MessageBoxIcon.Warning); // return; //} Model.Base_Discipline discipline = new Model.Base_Discipline { Discipline = this.txtDiscipline.Text.Trim(), DisciplineCN = this.txtDisciplineCN.Text.Trim(), Remark = this.txtRemark.Text.Trim() }; //if (this.drpCategoryId.SelectedValue != BLL.Const._Null) //{ // discipline.CategoryId = this.drpCategoryId.SelectedValue; //} if (string.IsNullOrEmpty(strRowID)) { discipline.DisciplineId = SQLHelper.GetNewID(typeof(Model.Base_Discipline)); BLL.DisciplineService.AddDiscipline(discipline); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add the Discipline"); ShowNotify("Save successfully!", MessageBoxIcon.Success); } else { discipline.DisciplineId = strRowID; BLL.DisciplineService.UpdateDiscipline(discipline); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify the Discipline"); ShowNotify("Save successfully!", MessageBoxIcon.Success); } this.SimpleForm1.Reset(); // 重新绑定表格,并点击当前编辑或者新增的行 BindGrid(); //PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, discipline.DisciplineId)); //PageContext.RegisterStartupScript("onNewButtonClick();"); } else { ShowNotify("The discipline entered already exists!", MessageBoxIcon.Warning); } } #endregion #region 权限设置 /// /// 菜单按钮权限 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.DisciplineMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuEdit.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnSave)) { this.btnSave.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnDelete.Hidden = false; } } } #endregion protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e) { this.EditData(); } protected void btnNew_Click(object sender, EventArgs e) { this.hfFormID.Text = string.Empty; //this.drpCategoryId.SelectedValue = Const._Null; this.txtDiscipline.Text = string.Empty; this.txtDisciplineCN.Text = string.Empty; this.txtRemark.Text = string.Empty; this.btnDelete.Enabled = false; } #region 导出 /// /// 导出 /// /// /// protected void btnExport_Click(object sender, EventArgs e) { Grid1.PageSize = 10000; BindGrid(); string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "Discipline.xlsx"; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ReportFileName = filePath + "out.xls"; FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); XSSFSheet ws = (XSSFSheet)hssfworkbook.GetSheet("Sheet1"); #region 样式 // 字体样式 IFont font = hssfworkbook.CreateFont(); font.FontHeightInPoints = 11; font.IsBold = false; font.FontName = "Arial"; ICellStyle fontStyle = hssfworkbook.CreateCellStyle(); fontStyle.SetFont(font); ICellStyle yearStyle = hssfworkbook.CreateCellStyle(); yearStyle.VerticalAlignment = VerticalAlignment.Center; yearStyle.Alignment = HorizontalAlignment.Center; yearStyle.SetFont(font); //创建单元格样式 XSSFCellStyle backgroundstyle = (XSSFCellStyle)hssfworkbook.CreateCellStyle(); //填充模式 backgroundstyle.FillPattern = FillPattern.SolidForeground; //创建颜色 XSSFColor xssfcolor = new XSSFColor(); //rbg值 byte[] rgb = { (byte)255, (byte)192, (byte)203 }; //写入rgb 粉色背景颜色定义 xssfcolor.SetRgb(rgb); //设置颜色值 backgroundstyle.SetFillForegroundColor(xssfcolor); backgroundstyle.SetFont(font); #endregion if (Grid1.Rows.Count > 0) { var rowIndex = 1; for (int i = 0; i < Grid1.Rows.Count; i++) { //int rowID = Convert.ToInt32(Grid1.DataKeys[i][0]); if (ws.GetRow(rowIndex) == null) ws.CreateRow(rowIndex); #region 列赋值 if (ws.GetRow(rowIndex).GetCell(0) == null) ws.GetRow(rowIndex).CreateCell(0); ws.GetRow(rowIndex).GetCell(0).SetCellValue(rowIndex.ToString()); ws.GetRow(rowIndex).GetCell(0).CellStyle = fontStyle; if (ws.GetRow(rowIndex).GetCell(1) == null) ws.GetRow(rowIndex).CreateCell(1); ws.GetRow(rowIndex).GetCell(1).SetCellValue(Grid1.Rows[i].Values[1].ToString()); ws.GetRow(rowIndex).GetCell(1).CellStyle = fontStyle; if (ws.GetRow(rowIndex).GetCell(2) == null) ws.GetRow(rowIndex).CreateCell(2); ws.GetRow(rowIndex).GetCell(2).SetCellValue(Grid1.Rows[i].Values[2].ToString()); rowIndex++; } #endregion } ws.ForceFormulaRecalculation = true; using (FileStream filess = File.OpenWrite(ReportFileName)) { hssfworkbook.Write(filess); } FileInfo filet = new FileInfo(ReportFileName); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=Discipline" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } #endregion } }