public HttpResponseBase KeyWordUploadExcel() { string newName = string.Empty; string json = string.Empty; List<SphinxKeywordQuery> store = new List<SphinxKeywordQuery>(); HashEncrypt hashpt = new HashEncrypt(); try { DTIlocExcel.Clear(); DTIlocExcel.Columns.Clear(); DTIlocExcel.Columns.Add("keyword", typeof(String)); DTIlocExcel.Columns.Add("foodkeyword(1:是食安關鍵字,0不是食安關鍵字)", typeof(String)); DTIlocExcel.Columns.Add("failureMessage", typeof(String)); int result = 0; int count = 0;//總匯入數 //int entercount = 0;//插入失敗個數 int errorcount = 0;//數據異常個數 int repeat = 0;//數據已存在個數 string create_user = (Session["caller"] as Caller).user_username; if (Request.Files["ImportExcelFile"] != null && Request.Files["ImportExcelFile"].ContentLength > 0) { HttpPostedFileBase excelFile = Request.Files["ImportExcelFile"]; newName = Server.MapPath(excelPath) + excelFile.FileName; excelFile.SaveAs(newName); DataTable dt = new DataTable(); NPOI4ExcelHelper helper = new NPOI4ExcelHelper(newName); dt = helper.SheetData(); if (dt.Rows.Count > 0) { swMgr = new SystemKeyWordMgr(SqlConnectionString); int i = 0; foreach (DataRow dr in dt.Rows) { StringBuilder strsql = new StringBuilder(); SphinxKeywordQuery query = new BLL.gigade.Model.Query.SphinxKeywordQuery(); i++; try { if (!string.IsNullOrEmpty(dr[0].ToString()) && !string.IsNullOrEmpty(dr[1].ToString())) { int row_id_exsit = swMgr.CheckKeyWordExsit(dr[0].ToString());//判斷關鍵字是否存在 query.user_name = create_user; query.key_word = dr[0].ToString(); query.flag = dr[1].ToString(); query.operateType = 1; if (row_id_exsit > 0) { DataRow drtwo = DTIlocExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "系統關鍵字已存在"; DTIlocExcel.Rows.Add(drtwo); repeat++; continue; } else//關鍵字不存在 { if (query.flag == "0" || query.flag == "1") { byte[] strBt = Encoding.Unicode.GetBytes(query.key_word); //關鍵字最多為25個中文字或50個英文字 if (strBt.Length <= 50) { result = swMgr.SaveSystemKeyWord(query); if (result > 0) { count++; continue; } else { DataRow drtwo = DTIlocExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "關鍵字插入數據庫時失敗"; DTIlocExcel.Rows.Add(drtwo); errorcount++; continue; } } else { DataRow drtwo = DTIlocExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "關鍵字最多為25個中文字或50個英文字"; DTIlocExcel.Rows.Add(drtwo); errorcount++; continue; } } else { DataRow drtwo = DTIlocExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "'foodkeyword的值只能為0或1'"; DTIlocExcel.Rows.Add(drtwo); errorcount++; continue; } } } else { DataRow drtwo = DTIlocExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "keyword或foodkeyword不符合格式"; DTIlocExcel.Rows.Add(drtwo); errorcount++; continue; } } catch { DataRow drtwo = DTIlocExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "數據異常"; DTIlocExcel.Rows.Add(drtwo); errorcount++; continue; } } if (count > 0) { json = "{success:true,total:" + count + ",error:" + errorcount + ",repeat:" + repeat+"}"; } else { json = "{success:true,total:" + 0 + ",error:" + errorcount + ",repeat:" + repeat+"}"; } } else { json = "{success:true,total:" + 0 + ",error:" + 0 + ",repeat" + repeat + "}"; } } } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); json = "{success:false,data:" + "" + "}"; } this.Response.Clear(); this.Response.Write(json); this.Response.End(); return this.Response; }
public HttpResponseBase SaveSystemKeyWord() { string json = string.Empty; ///返回的状态 int state = 0; List<SphinxKeywordQuery> stores = new List<SphinxKeywordQuery>(); SphinxKeywordQuery query = new SphinxKeywordQuery(); swMgr = new SystemKeyWordMgr(SqlConnectionString); try { if (!string.IsNullOrEmpty(Request.Params["row_id"])) { query.row_id = Convert.ToInt32(Request.Params["row_id"]); } if (!string.IsNullOrEmpty(Request.Params["operateType"])) { query.operateType = Convert.ToInt32(Request.Params["operateType"]); } if (!string.IsNullOrEmpty(Request.Params["flag"])) { query.flag = Request.Params["flag"]; } if (!string.IsNullOrEmpty(Request.Params["key_word"])) { query.key_word = Request.Params["key_word"]; } query.user_name = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_username; state = swMgr.SaveSystemKeyWord(query); //IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); ////这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式 //timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; json = "{success:true,state:" + state + "}";//返回json數據 } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); json = "{success:false}"; } this.Response.Clear(); this.Response.Write(json); this.Response.End(); return this.Response; }