中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何實現Excel、記事本數據導入到數據庫

發布時間:2021-10-09 09:50:02 來源:億速云 閱讀:147 作者:iii 欄目:開發技術

這篇文章主要介紹“如何實現Excel、記事本數據導入到數據庫”,在日常操作中,相信很多人在如何實現Excel、記事本數據導入到數據庫問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何實現Excel、記事本數據導入到數據庫”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

前臺代碼:

復制代碼 代碼如下:

<div class="tab-content detail" id="divSecond" runat="server" visible="false">
            <fieldset >
             <p>
                <label>
                  選擇文件</label>
                  <asp:FileUpload ID="FileUpload1" runat="server"  /><asp:RequiredFieldValidator
                      ID="RequiredFieldValidator1" runat="server" ErrorMessage="請選擇要提交的Excel文件" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
              </p>
              <p>
              <label>文件示例</label><img src="../images/ex_excel.jpg" />
              </p>
            </fieldset>
            <div class="clear"></div>
           <div class="clear"></div>
            <div >
                <asp:Button ID="btnExcel" runat="server" Text="提 交" CssClass="button" 
                    onclick="btnExcel_Click"  />
                  &nbsp;&nbsp;&nbsp;
                <input class="button" type="button" value="返 回" onclick="javascript:window.location.href='CartList.aspx'" />
            </div>
          </div>
          <div class="tab-content detail" id="divThird" runat="server" visible="false">
            <fieldset >
              <p>
                <label>
                  選擇文件</label>
                  <asp:FileUpload ID="FileUpload2" runat="server" /> <asp:RequiredFieldValidator
                      ID="RequiredFieldValidator2" runat="server" ErrorMessage="請選擇要提交的tTXT文件"
                      ControlToValidate="FileUpload2"></asp:RequiredFieldValidator>
              </p>
              <p>
              <label>文件示例</label><img src="../images/ex_txt.jpg" />
              </p>
            </fieldset>
            <div class="clear"></div>
           <div class="clear"></div>
            <div >
                <asp:Button ID="btnNotepad" runat="server" Text="提 交" CssClass="button"
                    onclick="btnNotepad_Click"  />
                  &nbsp;&nbsp;&nbsp;
                <input class="button" type="button" value="返 回" onclick="javascript:window.location.href='CartList.aspx'" />
            </div>
          </div>


后臺代碼

復制代碼 代碼如下:

//Excel上傳
        protected void btnExcel_Click(object sender, EventArgs e)
        {         
              string backStr= UploadFile(FileUpload1, "Excel",1);
              if (backStr == "-1")
              {
                   //這里是放返回消息的,改為對應放消息的方式就行了,Js這個類就不上傳了
                  Js.ShowSureMsgBox(this.Page,"請選擇要提交的Excel文件");
                  return;
              }
              else if (backStr == "-2")
              {
                  Js.ShowSureMsgBox(this.Page, "請選擇.xls或.xlsx類型文件");
                  return;
              }
              else
              {
                  string url = backStr;   //絕對路徑
                  DataTable dt = ExcelToDataSet(url);
                  if (dt.Rows.Count > 0)
                  {
                     for (int i = 0; i < dt.Rows.Count; i++)
                     {
                          //讀取每行數據         
                           string phoneNum= dt.Rows[i][0].ToString();            
                     }           
                  }
                  else
                  {
                      Js.ShowSureMsgBox(this.Page, "文件內容為空");
                  }
                  File.Delete(url);  //刪除上傳的文件
              }
        }
        //記事本上傳
        protected void btnNotepad_Click(object sender, EventArgs e)
        {
            string backStr = UploadFile(FileUpload2, "TXT",2);
            if (backStr == "-1")
            {
                Js.ShowSureMsgBox(this.Page, "請選擇要提交的TXT文件");
                return;
            }
            else if (backStr == "-2")
            {
                Js.ShowSureMsgBox(this.Page, "請選擇.txt類型文件");
                return;
            }
            else
            {
                string url = backStr;   //絕對路徑
                DataTable dt = ReadTXT(url);
                if (dt.Rows.Count > 0)
                {
                     for (int i = 0; i < dt.Rows.Count; i++)
                     {
                          //讀取每行數據         
                           string phoneNum= dt.Rows[i][0].ToString();            
                     }                
                }
                else
                {
                    Js.ShowSureMsgBox(this.Page, "文件內容為空");
                }
                File.Delete(url);  //刪除上傳的文件
            }
        }

        //上傳文件
        public string UploadFile(FileUpload FileUploadName, string varfilename,int type)
        {
                if (FileUploadName.HasFile)//判斷是否有上傳文件
                {
                    string fileExtension = System.IO.Path.GetExtension(FileUploadName.FileName).ToLower();//獲取文件的后綴名
                    if (type == 1)
                    {
                        if (fileExtension != ".xls" && fileExtension != ".xlsx")
                        {
                            return "-2";
                        }
                    }
                    if (type == 2)
                    {
                        if (fileExtension != ".txt")
                        {
                            return "-2";
                        }
                    }                 
                    string fpath = System.Web.HttpContext.Current.Server.MapPath("/Manager/Uploadfiles/" + varfilename + "/");//圖片存儲文件夾路徑,需要按照不同的需要進行相應的修改
                    if (!Directory.Exists(fpath))//查看存儲路徑的文件是否存在
                    {
                        Directory.CreateDirectory(fpath);   //創建文件夾,并上傳文件
                    }
                    string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");//使用時間定義上傳圖片的名字
                    string picturename = time + fileExtension;
                    string newFilePath = fpath + picturename; //文件保存路徑
                    FileUploadName.SaveAs(newFilePath);
                    return newFilePath;   //絕對路徑

                }
                else
                {
                    return "-1";   //沒有文件
                }           
        }
        //讀取Excel數據
         public DataTable ExcelToDataSet(string filename)
        {
            string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filename + ";Extended Properties=Excel 8.0";
            OleDbConnection conn = new OleDbConnection(strCon);
            conn.Open();
            //返回Excel的架構,包括各個sheet表的名稱,類型,創建時間和修改時間等 
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串數組
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            OleDbDataAdapter myCommand = null;
            DataTable dt = new DataTable();
            //從指定的表明查詢數據,可先把所有表明列出來供用戶選擇
            string strExcel = "select * from [" + strTableNames[0] + "]";
            myCommand = new OleDbDataAdapter(strExcel, strCon);
            myCommand.Fill(dt);
            conn.Close();

            return dt;
        }
        //讀取記事本數據
         public DataTable ReadTXT(string dirTXT)
         {
             StreamReader objReader = new StreamReader(dirTXT);
             System.Data.DataTable dt = new System.Data.DataTable();
             dt.Columns.Add("DN", System.Type.GetType("System.String"));
             string sLine = "";
             while (sLine != null)
             {
                 sLine = objReader.ReadLine();
                 if (sLine != null && !sLine.Equals(""))
                 {
                     DataRow dr = dt.NewRow();
                     dr[0] = sLine;
                     dt.Rows.Add(dr);
                 }
             }
             objReader.Close();
             return dt;
         }

到此,關于“如何實現Excel、記事本數據導入到數據庫”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

梅州市| 若尔盖县| 敦煌市| 嘉禾县| 桐梓县| 米脂县| 沙坪坝区| 什邡市| 泰顺县| 通州区| 牟定县| 宜章县| 乌拉特后旗| 孝义市| 通城县| 元朗区| 仙居县| 东莞市| 曲松县| 玛沁县| 尚义县| 神池县| 柘城县| 郸城县| 中方县| 九江市| 江山市| 阿尔山市| 射阳县| 大港区| 贡觉县| 汝城县| 肥西县| 寻甸| 弥勒县| 文登市| 浮山县| 江北区| 栖霞市| 察哈| 武平县|