using System;
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;
using static TreeView;
public partial class admin_hr_index : MyWeb.config
{
private Model.ezEntities _db = new Model.ezEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BuildKind();
}
}
#region 分類
public void BuildKind()
{
s_kind.Items.Clear();
s_kind.Items.Add(new ListItem("", ""));
s_kind2.Items.Clear();
s_kind2.Items.Add(new ListItem("", ""));
//buildMultiKind(s_kind, "member_group", 0, "", 1, Model.member.KindLevelMax);
var qry1 = new TreeView().get_data2(_db.member_group.Select(o => new TreeItem()
{
kind = o.kind,
num = o.num,
root = o.root,
range = o.range,
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
if (qry1.Count() > 0)
foreach (var qq in qry1)
s_kind.Items.Add(new ListItem(new TreeView().RptDash(qq.Level) + qq.kind, qq.num.ToString()));
//buildMultiKind(s_kind2, "member_title", 0, "", 1, Model.member.TitleLevelMax);
var qry2 = new TreeView().get_data2(_db.member_title.Select(o => new TreeItem()
{
kind = o.kind,
num = o.num,
root = o.root,
range = o.range,
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
if (qry1.Count() > 0)
foreach (var qq in qry2)
s_kind2.Items.Add(new ListItem(new TreeView().RptDash(qq.Level) + qq.kind, qq.num.ToString()));
}
#endregion
#region GridView相關
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string _demoTxt = (DataBinder.Eval(e.Row.DataItem, "demo") ?? "").ToString();
if (!isStrNull(_demoTxt))
{
Literal demo = (Literal)e.Row.FindControl("demo");
demo.Text = _demoTxt;
string tbID = "dd" + e.Row.RowIndex.ToString();
demo.Text = "
" + demo.Text + "
";
HyperLink demoPanel = (HyperLink)e.Row.FindControl("demoPanel");
demoPanel.Attributes.Add("onclick", "msgbox($('#" + tbID + "').html())");
demoPanel.Visible = true;
}
}
}
#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 = 3, Width = 15, CustomWidth = true },
new Column { Min = 4, Max = 4, Width = 10, CustomWidth = true },
new Column { Min = 5, Max = 6, Width = 15, CustomWidth = true },
new Column { Min = 7, Max = 7, Width = 10, CustomWidth = true },
new Column { Min = 8, Max = 15, Width = 20, CustomWidth = true },
new Column { Min = 16, Max = 17, Width = 10, CustomWidth = true },
new Column { Min = 18, Max = 23, Width = 20, 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("性別"), 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 },
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("備註"), 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 },
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("聯絡電話"), 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 },
new Cell() { CellValue = new CellValue("戶籍地址"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("居住地址"), DataType = CellValues.String }
);
sd.AppendChild(tr);
//查詢要匯出的資料
var qry = _db.members.AsEnumerable();
if (!isStrNull(s_kind.SelectedValue))
qry = qry.Where(o => o.group_kind == Convert.ToInt32(s_kind.SelectedValue));
if (!isStrNull(s_kind2.SelectedValue))
qry = qry.Where(o => o.title_kind == Convert.ToInt32(s_kind2.SelectedValue));
if (!isStrNull(s_u_name.Text))
qry = qry.Where(o => o.u_name.Contains(s_u_name.Text.Trim()));
if (!isStrNull(s_starttime.Value) )
qry = qry.Where(o => o.group_kind.HasValue && o.member_group.starttime >= TimeSpan.Parse(s_starttime.Value));
if (!isStrNull(s_offtime.Value))
qry = qry.Where(o => o.group_kind.HasValue && o.member_group.starttime <= TimeSpan.Parse(s_offtime.Value));
qry = qry.OrderByDescending(o => o.reg_time);
var list = qry.ToList();
if (list.Count > 0)
{
MyWeb.encrypt encrypt = new MyWeb.encrypt();
foreach (var item in list)
{
//新增資料列
tr = new Row();
tr.Append(
new Cell() { CellValue = new CellValue(item.f_num.HasValue ? item.follower.f_number : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.m_number), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.u_name), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.sex), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey( item.id_code)), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.birthday.HasValue ? ValDate(item.birthday.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.blood), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.educational), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.refugedate.HasValue ? ValDate(item.refugedate.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.refuge_name), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.demo), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.admin_num.HasValue ? item.admin.uid : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.takedate.HasValue ? ValDate(item.takedate.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.leavedate.HasValue ? ValDate(item.leavedate.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.salary.HasValue ? ValMoney(item.salary.Value) : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.group_kind.HasValue ? item.member_group.kind : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.title_kind.HasValue ? item.member_title.kind : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.phone)), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.com_email), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.email), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.contact), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.address1), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.address2), DataType = CellValues.String }
);
sd.AppendChild(tr);
}
Model.admin_log admin_log = new Model.admin_log();
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.HR, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => x.m_number + x.u_name).ToList()));
}
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=hr_data.xlsx");
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.End();
}
#endregion
}