读取Excel文件中的数据,并将其导入数据库

Posted on 2009-08-25 16:46  sl2008  阅读(1207)  评论(0编辑  收藏  举报
读取Excel文件中的数据,并将其导入数据库

步骤如下:
//将需要导入的文件上传到服务器  
        string filePath = "", fileExtName = "";  
        //string myFileName;//用不到,但也写上吧  
        string myPath;  
        string FullName = "";//保存文件的完整文件名  
        if(fileUp.PostedFile.FileName!="")  
        {  
            //取得文件路径   
            filePath = fileUp.PostedFile.FileName;  
            //取得文件扩展名  
            fileExtName = filePath.Substring(filePath.LastIndexOf(".")+1);  
            //判断是否为Excel文件  
            if (fileExtName == "xls")  
            {  
                try 
                {  
                    //取得与web服务器上指定的虚拟路径相对应的物理路径  
                    myPath = Server.MapPath("Upfiles/");  
                    //取得文件名  
                    //myFileName = filePath.Substring(filePath.LastIndexOf("")+1);  
                    //取得当前时间,以“时时分分秒秒”来命名,以免重复  
                    string strDateName = DateTime.Now.ToString("hhmmss");  
                    //保存上传文件到指定目录  
                    FullName = myPath + strDateName + "." + fileExtName;  
                    fileUp.PostedFile.SaveAs(FullName);  
                }  
                catch (Exception ex)  
                {  
                    Response.Write(ex.Message);  
                }  
            }  
            else 
            {  
                Page.RegisterStartupScript("","<SCRIPT>alert('文件格式不正确');</SCRIPT>");  
                return;  
            }  
        }  
        //读取Excel中的内容  
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FullName + ";Extended Properties=Excel 8.0;";  
        OleDbConnection connxls = new OleDbConnection(strConn);  
        if(connxls.State.ToString()=="Closed")  
        {  
            connxls.Open();  
        }  
        string sqlExcel=("select * from [Sheet1$]");  
        OleDbDataAdapter myDa = new OleDbDataAdapter(sqlExcel,connxls);  
        DataSet myDs = new DataSet();  
        myDa.Fill(myDs);  
        if (myDs.Tables[0].Rows.Count > 0)  
        {  
            /***************将数据绑定到gridview*****************************************************/ 
            try 
            {  
 
                GridView1.DataSource = myDs.Tables[0].DefaultView;  
                GridView1.DataBind();  
   
            }  
            catch (Exception ex)  
            {  
                Response.Write("读取出错" + ex.Message);  
            }  
            /***************绑定完成******************************************************************/ 
 
            /********将数据导入到数据库,如果有对应的SQLserver2000数据库,要求字段要一一对应**********/ 
            string mySql = "";  
            //链接SQLserver2000数据库  
            string sqlConn = "server=(local);database=learning;user id=sa;pwd=sasasa";  
            SqlConnection con = new SqlConnection(sqlConn);  
            if(con.State.ToString()=="Closed")  
            {  
                con.Open();  
            }  
            SqlCommand myCmd = new SqlCommand();  
            //将数据逐行写入到数据库中  
            for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)  
            {  
                //数据库中三个字段,所以插入三列  
                mySql = ("insert into myInfo(myname,sex,mylike) values('" + myDs.Tables[0].Rows[i][0].ToString() + "',"+  
                         "'" + myDs.Tables[0].Rows[i][1].ToString() + "','" + myDs.Tables[0].Rows[i][2].ToString() + "')");  
                myCmd.Connection = con;  
                myCmd.CommandText = mySql;  
                try 
                {  
                    myCmd.ExecuteNonQuery();  
                }  
                catch (Exception ex)  
                {  
                    Response.Write("将数据插入数据库时出错" + ex.Message);  
                }  
            }  
            Response.Write("<SCRIPT>alert('数据已成功导入到数据库!');</SCRIPT>");  
            if(con.State.ToString()=="Open")  
            {  
                con.Close();  
            }  
            /*********导入数据库完成******************************************************************/ 
        }  
        else 
        {  
            Page.RegisterStartupScript("","<SCRIPT>alert('Excel中没有数据!');</SCRIPT>");  
        }  
        if(connxls.State.ToString()=="Open")  
        {  
            connxls.Close();  
        }

Copyright © 2024 sl2008
Powered by .NET 9.0 on Kubernetes