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;
    }
posted @ 2015-08-17 20:07  Pavilion  阅读(429)  评论(0编辑  收藏  举报