zoukankan      html  css  js  c++  java
  • winform 导入Excel到数据库

     static ArrayList Sheets(string filepath)
            {
                ArrayList al = new ArrayList();
                string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strconn);
                conn.Open();
                DataTable sheetnames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                conn.Close();
                foreach (DataRow row in sheetnames.Rows) { al.Add(row["table_name"].ToString()); }
                return al;
            }
    
            static DataTable ExcelDataSource(string filepath, string sheetname)
            {
                string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                OleDbConnection conn = new OleDbConnection(strconn);
                OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strconn);//
                DataTable dt = new DataTable();
                oada.Fill(dt);
                return dt;
            }
    
            static void In(DataTable dt)
            {
                SqlConnection conn = new SqlConnection("server=.;database=ManageDatas;uid=sa;pwd=sa;");
                SqlCommand cmd = new SqlCommand();
                try
                {
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    foreach (DataRow row in dt.Rows)
                    {
                        cmd.CommandText = @"if (select count(chvBandName) from dbo.tblBand WHERE chvBandName=@name)=1 
                                            begin 
                                            update dbo.tblBand set chvBandName=@name WHERE chvBandName=@name 
                                            end 
                                            else 
                                            begin 
                                            insert into dbo.tblBand values(@name) 
                                            end ";
                        cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = row[0].ToString();
                        int i = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                }
                catch (Exception ex) { }
                finally { conn.Close(); }
            }
    
            public static void ImportExcel()
            {
                //获取Excel文件的路径
                OpenFileDialog ofd1 = new OpenFileDialog();
                ofd1.ShowDialog();
                string FilePath = ofd1.FileName;
    
                if (!string.IsNullOrEmpty(FilePath))
                {
                    In(ExcelDataSource(FilePath, Sheets(FilePath)[0].ToString()));
                }
            }

    命名空间:

    using System.Collections;
    using System.Data.OleDb;

    添加引用:Microsoft.Office.Interop.Excel.dll

  • 相关阅读:
    SQL Server 2005中 with as 使用介绍
    论信息系统项目的整体管理
    自定义控件的使用以及与用户控件的区别
    Sql Server2005 TransactSQL 新兵器学习总结之DDL触发器
    我的大学系分之路
    C#序列化与反序列化(Serializable and Deserialize)
    父子节点树形数据输出
    自定义枚举类型注释属性,并在程序中获取
    SSAS没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)) 解决办法
    对ASP.NET网站的服务器端压缩
  • 原文地址:https://www.cnblogs.com/jcdd-4041/p/3394360.html
Copyright © 2011-2022 走看看