導入模板下載
ImportTemplateDownload: function () {
window.location.href = "../../../Areas/CompanyManage/Files/銀行導入模板.xls";
},
導入的input 標籤
<input id="fileTmp" type="file" accept="application/vnd.ms-excel" name="file" style="display:none" />
//導入
dataImport: function () {
var that=this;
$("#fileTmp").prop("value","");
$("#fileTmp").change(function () {
$("#load").show();
$.ajaxFileUpload({
url: "/CompanyManage/BankBranch/BankBranchImport",
data: { LegalUnitID: that.selectedCompanyGUID,},
secureuri: false,
fileElementId: 'fileTmp',
dataType: 'json',
success: function (res)
{
$("#load").hide();
that.bankBranchInfoList.read();
that.bankBranchInfoList.page(1);
if (res && res.Head && res.Head.Ret == 0) {
messageBox.show({ message: res.Head.Msg }, "upload-success");
} else {
messageBox.show({ message: res.Head.Msg }, "info");
}
},
error: function (data, status, e)
{
$("#data").hide();
that.bankBranchInfoList.read();
that.bankBranchInfoList.page(1);
messageBox.show({ message: data.Head.Msg }, "info");
}
});
});
$("#fileTmp").click();
},
後臺處理
[Data, HttpPost]
public JsonResult BankBranchImport(string LegalUnitID)
{
var rep = new ResponseContext<BANKACCOUNTModel>();
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
var file = files[0];
if (file != null && file.ContentLength > 0)
{
string folderpath = "/AddExcel/";
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(Server.MapPath(folderpath));
}
string ext1 = Path.GetExtension(file.FileName);
if (ext1 != ".xls" && ext1 != ".xlsx")
{
rep.Head.Code = ErrCode.ImportFailure;
rep.Head.Msg = "請上傳.xls|.xlsx格式的文件";
return Json(rep);
}
Random ra = new Random(4);
string name = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ra.Next();
string ext = Path.GetExtension(file.FileName);
string downpath = folderpath + name + ext;
string filepath = Server.MapPath(folderpath) + name + ext;
file.SaveAs(filepath);
List<BANKACCOUNTINFOModel> listModel = new List<BANKACCOUNTINFOModel>();
try
{
IWorkbook workbook = new HSSFWorkbook(System.IO.File.Open(filepath, FileMode.Open));
//用完刪除
System.IO.File.Delete(filepath);
ISheet sheet = workbook.GetSheetAt(0);
IRow headerRow = sheet.GetRow(0);
int cellCount = headerRow.LastCellNum;
int rowCount = sheet.LastRowNum;
for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
{
IRow row = sheet.GetRow(i);
if (row != null)
{
BANKACCOUNTINFOModel item = new BANKACCOUNTINFOModel();
item.ACCOUNT_INFO_ID = Guid.NewGuid().ToString(); ;
item.BANK_NAME = row.GetCell(0).ToString();
item.BANK_NETWORK = row.GetCell(1).ToString();
item.BANK_PROVINCE = row.GetCell(2).ToString();
item.BANK_CITY = row.GetCell(3).ToString();
item.BANK_PROVINCE_CD = row.GetCell(4).ToString();
item.BANK_LINE_NO = row.GetCell(5).ToString();
item.LegalUnitID = LegalUnitID;
item.R_RECORD_STATUS = 1;
item.R_RECORD_CREATE_DATE = System.DateTime.Now;
item.R_RECORD_CREATE_USER = UserContext.UserName;
item.R_RECORD_UPDATE_DATE = System.DateTime.Now;
item.R_RECORD_UPDATE_USER = UserContext.UserName;
var isExistModel = backAccountInfoService.FirstOrDefault(t => t.LegalUnitID == item.LegalUnitID&&t.BANK_LINE_NO==item.BANK_LINE_NO&&t.BANK_PROVINCE_CD == item.BANK_PROVINCE_CD);
if(isExistModel!=null)
{
//存在 不導入 寫入log
Log.LogMsg("【導入銀行網點數據】", "【導入銀行網點數據失敗】詳細信息:" + item.BANK_LINE_NO+" 存在!");
continue;
}
else
{
//不存在 導入
listModel.Add(item);
}
}
}
if(listModel!=null&&listModel.Count>0)
{
backAccountInfoService.BeachAddData(listModel);
}
return Json(rep);
}
catch (Exception ex)
{
Log.LogMsg("【導入銀行網點數據】", "【導入銀行網點數據報錯】詳細信息:" + ex.Message);
rep.Head.Code = ErrCode.ImportFailure;
rep.Head.Msg = ex.Message;
return Json(rep);
}
}
else
{
rep.Head.Code = ErrCode.ImportFailure;
rep.Head.Msg = "請上傳文件";
return Json(rep);
}
}