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

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

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

文件示例:
Excel:

記事本:

前臺(tái)代碼:

復(fù)制代碼 代碼如下:

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="請(qǐng)選擇要提交的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="請(qǐng)選擇要提交的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>

后臺(tái)代碼
復(fù)制代碼 代碼如下:

//Excel上傳
        protected void btnExcel_Click(object sender, EventArgs e)
        {         
              string backStr= UploadFile(FileUpload1, "Excel",1);
              if (backStr == "-1")
              {
                   //這里是放返回消息的,改為對(duì)應(yīng)放消息的方式就行了,Js這個(gè)類就不上傳了
                  Js.ShowSureMsgBox(this.Page,"請(qǐng)選擇要提交的Excel文件");
                  return;
              }
              else if (backStr == "-2")
              {
                  Js.ShowSureMsgBox(this.Page, "請(qǐng)選擇.xls或.xlsx類型文件");
                  return;
              }
              else
              {
                  string url = backStr;   //絕對(duì)路徑
                  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, "文件內(nèi)容為空");
                  }
                  File.Delete(url);  //刪除上傳的文件
              }
        }
        //記事本上傳
        protected void btnNotepad_Click(object sender, EventArgs e)
        {
            string backStr = UploadFile(FileUpload2, "TXT",2);
            if (backStr == "-1")
            {
                Js.ShowSureMsgBox(this.Page, "請(qǐng)選擇要提交的TXT文件");
                return;
            }
            else if (backStr == "-2")
            {
                Js.ShowSureMsgBox(this.Page, "請(qǐng)選擇.txt類型文件");
                return;
            }
            else
            {
                string url = backStr;   //絕對(duì)路徑
                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, "文件內(nèi)容為空");
                }
                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 + "/");//圖片存儲(chǔ)文件夾路徑,需要按照不同的需要進(jìn)行相應(yīng)的修改
                    if (!Directory.Exists(fpath))//查看存儲(chǔ)路徑的文件是否存在
                    {
                        Directory.CreateDirectory(fpath);   //創(chuàng)建文件夾,并上傳文件
                    }
                    string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");//使用時(shí)間定義上傳圖片的名字
                    string picturename = time + fileExtension;
                    string newFilePath = fpath + picturename; //文件保存路徑
                    FileUploadName.SaveAs(newFilePath);
                    return newFilePath;   //絕對(duì)路徑

                }
                else
                {
                    return "-1";   //沒(méi)有文件
                }           
        }
        //讀取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的架構(gòu),包括各個(gè)sheet表的名稱,類型,創(chuàng)建時(shí)間和修改時(shí)間等 
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串?dāng)?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ù),可先把所有表明列出來(lái)供用戶選擇
            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;
         }

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

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

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