Example #1
0
        //excel
        public HttpResponseBase RedirectUploadExcel()
        {
            string newName = string.Empty;
            string json = string.Empty;
            _redirectMgr = new RedirectMgr(mySqlConnectionString);
            try
            {
                DTExcels.Clear();
                DTExcels.Columns.Clear();
                DTExcels.Columns.Add("連結名稱", typeof(String));
                DTExcels.Columns.Add("目的連結", typeof(String));
                DTExcels.Columns.Add("連結狀態", typeof(String));//1表示正常2表示停用 其他都為正常
                int count = 0;//總匯入數
                int errorcount = 0;//數據異常個數
                int create_user = (Session["caller"] as Caller).user_id;
                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 && !string.IsNullOrEmpty(dt.Rows[0][0].ToString()) && !string.IsNullOrEmpty(dt.Rows[0][1].ToString()))
                    {
                            foreach (DataRow dr in dt.Rows)
                            {
                                if (!string.IsNullOrEmpty(dr[0].ToString()) && !string.IsNullOrEmpty(dr[1].ToString()))
                                {
                                    #region 匯入數據
                                    RedirectQuery rd = new RedirectQuery();
                                    rd.group_id = Convert.ToUInt32(Request.Params["group_id"]);//得到群組
                                    try
                                    {
                                        int linkstatus = 0;
                                        if (!int.TryParse(dr[2].ToString(), out linkstatus))
                                        {
                                            linkstatus = 1;
                                        }
                                        if (linkstatus == 2)
                                        {
                                            linkstatus = 2;
                                            rd.redirect_name = dr[0].ToString();//連接名稱
                                            rd.redirect_url = dr[1].ToString();//目的連接
                                            rd.redirect_status = Convert.ToUInt32(linkstatus);//連接狀態
                                        }
                                        else
                                        {
                                            linkstatus = 1;
                                            rd.redirect_name = dr[0].ToString();
                                            rd.redirect_url = dr[1].ToString();
                                            rd.redirect_status = Convert.ToUInt32(linkstatus);
                                        }
                                        rd.redirect_createdate = Convert.ToUInt32(CommonFunction.GetPHPTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                                        rd.redirect_updatedate = Convert.ToUInt32(CommonFunction.GetPHPTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                                        rd.redirect_ipfrom = CommonFunction.GetClientIP();
                                        //獲取Serial裡面的redirect_id
                                        _serialMgr = new SerialMgr(mySqlConnectionString);
                                        rd.redirect_id = uint.Parse((_serialMgr.GetSerialById(4).Serial_Value + 1).ToString());
                                    }
                                    catch (Exception ex)
                                    {//不是正常數據時候直接帶1(正常)
                                        int linkstatus = 1;
                                        rd.redirect_name = dr[0].ToString();
                                        rd.redirect_url = dr[1].ToString();
                                        rd.redirect_status = Convert.ToUInt32(linkstatus);
                                    }
                                    int result = 0;//如果獲取到的連結名稱和url為空就不新增
                                    if (!string.IsNullOrEmpty(rd.redirect_name) && !string.IsNullOrEmpty(rd.redirect_url))//&& Regex.IsMatch(rd.redirect_url,'')
                                    {

                                        result = _redirectMgr.EnterInotRedirect(rd);
                                    }
                                    if (result > 0)
                                    {//插入成功一條并修改serial表數據
                                        count++;
                                        Serial serial = new Serial();
                                        serial.Serial_id = 4;
                                        serial.Serial_Value = rd.redirect_id;
                                        _serialMgr.Update(serial);
                                    }
                                    else
                                    {
                                        DataRow drs = DTExcels.NewRow();
                                        drs[0] = dr[0].ToString();
                                        drs[1] = dr[1].ToString();
                                        drs[2] = dr[2].ToString();
                                        DTExcels.Rows.Add(drs);
                                        errorcount++;//插入失敗一條
                                    }
                                    #endregion
                                }
                                json = "{success:true,count:" + count + ",errorcount:" + errorcount + "}";
                            }
                    }                
                    else
                    {
                        json = "{success:true,total:" + 0 + ",error:" + 0 + ",entercount:" + 0 + "}";
                    }
                }
            }
            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;
        }