xls文件導入數據庫

 protected void btn_ok_Click(object sender, EventArgs e)     {         int num = 0;                  try         {             string x = txtpath.Text;             //DirectoryInfo theFolder = new DirectoryInfo(x);             //FileInfo[] fileInfo = theFolder.GetFiles();             //foreach (FileInfo NextFile in fileInfo)  //遍歷文件             //{             //    b++;             //    DataSet ds = GetConnect(x+NextFile.Name);             DataSet ds = GetConnect(x);                 DataTable xDataTable = ds.Tables[0];                 for (int i = 0; i < xDataTable.Rows.Count; i++)                 {                     num = i;                     //var isbn = WebUtil.ReplaceChar((xDataTable.Rows[i][0] + "").Substring(1).Trim());                     var isbn = (xDataTable.Rows[i][0] + "").Trim();                     var bookname = xDataTable.Rows[i][1] + "";                     // var bookname = xDataTable.Rows[i][1] + "";                     //var cover =xDataTable.Rows[i][2];                     var cover = xDataTable.Rows[i][2] + "";                     var author = xDataTable.Rows[i][3] + "";                     var bottle = xDataTable.Rows[i][4] + "";                     var pricie = xDataTable.Rows[i][5] + "";                     var publicationdate = xDataTable.Rows[i][6] + "";                     var contentintroduced = xDataTable.Rows[i][7] + "";                     var authorIntroduced = xDataTable.Rows[i][8] + "";                     var area = xDataTable.Rows[i][9] + "";                     SQLiteHelper.ExecuteNonQuery("Insert into [Books] (ISBN,BookName,Cover,Author,Bottle,Pricie,PublicationDate,ContentIntroduced,AuthorIntroduced,Area,IsCommend,IsConver)values('" + isbn + "','" + bookname + "','" + cover + "','" + author + "','" + bottle + "','" + pricie + "','" + publicationdate + "','" + authorIntroduced + "','" + contentintroduced + "','" + area + "',0,0)");                             }               //}             lblshow.InnerText = "導入成功!";         }         catch (Exception ex)         {             lblshow.InnerText = "導入失敗!" + num;         }     }     /// <summary>     /// 導出EXCEL表中的數據到 myDataSet     /// </summary>     public static DataSet GetConnect(string FilePath)     {         DataSet myDataSet = new DataSet();         string strCon = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + FilePath + "; Extended Properties=\"Excel 8.0; HDR=YES; IMEX=1;\"";         if (Path.GetExtension(FilePath).ToLower() == ".xlsx")             strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";         OleDbConnection myConn = new OleDbConnection(strCon);         //條件查詢EXCEL表         string strCom = " SELECT * FROM [Sheet1$] ";         myConn.Open();         //打開數據連接,獲得一個數據集         OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);         //建立一個 DataSet對象         //獲得本身的DataSet對象         myCommand.Fill(myDataSet, "[Sheet1$]");         //關閉此數據連接         myConn.Close();         return myDataSet;     }
相關文章
相關標籤/搜索