c#从excel中读取数据并添加到数据库中

本文介绍如何在Visual Studio 2010中处理无法链接Office 2010 Access数据库的问题,建议下载AccessDatabaseEngine.exe来解决。提供了一个C#示例,展示了如何在Btn_Sure_Click事件中从上传的Excel文件读取数据,并将其添加到Access数据库中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

visual studio 2010 不能链接 office 2010 access 数据库,请下载

AccessDatabaseEngine.exe


 protected void Btn_Sure_Click(object sender, EventArgs e)

        {

             //FileUpload为c#控件中上传文件的ID

            if (FileUpload.FileName!="")
            {
                string path = Server.HtmlEncode(Request.PhysicalApplicationPath);
                string excel_path = path + "load_file\\" + System.Guid.NewGuid().ToString() + ".xlsx";   //获取上传的excel文件
                FileUpload.SaveAs(excel_path);//save file  
                DataSet ExcelData = ExcelRead(excel_path);
                ArrayList LoadData = new ArrayList();  定义一个空ArrayList 保存从excel中读取的数据


                foreach (DataTable dt in ExcelData.Tables)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        ArrayList ExcelRow = new ArrayList();
                        foreach (DataColumn dc in dt.Columns)
                        {
                            ExcelRow.Add(dr[dc]);
                        }
                        LoadData.Add(ExcelRow);
                    }
                }
                // 检查用户是否都存在
                for (int i = 0; i < LoadData.Count; i++)
                {

                    ArrayList rowData = (ArrayList)LoadData[i];

                   //取出集合中的数据

                    string site_name = rowData[0].ToString();
                    string c_year_product = rowData[1].ToString();
                    string c_day_product = rowData[2].ToString();
                    string c_year = rowData[3].ToString();
                    string c_day = rowData[4].ToString();

                   //将ArrayList 中的数据添加到指定的数据库表中(省略) 

                }
                Response.Redirect("success.aspx");
            }

        }


        public DataSet ExcelRead(string file)
        {
            string fileType = file.Substring(file.LastIndexOf(".") + 1);     //获取excel的后缀,由后缀来判断excel的版本
            string ConnectionString = "";
            if (fileType == "xlsx")  2007版以上
            {
                ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended                                                            Properties='Excel 8.0;HDR=Yes;IMEX=1;'", file);
              //  ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";" +
               //        ";Extended ProPerties= 'Excel 12.0;HDR=Yes;'";
            }
            else {    //xls版本
               
               ConnectionString =  string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended                                                               Properties='Excel 8.0;HDR=Yes;IMEX=1;'", file);
            }
            
            OleDbConnection conn = new OleDbConnection(ConnectionString);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select * from [Sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, ConnectionString);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");
            conn.Close();
            return ds;

        }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值