• C# 读取excel到数据库


     private DataSet xsldata(string filepath,string extension)
            {
                string strConn = "";
              
                switch (extension)
                {
                    case ".xls":
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                        break;
                    case ".xlsx":
                        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
                        break;
                    default:
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                        break;
                }           
                System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strConn);
    
                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;
    
            }
    
            /// <summary>
            /// 导入Sql数据库
            /// </summary>
            /// <param name="filepath"></param>
            private void daoRu(string filepath,string Extente)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
                
                SqlConnection conn = new SqlConnection(connectionString);//链接数据库
                conn.Open();
                try
                {
    
                    DataSet ds = new DataSet();
                    //取得数据集
                    //调用上面的函数
                    ds = xsldata(filepath, Extente);
                   
                 
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
    
                        string  cardtypeid = 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();
                        string officeid = ds.Tables[0].Rows[i][3].ToString();
    
                        if (cardtypeid != "" && cardnum != "" && cardpwd != "" && officeid != "")
                        {
                            SqlCommand insertcmd = new SqlCommand("insert into StaffersTemp(EmpID,Depart,ChineseName,Isleave) values('" + cardtypeid + "','" + cardnum + "','" + cardpwd + "','" + officeid  + "')", conn);
                            insertcmd.ExecuteNonQuery();                           
                        }
                        else
                        {
                                               
                        }                    
                    }
                }
    
                catch (Exception ex)
                {
                  
                }
    
                finally
                {
                    conn.Close();
    
                }
            }
  • 相关阅读:
    成为高级 React 开发你需要知道的知识点
    Socket 连接问题之大量 TIME_WAIT
    x == (x = y) 不等于 (x = y) == x ?
    「工具」三分钟了解一款思维导图工具:XMind Zen
    Touch Bar 废物利用系列 | 在触控栏上显示 Dock 应用图标
    vim中delete(backspace)键不能向左删除
    Vue2.0学习(四)--组件的继承与扩展
    quasar+cordova+zbar实现Android扫描条形码
    PWA技术理论+实战全解析
    分页请求时,有新数据加入时,下一页会出现重复数据问题
  • 原文地址:https://www.cnblogs.com/WangYunyun/p/3305144.html
Copyright © 2020-2023  润新知