标签:style blog http color io os ar for 文件
代码:
/// <summary> /// Excel文件导入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpload_Click(object sender, EventArgs e) { string msg = string.Empty; try { if (FileUpload1.HasFile) { string fileName = Server.HtmlEncode(FileUpload1.FileName); string fileExt = System.IO.Path.GetExtension(fileName).ToLower(); int result = 0; // 检查上传的文件类型 if (fileExt != ".xls" && fileExt != ".xlsx") { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "show(\"请选择需要上传的Excel文件\")", true); return; } List<AllianceSpecialCommissionTicketsResource> list = new List<AllianceSpecialCommissionTicketsResource>(); long SCRuleID = Convert.ToInt64(this.hid_SCRuleID.Value); // NPOI Stream fileStream = FileUpload1.PostedFile.InputStream; IWorkbook workbook = null; if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fileStream); // .xlsx } else { workbook = new HSSFWorkbook(fileStream); // .xls } ISheet sheet = workbook.GetSheetAt(0); for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row != null) { if (row.Cells.Count == 7) { for (var j = 0; j < 7; j++) { string c = CommonFunc.ConvertObjectToString(row.GetCell(j)).ToString().Trim(); string colName = GetColName(j); if (string.IsNullOrEmpty(c)) { msg = "文件第" + (i + 1) + "行" + colName + "不能为空!"; break; } if (j % 2 == 0 && j != 6) { //必须为int类型 int re = 0; if (!Int32.TryParse(c, out re)) { msg = "文件第" + (i + 1) + "行" + colName + "格式错误!"; break; } } //必须为double类型 if (j == 6) { double re = 0; if (Double.TryParse(c, out re)) { if (re >= 100) { msg = "文件第" + (i + 1) + "行分成率不能超过100%!"; break; } } else { msg = "文件第" + (i + 1) + "行" + colName + "格式错误!"; break; } } } if (string.IsNullOrEmpty(msg)) { AllianceSpecialCommissionTicketsResource r = new AllianceSpecialCommissionTicketsResource(); r.SCRuleID = SCRuleID; r.ScenicID = CommonFunc.ConvertObjectToInt32(row.GetCell(0)); r.ScenicName = CommonFunc.ConvertObjectToString(row.GetCell(1)).Trim(); r.TicketID = CommonFunc.ConvertObjectToInt32(row.GetCell(2)); r.TicketName = CommonFunc.ConvertObjectToString(row.GetCell(3)).Trim(); r.ResourceID = CommonFunc.ConvertObjectToInt32(row.GetCell(4)); r.ResourceName = CommonFunc.ConvertObjectToString(row.GetCell(5)).Trim(); r.Rate = CommonFunc.ConvertObjectToDecimalSingle(row.GetCell(6)); r.Rate = r.Rate / 100; list.Add(r); } } //防止空行 else if (row.Cells.Count == 1) { string c1 = CommonFunc.ConvertObjectToString(row.GetCell(row.LastCellNum - 1)).ToString().Trim(); if (string.IsNullOrEmpty(c1)) { //空行 } else { msg = "文件第" + (i + 1) + "行数据有误!"; } } else { msg = "文件第" + (i + 1) + "行数据有误!"; } if (!string.IsNullOrEmpty(msg)) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "show(\"" + msg + "\");isS=true;", true); return; } } } using (TransactionScope scope = new TransactionScope()) { AllianceSpecialCommissionRules rule = rDao.FindByPk(SCRuleID); if (rule.ApproveStatus != "W") { rule.ApproveStatus = "W"; rule.ApproveTime = null; rule.Approver = null; result = rDao.UpdateAllianceSpecialCommissionRules(rule); } //删除旧数据 List<AllianceSpecialCommissionTicketsResource> trs = tDao.GetAll().Where(x => x.SCRuleID == SCRuleID).ToList(); foreach (var t in trs) { result = tDao.DeleteAllianceSpecialCommissionTicketsResource(t); } //添加新数据 foreach (var r in list) { result = tDao.InsertAllianceSpecialCommissionTicketsResource(r); } if (result == 0) { msg = "Excel导入特殊门票成功!"; scope.Complete(); } else { msg = "Excel导入特殊门票失败!"; } } } else { msg = "请选择上传文件!"; } } catch (Exception ex) { logger.Debug(ex); msg = "Excel导入特殊门票失败!"; } Page.ClientScript.RegisterStartupScript(this.GetType(), "", "show(\"" + msg + "\");isS=true;", true); } protected string GetColName(int k) { string msg = string.Empty; switch (k) { case 0: msg = "景点ID"; break; case 1: msg = "景点名称"; break; case 2: msg = "门票ID"; break; case 3: msg = "门票名称"; break; case 4: msg = "资源ID"; break; case 5: msg = "资源名称"; break; case 6: msg = "分成率"; break; } return msg; }
整个文件
http://files.cnblogs.com/hongdada/SpecialList.7z
标签:style blog http color io os ar for 文件
原文地址:http://www.cnblogs.com/hongdada/p/3986084.html