• 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

  • 相关阅读:
    .NET破解之百分百营销软件系列
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序
    .NET破解之百度网盘批量转存工具
    The system clock has been set back more than 24 hours
    64位系统中读写注册表
    FME2014汉化问题
    FME中Cass扩展属性转Shp的方法
    Hosts文件小结
    浅谈Java中的equals和==
    mysql读写分离
  • 原文地址:https://www.cnblogs.com/jcdd-4041/p/3394360.html
Copyright © 2020-2023  润新知