348 lines
16 KiB
C#
348 lines
16 KiB
C#
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;
|
||
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
|
||
using System.Collections.Generic;
|
||
using DocumentFormat.OpenXml.Office2013.PowerPoint.Roaming;
|
||
|
||
public partial class admin_follower_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 = 2, Width = 15, CustomWidth = true },
|
||
new Column { Min = 3, Max = 3, Width = 10, CustomWidth = true },
|
||
new Column { Min = 4 , Max = 17, Width = 15, CustomWidth = true },
|
||
new Column { Min = 18, Max = 18, Width = 8, CustomWidth = true },
|
||
new Column { Min = 19, Max = 19, Width = 30, CustomWidth = true },
|
||
new Column { Min = 20, Max = 20, Width = 8, CustomWidth = true },
|
||
new Column { Min = 21, Max = 30, 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("身分別 *"), DataType = CellValues.String },//4
|
||
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 }, //10
|
||
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 }, //14
|
||
new Cell() { CellValue = new CellValue("LINE帳號"), 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 },//18
|
||
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);
|
||
|
||
//國籍
|
||
ws = wb.AddNewPart<WorksheetPart>();
|
||
ws.Worksheet = new Worksheet();
|
||
sheets.Append(new Sheet()
|
||
{
|
||
Id = wb.GetIdOfPart(ws),
|
||
SheetId = 2,
|
||
Name = "國籍資料參考"
|
||
});
|
||
|
||
//設定欄寬
|
||
cu = new Columns();
|
||
cu.Append(
|
||
new Column { Min = 1, Max = 1, Width = 10, CustomWidth = true },
|
||
new Column { Min = 2, Max = 2, Width = 30, CustomWidth = true }
|
||
);
|
||
ws.Worksheet.Append(cu);
|
||
|
||
//建立資料頁
|
||
sd = new SheetData();
|
||
ws.Worksheet.AppendChild(sd);
|
||
|
||
//第一列資料
|
||
tr = new Row();
|
||
tr.Append(
|
||
new Cell() { CellValue = new CellValue("代碼"), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue("中文名稱"), DataType = CellValues.String }
|
||
);
|
||
sd.AppendChild(tr);
|
||
|
||
var qry = _db.countries.AsEnumerable()
|
||
.OrderBy(o => o.range).ThenBy(o => o.name_en).ToList();
|
||
|
||
//國籍資料
|
||
if (qry.Count > 0)
|
||
{
|
||
foreach(var item in qry)
|
||
{
|
||
tr = new Row();
|
||
tr.Append(
|
||
new Cell() { CellValue = new CellValue(item.ID), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.name_zh), DataType = CellValues.String }
|
||
);
|
||
sd.AppendChild(tr);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
HttpContext.Current.Response.Clear();
|
||
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=follower_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;
|
||
}
|
||
|
||
List<Model.country> _country = _db.countries.ToList();
|
||
|
||
MyWeb.encrypt encrypt = new MyWeb.encrypt();
|
||
List<string> addList = new List<string>();
|
||
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;//略過此列
|
||
}
|
||
|
||
|
||
if (!isStrNull(sheet.Cells[currentRow, 1].Text) &&
|
||
!isStrNull(sheet.Cells[currentRow, 2].Text) &&
|
||
!isStrNull(sheet.Cells[currentRow, 3].Text) &&
|
||
!isStrNull(sheet.Cells[currentRow, 4].Text) &&
|
||
!isStrNull(sheet.Cells[currentRow, 18].Text) )
|
||
{
|
||
|
||
//定義日期欄位格式
|
||
sheet.Cells[currentRow, 10].Style.Numberformat.Format = "yyyy-MM-dd";
|
||
sheet.Cells[currentRow, 11].Style.Numberformat.Format = "yyyy-MM-dd";
|
||
sheet.Cells[currentRow, 14].Style.Numberformat.Format = "yyyy-MM-dd";
|
||
|
||
//寫值---
|
||
//新增
|
||
|
||
string _sex = ValString(sheet.Cells[currentRow, 3].Text.Trim()).Replace("眾", "").Trim() + "眾";
|
||
|
||
int _identity_type = (int)Model.follower.type.Personal; //身分別預設
|
||
switch (ValString(sheet.Cells[currentRow, 4].Text.Trim()))
|
||
{
|
||
case "出家眾":
|
||
_identity_type = (int)Model.follower.type.Monk;
|
||
break;
|
||
case "個人":
|
||
_identity_type = (int)Model.follower.type.Personal;
|
||
break;
|
||
case "法人":
|
||
_identity_type = (int)Model.follower.type.Legal;
|
||
break;
|
||
case "往生菩薩":
|
||
_identity_type = (int)Model.follower.type.Bodhisattva;
|
||
break;
|
||
case "蓮友":
|
||
_identity_type = (int)Model.follower.type.Seeker;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
|
||
Model.follower follower = new Model.follower();
|
||
follower.f_number = ValString(sheet.Cells[currentRow, 1].Text.Trim());
|
||
follower.u_name = ValString(sheet.Cells[currentRow, 2].Text.Trim());
|
||
follower.sex = _sex;
|
||
follower.identity_type = _identity_type;//身分別 4
|
||
follower.phone = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 5].Text.Trim()));
|
||
follower.cellphone = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 6].Text.Trim()));
|
||
follower.id_code = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 7].Text.Trim()));
|
||
follower.passport = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 8].Text.Trim()));
|
||
follower.email = ValString(sheet.Cells[currentRow, 9].Text.Trim());
|
||
if (isDate(sheet.Cells[currentRow, 10].Text.Trim())) { follower.birthday = ValDate(sheet.Cells[currentRow, 10].Text.Trim()); }
|
||
if (isDate(sheet.Cells[currentRow, 11].Text.Trim())) { follower.refugedate = ValDate(sheet.Cells[currentRow, 11].Text.Trim()); }
|
||
follower.refuge_name = ValString(sheet.Cells[currentRow, 12].Text.Trim());
|
||
follower.refuge_area = ValString(sheet.Cells[currentRow, 13].Text.Trim());
|
||
|
||
if (isDate(sheet.Cells[currentRow, 14].Text.Trim())) { follower.join_date = ValDate(sheet.Cells[currentRow, 14].Text.Trim()); }
|
||
|
||
follower.socialid1 = ValString(sheet.Cells[currentRow, 15].Text.Trim());
|
||
follower.socialid2 = ValString(sheet.Cells[currentRow, 16].Text.Trim());
|
||
follower.introducer = ValString(sheet.Cells[currentRow, 17].Text.Trim());
|
||
|
||
//檢查國籍代碼是否存在
|
||
var country = _country.Where(x => x.name_zh == ValString(sheet.Cells[currentRow, 18].Text.Trim())).FirstOrDefault();
|
||
if(country != null)
|
||
{
|
||
//follower.country = ValString(sheet.Cells[currentRow, 18].Text.Trim());
|
||
follower.country = country.ID;
|
||
}
|
||
else
|
||
{
|
||
follower.country = null;
|
||
}
|
||
|
||
follower.address = ValString(sheet.Cells[currentRow, 19].Text.Trim());
|
||
follower.blood = ValString(sheet.Cells[currentRow, 20].Text.Trim()).Replace("型", "").ToUpper();
|
||
follower.contactor = ValString(sheet.Cells[currentRow, 21].Text.Trim());
|
||
follower.contactor_phone = encrypt.EncryptAutoKey(ValString(sheet.Cells[currentRow, 22].Text.Trim()));
|
||
follower.demo = ValString(sheet.Cells[currentRow, 23].Text.Trim());
|
||
follower.reg_time = DateTime.Now;
|
||
//follower.admin_log = admin.info.u_id + " " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
|
||
string ChkNewMsg = Model.follower.ChkNewFollower(follower);
|
||
if (string.IsNullOrEmpty(ChkNewMsg))
|
||
{
|
||
_db.followers.Add(follower);
|
||
addList.Add(follower.f_number + follower.u_name);
|
||
}
|
||
else
|
||
{
|
||
jumpLog += "第" + currentRow + "行: "
|
||
+ follower.u_name + " : "
|
||
+ ChkNewMsg + "<br>";
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
jumpLog += "第" + currentRow + "行:欄位輸入不完整<br>";
|
||
}
|
||
|
||
}
|
||
try
|
||
{
|
||
_db.SaveChanges();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
errorLog = ex.InnerException.Message;
|
||
}
|
||
|
||
Model.admin_log admin_log = new Model.admin_log();
|
||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Follower, (int)Model.admin_log.Status.Insert, "Excel匯入:" + admin_log.LogViewBtn(addList));
|
||
|
||
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
|
||
|
||
} |