using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.IO; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; using System.Linq; public partial class admin_user_group_show : MyWeb.config { private Model.ezEntities _db = new Model.ezEntities(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ObjectDataSource1.SelectParameters["TableName_val"].DefaultValue = "[admin_group]"; ObjectDataSource1.SelectParameters["ColumnName_val"].DefaultValue = "*"; ObjectDataSource1.SelectParameters["OrderColumn_val"].DefaultValue = "g_name"; if (admin.info.group != "EZ") { ObjectDataSource1.SelectParameters["Where_val"].DefaultValue = "g_name<>'EZ' and g_name<>'A'"; } //判斷修改完成後,是否有傳「頁數」的值回來 if (!isStrNull(Request["page"])) { if (Session["search1"] != null) { ObjectDataSource1.SelectParameters["Where_val"].DefaultValue = ValString(Session["search1"]); } GridView1.PageIndex = Convert.ToInt32(Request["page"]); } else { Session["s_msg"] = null; Session["search1"] = null; Session["search2"] = null; } } else { if (Session["search1"] != null) { ObjectDataSource1.SelectParameters["Where_val"].DefaultValue = ValString(Session["search1"]); } } if (Session["s_msg"] != null) { //L_msg.Text = Session["s_msg"].ToString(); } else { //L_msg.Text = ""; } } #region GridView相關 protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e) { if (e.Exception != null) { e.ExceptionHandled = true; Response.Write(e.Exception.Message); } GridViewRow gvrPagerRow = GridView1.BottomPagerRow; } protected void GridView1_DataBound(object sender, System.EventArgs e) { if (GridView1.Rows.Count == 0 & GridView1.PageCount > 0) { GridView1.DataBind(); // 若有查到一筆以上的資料 } else if (GridView1.PageCount > 0) { GridView1.BottomPagerRow.Visible = true; //在pagertemplate裡的控制項中寫入值 GridViewRow pagerRow = GridView1.BottomPagerRow; DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel"); if ((pageList != null)) { int i = 0; for (i = 0; i <= GridView1.PageCount - 1; i++) { int pageNumber = i + 1; ListItem item = new ListItem(pageNumber.ToString()); if (i == GridView1.PageIndex) { item.Selected = true; } pageList.Items.Add(item); } } if (Session["s_RecordTotalCount"] != null) { ((Label)pagerRow.Cells[0].FindControl("total_count")).Text = "資料總數:" + Session["s_RecordTotalCount"] + ""; //取自 App_Code/Cus_GetSQL.vb 的 Session } if ((pageLabel != null)) { // 取得正確頁數及更新目前頁次 int currentPage = GridView1.PageIndex + 1; pageLabel.Text = "目前頁數:" + currentPage.ToString() + " / " + GridView1.PageCount.ToString() + ""; if (currentPage <= 1) { ((LinkButton)pagerRow.Cells[0].FindControl("FirstButton")).Visible = false; ((LinkButton)pagerRow.Cells[0].FindControl("PreviousButton")).Visible = false; } if (currentPage >= GridView1.PageCount) { ((LinkButton)pagerRow.Cells[0].FindControl("NextButton")).Visible = false; ((LinkButton)pagerRow.Cells[0].FindControl("LastButton")).Visible = false; } } } else { L_msg.Type = alert_type.warning; L_msg.Text = "找不到符合條件資料!"; } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { } } protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow pagerRow = GridView1.BottomPagerRow; DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); GridView1.PageIndex = pageList.SelectedIndex; } #endregion #region 條件搜尋 protected void Button1_Click(object sender, EventArgs e) { Session["search1"] = null; Session["s_msg"] = null; string s_msg = null; this.L_msg.Text = ""; ArrayList fs = new ArrayList(); if (admin.info.group != "EZ") { fs.Add("g_name<>'EZ' and g_name<>'A'"); } if (search1.Text.Trim() != "") { fs.Add("CHARINDEX(N'" + search1.Text.Trim() + "',g_name)>0"); s_msg += "群組代號 =【" + search1.Text.Trim() + "】"; } if (search2.Text.Trim() != "") { fs.Add("CHARINDEX(N'" + search2.Text.Trim() + "',demo)>0"); s_msg += "群組名稱 =【" + search2.Text.Trim() + "】"; } if (s_msg != null) { Session["s_msg"] = "搜尋條件:" + s_msg; //L_msg.Text = Session["s_msg"].ToString(); Cus_SQL.SQL_Array find_str = new Cus_SQL.SQL_Array(); Session["search1"] = find_str.sql_str(fs); ObjectDataSource1.SelectParameters["Where_val"].DefaultValue = ValString(Session["search1"]); if (GridView1.PageCount > 0) { GridView1.PageIndex = 0; } } else { Response.Redirect(Request.Url.AbsolutePath); } } protected void Button_All_Click(object sender, EventArgs e) { Response.Redirect(Request.Url.AbsolutePath); } #endregion #region 刪除 protected void DelButton_Click(object sender, EventArgs e) { LinkButton DelButton = (LinkButton)sender; GridViewRow oRow = (GridViewRow)DelButton.NamingContainer; int iRowIndex = oRow.RowIndex; string strID = Convert.ToString(GridView1.DataKeys[iRowIndex].Values[0]); //取得主索引(g_name)編號 start_del_data(strID); Literal UserName = (Literal)oRow.FindControl("UserName"); Model.admin_log admin_log = new Model.admin_log(); admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Power, (int)Model.admin_log.Status.Delete, "群組:" + UserName.Text); GridView1.DataBind(); } protected void del_all_Click(object sender, EventArgs e) { List x = new List(); for (int i = GridView1.Rows.Count - 1; i >= 0; i--) { CheckBox oCheckBox = (CheckBox)GridView1.Rows[i].Cells[GridView1.Columns.Count - 1].FindControl("chkSelect1"); Literal UserName = (Literal)GridView1.Rows[i].Cells[GridView1.Columns.Count - 1].FindControl("UserName"); if (oCheckBox.Checked) { x.Add(UserName.Text); start_del_data(Convert.ToString(GridView1.DataKeys[i].Values[0])); } } Model.admin_log admin_log = new Model.admin_log(); admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Power, (int)Model.admin_log.Status.Delete, "群組:" + admin_log.LogViewBtn(x)); GridView1.DataBind(); } public void start_del_data(string num) { var prod = _db.admin_group.AsEnumerable().Where(q => q.g_name == num).FirstOrDefault(); //刪除該筆資料 if (prod != null) { _db.admin_group.Remove(prod); _db.SaveChanges(); //執行 } // MyWeb.sql sql = new MyWeb.sql(); //OleDbConnection sqlConn = sql.conn(db, p_name); //try //{ // sqlConn.Open(); // OleDbCommand sqlCmd = new OleDbCommand("", sqlConn); // sqlCmd.CommandText = "Delete from admin_group Where g_name=?"; // sqlCmd.Parameters.Add(new OleDbParameter("g_name", num)); // sqlCmd.ExecuteNonQuery(); //} //catch (Exception ex) //{ // Response.Write(ex.Message); //} //finally //{ // sqlConn.Close(); sqlConn.Dispose(); //} } #endregion #region 匯出Excel protected void excel_Click(object sender, EventArgs e) { var memoryStream = new MemoryStream(); using (var doc = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook)) { var wb = doc.AddWorkbookPart(); wb.Workbook = new Workbook(); var sheets = wb.Workbook.AppendChild(new Sheets()); //建立第一個頁籤 var ws = wb.AddNewPart(); ws.Worksheet = new Worksheet(); sheets.Append(new Sheet() { Id = wb.GetIdOfPart(ws), SheetId = 1, Name = "群組資料" }); //設定欄寬 var cu = new Columns(); cu.Append( new Column { Min = 1, Max = 1, Width = 10, CustomWidth = true }, new Column { Min = 2, Max = 2, Width = 15, CustomWidth = true } ); ws.Worksheet.Append(cu); //建立資料頁 var sd = new SheetData(); ws.Worksheet.AppendChild(sd); //第一列資料 var tr = new Row(); tr.Append( new Cell() { CellValue = new CellValue("群組代號"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("群組名稱"), DataType = CellValues.String } ); sd.AppendChild(tr); //查詢要匯出的資料 string table = ObjectDataSource1.SelectParameters["TableName_val"].DefaultValue; string column = ObjectDataSource1.SelectParameters["ColumnName_val"].DefaultValue; string sort = ObjectDataSource1.SelectParameters["OrderColumn_val"].DefaultValue; string query = ObjectDataSource1.SelectParameters["Where_val"].DefaultValue; MyWeb.sql sql = new MyWeb.sql(); OleDbConnection sqlConn = sql.conn(db, p_name); try { sqlConn.Open(); OleDbCommand sqlCmd = new OleDbCommand("", sqlConn); sqlCmd.CommandText = "SELECT " + column + " FROM " + table; if (!isStrNull(query)) { sqlCmd.CommandText += " where " + query; } sqlCmd.CommandText += " order by " + sort; DataTable dt = sql.dataTable(sqlCmd); if (dt.Rows.Count > 0) { List x = new List(); foreach (DataRow row in dt.Rows) { //新增資料列 tr = new Row(); tr.Append( new Cell() { CellValue = new CellValue(ValString(row["g_name"])), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(ValString(row["demo"])), DataType = CellValues.String } ); sd.AppendChild(tr); x.Add(ValString(row["g_name"])); } Model.admin_log admin_log = new Model.admin_log(); admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Power, (int)Model.admin_log.Status.Excel, "群組:"+ admin_log.LogViewBtn(x)); } } catch (Exception ex) { //Response.Write(ex.Message); } finally { sqlConn.Close(); sqlConn.Dispose(); } //儲存文件(如果有要儲實體的檔案在upload才需要) //wb.Workbook.Save(); } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=群組資料.xlsx"); HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray()); HttpContext.Current.Response.End(); } #endregion }