360 lines
13 KiB
C#
360 lines
13 KiB
C#
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.Linq;
|
|
using System.IO;
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
using DocumentFormat.OpenXml;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
public partial class admin_user_log1 : MyWeb.config
|
|
{
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
if (!IsPostBack)
|
|
{
|
|
|
|
ObjectDataSource1.SelectParameters["TableName_val"].DefaultValue = "[admin_log]";
|
|
ObjectDataSource1.SelectParameters["ColumnName_val"].DefaultValue = "*";
|
|
ObjectDataSource1.SelectParameters["OrderColumn_val"].DefaultValue = "num desc";
|
|
ObjectDataSource1.SelectParameters["Where_val"].DefaultValue = "";
|
|
|
|
InitEnumsOptions<Model.admin_log.Status>(search3);
|
|
InitEnumsOptions<Model.admin_log.Systems>(search4);
|
|
|
|
//判斷修改完成後,是否有傳「頁數」的值回來
|
|
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)
|
|
{
|
|
// s_msg.Text = Session["s_msg"].ToString();
|
|
}
|
|
else
|
|
{
|
|
//s_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 = "資料總數:<font color='red'><b>" + Session["s_RecordTotalCount"] + "</b></font>";
|
|
//取自 App_Code/Cus_GetSQL.vb 的 Session
|
|
}
|
|
|
|
if ((pageLabel != null))
|
|
{
|
|
// 取得正確頁數及更新目前頁次
|
|
int currentPage = GridView1.PageIndex + 1;
|
|
pageLabel.Text = "目前頁數:<b><font color='red'>" + currentPage.ToString() + "</font> / " + GridView1.PageCount.ToString() + "</b>";
|
|
|
|
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)
|
|
{
|
|
DataRowView row = (DataRowView)e.Row.DataItem;
|
|
|
|
Model.admin_log.Status myStatus = (Model.admin_log.Status)Val(row["status"]);
|
|
Label status = (Label)e.Row.FindControl("status");
|
|
status.Text = GetEnumsDescription(myStatus);
|
|
|
|
|
|
|
|
Model.admin_log.Detail LoginDetail = (Model.admin_log.Detail)Val(row["word"]);
|
|
Label word = (Label)e.Row.FindControl("word");
|
|
word.Text = ValString(row["word"]);
|
|
|
|
}
|
|
}
|
|
|
|
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 (search1.Text.Trim() != "")
|
|
{
|
|
fs.Add("CHARINDEX(N'" + search1.Text.Trim() + "',u_id)>0");
|
|
s_msg += "帳號 =【" + search1.Text.Trim() + "】";
|
|
}
|
|
|
|
if (search2.Text.Trim() != "")
|
|
{
|
|
fs.Add("CHARINDEX(N'" + search2.Text.Trim() + "',login_ip)>0");
|
|
s_msg += "登入ip =【" + search2.Text.Trim() + "】";
|
|
}
|
|
|
|
if (search3.SelectedIndex > 0)
|
|
{
|
|
fs.Add("status = " + search3.SelectedValue);
|
|
s_msg += "事件 =【" + search3.SelectedItem.Text + "】";
|
|
}
|
|
|
|
if (search4.SelectedIndex > 0)
|
|
{
|
|
fs.Add("systems = " + search4.SelectedValue);
|
|
s_msg += "系統 =【" + search4.SelectedItem.Text + "】";
|
|
}
|
|
if (selectDate(start_day) != DBNull.Value)
|
|
{
|
|
DateTime s1 = ValDate(start_day.Text);
|
|
fs.Add("login_time >= '" + s1.ToString("yyyy-MM-dd") + "'");
|
|
s_msg += "登入日期(起) =【" + start_day.Text + "】";
|
|
}
|
|
|
|
if (selectDate(end_day) != DBNull.Value)
|
|
{
|
|
DateTime s2 = ValDate(end_day.Text);
|
|
s2 = s2.AddDays(1);
|
|
fs.Add("login_time < '" + s2.ToString("yyyy-MM-dd") + "'");
|
|
s_msg += "登入日期(訖) =【" + end_day.Text + "】";
|
|
}
|
|
|
|
if (s_msg != null)
|
|
{
|
|
Session["s_msg"] = "<span class=\"word9\">搜尋條件:</span>" + s_msg;
|
|
//this.s_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;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
protected void Button_All_Click(object sender, EventArgs e)
|
|
{
|
|
Response.Redirect(Request.Url.AbsolutePath);
|
|
}
|
|
|
|
#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<WorksheetPart>();
|
|
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 = 25, CustomWidth = true },
|
|
new Column { Min = 2, Max = 3, Width = 10, CustomWidth = true },
|
|
new Column { Min = 4, Max = 4, Width = 15, CustomWidth = true },
|
|
new Column { Min = 5, Max = 5, 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 },
|
|
new Cell() { CellValue = new CellValue("裝置"), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue("IP"), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue("系統"), DataType = CellValues.String },
|
|
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)
|
|
{
|
|
foreach (DataRow row in dt.Rows)
|
|
{
|
|
//新增資料列
|
|
tr = new Row();
|
|
tr.Append(
|
|
new Cell() { CellValue = new CellValue(ValString(row["login_time"])), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue(ValString(row["u_id"])), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue(ValString(row["agent"])), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue(ValString(row["login_ip"])), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue(!isStrNull(row["systems"])? GetEnumsDescription((Model.admin_log.Systems)Val(row["systems"])):"" ), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue(GetEnumsDescription((Model.admin_log.Status)Val(row["status"])) ), DataType = CellValues.String },
|
|
new Cell() { CellValue = new CellValue(ValString(row["word"])), DataType = CellValues.String }
|
|
);
|
|
sd.AppendChild(tr);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//Response.Write(ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
sqlConn.Close();
|
|
sqlConn.Dispose();
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
} |