Excel电子表格导入数据库

本博客介绍如何从Excel文件导入数据,并将其插入到数据库中。通过使用ADO.NET连接到Excel文件,读取数据并根据特定条件进行更新或插入操作。在数据处理过程中,检查数据的有效性和完整性,确保只有符合预设标准的数据被导入数据库。

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

private DataSet xsldata(string filepath)

       {

           
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";

           System.Data.OleDb.OleDbConnection Conn 
= new System.Data.OleDb.OleDbConnection(strCon);

           
string strCom = "SELECT * FROM [Sheet1$]";

           Conn.Open();

           System.Data.OleDb.OleDbDataAdapter myCommand 
= new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);

           DataSet ds 
= new DataSet();
           
           myCommand.Fill(ds, 
"[Sheet1$]");
           dataGridView1.DataSource 
= ds.Tables[0];
           Conn.Close();

           
return ds;

       }

 

 

 private void button1_Click(object sender, EventArgs e)
        {
            
if(txtpath.Text=="")
            {
               MessageBox.Show(
"请选择要导入的Excel文档!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                   
                    
return;
               
            }
            
string filepath=txtpath.Text;
            SqlConnection conn 
=new SqlConnection(strcon);//链接数据库

            conn.Open();

            
try

            {

              

               DataSet ds 
= new DataSet();

//取得数据集
//调用上面的函数

               ds 
= xsldata(filepath);
               
//dataGridView2.DataSource = ds.Tables[0];
               int errorcount = 0;//记录错误信息条数

               
int insertcount = 0;//记录插入成功条数

               
int updatecount = 0;//记录更新信息条数

                
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

               {

                   
int  cardtypeid = Convert.ToInt32(ds.Tables[0].Rows[i][0].ToString());

                   
string cardnum = ds.Tables[0].Rows[i][1].ToString();

                   
string cardpwd = ds.Tables[0].Rows[i][2].ToString();
                   
int officeid = Convert.ToInt32(ds.Tables[0].Rows[i][3].ToString());
                   DateTime  stdt 
= Convert.ToDateTime(ds.Tables[0].Rows[i][4].ToString());
                   
                   
string cardstate= ds.Tables[0].Rows[i][5].ToString();
                   
int userid=Convert.ToInt32(ds.Tables[0].Rows[i][6].ToString());
                  
                   
if (cardtypeid != 0 && cardnum != "" && cardpwd != "" && userid != 0 )

                   {

                       SqlCommand selectcmd 
= new SqlCommand("select count(*) from cardsinfo where CardNum='" + cardnum + "'and CardPwd='" + cardpwd + "' and CardState='1' and TypeID="+cardtypeid, conn);

                       
int count = Convert.ToInt32(selectcmd.ExecuteScalar());

                       
if (count > 0)

                       {
                           updatecount
++;
                          
                       }
                       
else
                       {

                           SqlCommand insertcmd
= new SqlCommand("insert into cardsinfo(TypeID,CardNum,CardPwd,OutByOffID,CardTime,CardState,CardByUser) values(" + cardtypeid + ",'" + 

                                                                 cardnum 
+ "','" + cardpwd + "'," + officeid + ",'" + stdt+ "','1',"+userid+")", conn);

                           insertcmd.ExecuteNonQuery();

                           insertcount
++;

                       }

                      

                   }
                   
else

                   {

                       
//MessageBox.Show("电子表格信息有错!");
                       errorcount++;
                       
;  

                    }
                 

               }

               MessageBox.Show( insertcount 
+ "条数据导入成功!" + updatecount + "条数据重复!" + errorcount + "条数据部分信息为空没有导入!");

           }

           
catch (Exception ex)

           {

              MessageBox.Show(ex.Message);

           }

           
finally

           {

               conn.Close();

           } 
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值