主頁 > 知識庫 > Excel、記事本數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)方法

Excel、記事本數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)方法

熱門標簽:辦理400電話證件 外呼系統(tǒng)費用一年 十堰正規(guī)電銷機器人系統(tǒng) 巫師3為什么地圖標注的財寶沒有 世紀佳緣地圖標注怎么去掉 手機地圖標注如何刪除 外呼系統(tǒng)代理品牌 怎么給超市做地圖標注入駐店 寧波自動外呼系統(tǒng)代理

文件示例:
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 style="margin-left:200px">
                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 style="margin-left:200px">
                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++)
                     {
                          //讀取每行數(shù)據(jù)         
                           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++)
                     {
                          //讀取每行數(shù)據(jù)         
                           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);   //創(chuàng)建文件夾,并上傳文件
                    }
                    string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");//使用時間定義上傳圖片的名字
                    string picturename = time + fileExtension;
                    string newFilePath = fpath + picturename; //文件保存路徑
                    FileUploadName.SaveAs(newFilePath);
                    return newFilePath;   //絕對路徑

                }
                else
                {
                    return "-1";   //沒有文件
                }           
        }
        //讀取Excel數(shù)據(jù)
         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表的名稱,類型,創(chuàng)建時間和修改時間等 
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串數(shù)組
            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();
            //從指定的表明查詢數(shù)據(jù),可先把所有表明列出來供用戶選擇
            string strExcel = "select * from [" + strTableNames[0] + "]";
            myCommand = new OleDbDataAdapter(strExcel, strCon);
            myCommand.Fill(dt);
            conn.Close();

            return dt;
        }
        //讀取記事本數(shù)據(jù)
         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;
         }

您可能感興趣的文章:
  • 怎么運行用記事本寫的java程序
  • 使用Java制作一個簡單的記事本
  • A notepad made in HTA(hta實現(xiàn)的記事本)
  • 使用記事本編寫java程序全過程圖解
  • 簡單記事本java源碼實例
  • VB簡易記事本實現(xiàn)代碼
  • EXTJS記事本 當CompositeField遇上RowEditor
  • python3.5使用tkinter制作記事本
  • Python基于Tkinter實現(xiàn)的記事本實例
  • Android實現(xiàn)記事本功能(26)

標簽:山西 泰州 嘉興 通遼 景德鎮(zhèn) 天門 牡丹江

巨人網(wǎng)絡通訊聲明:本文標題《Excel、記事本數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)方法》,本文關鍵詞  Excel,記事本,數(shù)據(jù),導入,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Excel、記事本數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)方法》相關的同類信息!
  • 本頁收集關于Excel、記事本數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)方法的相關信息資訊供網(wǎng)民參考!
  • 推薦文章