Excel电子表格导入数据库

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();

           } 
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值