migrate to new git
This commit is contained in:
348
web/admin/HR/import.aspx.cs
Normal file
348
web/admin/HR/import.aspx.cs
Normal file
@@ -0,0 +1,348 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.IO;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using System.Linq;
|
||||
using System.Drawing;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.Web;
|
||||
using OfficeOpenXml;
|
||||
using OfficeOpenXml.Style;
|
||||
|
||||
|
||||
public partial class admin_hr_import : MyWeb.config
|
||||
{
|
||||
private Model.ezEntities _db = new Model.ezEntities();
|
||||
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
if (!IsPostBack)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 下載空excel
|
||||
|
||||
|
||||
protected void databaseExport_Click(object sender, EventArgs e)
|
||||
{
|
||||
excelEPPlus();
|
||||
}
|
||||
protected void excelEPPlus() //下載空excel
|
||||
{
|
||||
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 = 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 ws2 = wb.AddNewPart<WorksheetPart>();
|
||||
ws2.Worksheet = new Worksheet();
|
||||
sheets.Append(new Sheet()
|
||||
{
|
||||
Id = wb.GetIdOfPart(ws2),
|
||||
SheetId = 2,
|
||||
Name = "組別"
|
||||
});
|
||||
//設定欄寬
|
||||
cu = new Columns();
|
||||
cu.Append(
|
||||
new Column { Min = 1, Max = 1, Width = 7, CustomWidth = true },
|
||||
new Column { Min = 2, Max = 2, Width = 15, CustomWidth = true }
|
||||
);
|
||||
ws2.Worksheet.Append(cu);
|
||||
|
||||
//建立資料頁
|
||||
var sd2 = new SheetData();
|
||||
ws2.Worksheet.AppendChild(sd2);
|
||||
//第一列資料
|
||||
tr = new Row();
|
||||
tr.Append(
|
||||
new Cell() { CellValue = new CellValue("流水號"), DataType = CellValues.String },
|
||||
new Cell() { CellValue = new CellValue("組別名稱"), DataType = CellValues.String }
|
||||
);
|
||||
sd2.AppendChild(tr);
|
||||
//查詢要匯出的資料
|
||||
var list = _db.member_group.AsEnumerable().ToList();
|
||||
if (list.Count > 0)
|
||||
{
|
||||
foreach (var item in list)
|
||||
{
|
||||
//新增資料列
|
||||
tr = new Row();
|
||||
tr.Append(
|
||||
new Cell() { CellValue = new CellValue(item.num.ToString()), DataType = CellValues.String },
|
||||
new Cell() { CellValue = new CellValue(item.kind.ToString()), DataType = CellValues.String }
|
||||
);
|
||||
sd2.AppendChild(tr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//================職稱
|
||||
|
||||
//建立第3個頁籤
|
||||
var ws3 = wb.AddNewPart<WorksheetPart>();
|
||||
ws3.Worksheet = new Worksheet();
|
||||
sheets.Append(new Sheet()
|
||||
{
|
||||
Id = wb.GetIdOfPart(ws3),
|
||||
SheetId = 3,
|
||||
Name = "職稱"
|
||||
});
|
||||
//設定欄寬
|
||||
cu = new Columns();
|
||||
cu.Append(
|
||||
new Column { Min = 1, Max = 1, Width = 7, CustomWidth = true },
|
||||
new Column { Min = 2, Max = 2, Width = 15, CustomWidth = true }
|
||||
);
|
||||
ws3.Worksheet.Append(cu);
|
||||
|
||||
//建立資料頁
|
||||
var sd3 = new SheetData();
|
||||
ws3.Worksheet.AppendChild(sd3);
|
||||
//第一列資料
|
||||
tr = new Row();
|
||||
tr.Append(
|
||||
new Cell() { CellValue = new CellValue("流水號"), DataType = CellValues.String },
|
||||
new Cell() { CellValue = new CellValue("職稱"), DataType = CellValues.String }
|
||||
);
|
||||
sd3.AppendChild(tr);
|
||||
//查詢要匯出的資料
|
||||
var list2 = _db.member_title.AsEnumerable().ToList();
|
||||
if (list2.Count > 0)
|
||||
{
|
||||
foreach (var item in list2)
|
||||
{
|
||||
//新增資料列
|
||||
tr = new Row();
|
||||
tr.Append(
|
||||
new Cell() { CellValue = new CellValue(item.num.ToString()), DataType = CellValues.String },
|
||||
new Cell() { CellValue = new CellValue(item.kind.ToString()), DataType = CellValues.String }
|
||||
);
|
||||
sd3.AppendChild(tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HttpContext.Current.Response.Clear();
|
||||
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=hr_import.xlsx");
|
||||
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
|
||||
HttpContext.Current.Response.End();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 匯入資料
|
||||
protected void submitButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (FileUpload1.HasFile)
|
||||
{
|
||||
string errorLog = "";
|
||||
string jumpLog = "";
|
||||
msg.Text = "";
|
||||
//開檔
|
||||
//載入Excel檔案
|
||||
using (ExcelPackage ep = new ExcelPackage(FileUpload1.PostedFile.InputStream))
|
||||
{
|
||||
|
||||
ExcelWorksheet sheet = ep.Workbook.Worksheets[1];//取得Sheet1
|
||||
int startRowNumber = sheet.Dimension.Start.Row;//起始列編號,從1算起
|
||||
int endRowNumber = sheet.Dimension.End.Row;//結束列編號,從1算起
|
||||
int startColumn = sheet.Dimension.Start.Column;//開始欄編號,從1算起
|
||||
int endColumn = sheet.Dimension.End.Column;//結束欄編號,從1算起
|
||||
|
||||
|
||||
bool isHeader = true;
|
||||
if (isHeader)//有包含標題
|
||||
{
|
||||
startRowNumber += 1;
|
||||
}
|
||||
|
||||
var fDt = _db.followers.AsEnumerable().ToList(); //信眾
|
||||
var aDt = _db.admins.AsEnumerable().ToList(); //後台系統
|
||||
var gDt = _db.member_group.AsEnumerable().Select(x => x.num).ToList(); //組別
|
||||
var tDt = _db.member_title.AsEnumerable().Select(x => x.num).ToList(); //職稱
|
||||
MyWeb.encrypt encrypt = new MyWeb.encrypt();
|
||||
|
||||
for (int currentRow = startRowNumber; currentRow <= endRowNumber; currentRow++)
|
||||
{
|
||||
ExcelRange range = sheet.Cells[currentRow, startColumn, currentRow, endColumn];//抓出目前的Excel列
|
||||
if (range.Any(c => !string.IsNullOrEmpty(c.Text)) == false)//這是一個完全空白列(使用者用Delete鍵刪除動作)
|
||||
{
|
||||
continue;//略過此列
|
||||
}
|
||||
|
||||
//--------- pro_num 判斷新增修改-----
|
||||
|
||||
if (!isStrNull(sheet.Cells[currentRow, 2].Text) && !isStrNull(sheet.Cells[currentRow, 3].Text))
|
||||
{
|
||||
|
||||
int f_num = fDt.Where(x => x.f_number == ValString(sheet.Cells[currentRow, 1].Text.Trim())).Select(x => x.num).FirstOrDefault(); //信眾編號id
|
||||
int a_num = aDt.Where(x => x.u_id == ValString(sheet.Cells[currentRow, 12].Text.Trim())).Select(x => x.num).FirstOrDefault(); //系統帳號id
|
||||
int g_sum = gDt.Where(x => x.ToString() == ValString(sheet.Cells[currentRow, 16].Text.Trim())).FirstOrDefault(); //組別
|
||||
int t_sum = tDt.Where(x => x.ToString() == ValString(sheet.Cells[currentRow, 17].Text.Trim())).FirstOrDefault(); //職稱
|
||||
|
||||
//定義日期欄位格式
|
||||
sheet.Cells[currentRow, 6].Style.Numberformat.Format = "yyyy-MM-dd";
|
||||
sheet.Cells[currentRow, 9].Style.Numberformat.Format = "yyyy-MM-dd";
|
||||
sheet.Cells[currentRow, 12].Style.Numberformat.Format = "yyyy-MM-dd";
|
||||
sheet.Cells[currentRow, 14].Style.Numberformat.Format = "yyyy-MM-dd";
|
||||
|
||||
//寫值---
|
||||
//新增
|
||||
|
||||
|
||||
Model.member member = new Model.member();//新增
|
||||
if (f_num > 0) { member.f_num = f_num; }
|
||||
member.m_number = ValString(sheet.Cells[currentRow, 2].Text.Trim());
|
||||
member.u_name = ValString(sheet.Cells[currentRow, 3].Text.Trim());
|
||||
member.sex = ValString(sheet.Cells[currentRow, 4].Text.Trim());
|
||||
member.id_code = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 5].Text.Trim()));
|
||||
if (isDate(sheet.Cells[currentRow, 6].Text.Trim())) { member.birthday = ValDate(sheet.Cells[currentRow, 6].Text.Trim()); }
|
||||
member.blood = ValString(sheet.Cells[currentRow, 7].Text.Trim()).Replace("型", "").ToUpper();
|
||||
member.educational = ValString(sheet.Cells[currentRow, 8].Text.Trim());
|
||||
if (isDate(sheet.Cells[currentRow, 9].Text.Trim())) { member.refugedate = ValDate(sheet.Cells[currentRow, 9].Text.Trim()); }
|
||||
member.refuge_name = ValString(sheet.Cells[currentRow, 10].Text.Trim());
|
||||
member.demo = ValString(sheet.Cells[currentRow, 11].Text.Trim());
|
||||
if (a_num > 0) { member.admin_num = a_num; }
|
||||
if (isDate(sheet.Cells[currentRow, 13].Text.Trim())) { member.takedate = ValDate(sheet.Cells[currentRow, 13].Text.Trim()); }
|
||||
if (isDate(sheet.Cells[currentRow, 14].Text.Trim())) { member.leavedate = ValDate(sheet.Cells[currentRow, 14].Text.Trim()); }
|
||||
if (isDate(sheet.Cells[currentRow, 15].Text.Trim())) { member.salary = ValFloat(ValString(sheet.Cells[currentRow, 15].Text.Trim()).Replace("元", "").Replace("$", "").Trim()); }
|
||||
if (g_sum > 0) { member.group_kind = g_sum; }
|
||||
if (t_sum > 0) { member.title_kind = t_sum; }
|
||||
member.phone = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 18].Text.Trim()));
|
||||
member.com_email = ValString(sheet.Cells[currentRow, 19].Text.Trim());
|
||||
member.email = ValString(sheet.Cells[currentRow, 20].Text.Trim());
|
||||
member.contact = ValString(sheet.Cells[currentRow, 21].Text.Trim());
|
||||
member.address1 = ValString(sheet.Cells[currentRow, 22].Text.Trim());
|
||||
member.address2 = ValString(sheet.Cells[currentRow, 23].Text.Trim());
|
||||
|
||||
_db.members.Add(member);
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
jumpLog += "第" + currentRow + "行,欄位輸入不完整";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_db.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
errorLog = ex.InnerException.Message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!isStrNull(errorLog))
|
||||
{
|
||||
L_msg.Type = alert_type.danger;
|
||||
L_msg.Text = "匯入失敗";
|
||||
msg.Text = errorLog;
|
||||
if (!isStrNull(jumpLog))
|
||||
{
|
||||
msg.Text += "<br>資料匯入不齊全:<br>" + jumpLog;
|
||||
}
|
||||
}
|
||||
else if (!isStrNull(jumpLog))
|
||||
{
|
||||
L_msg.Type = alert_type.warning;
|
||||
L_msg.Text = "資料匯入不齊全";
|
||||
msg.Text = "未匯入:<br>" + jumpLog;
|
||||
}
|
||||
else
|
||||
{
|
||||
L_msg.Type = alert_type.success;
|
||||
L_msg.Text = "匯入成功";
|
||||
}
|
||||
|
||||
|
||||
}//end using
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ScriptMsg("請上傳excel檔", "", msgIcon.error);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user