这个无非循环?
先把文件列表循环出来。
之后一个个EXCEL一个个sheet操作

解决方案 »

  1.   

    文件列表 sheet的个数这些都能网上找到获取方法!
      

  2.   

    循环遍历文件夹里面的Excel 然后读取
      

  3.   

    用folderBrowserDialog可以实现文件多选
      

  4.   

    导入多个Excel文件到指定数据库
    //批量读取Excel数据到指定数据库
     private void btn_Export_Click(object sender, EventArgs e)
            {
                string[] P_str_Names = txt_Path.Text.Split(',');//存储所有选择的Excel文件名
                string P_str_Name = "";//存储遍历到的Excel文件名
                List<string> P_list_SheetNames = new List<string>();//实例化泛型集合对象,用来存储工作表名称
                for (int i = 0; i < P_str_Names.Length - 1; i++)//遍历所有选择的Excel文件名
                {
                    P_str_Name = P_str_Names[i];//记录遍历到的Excel文件名
                    P_list_SheetNames = GetSheetName(P_str_Name);//获取Excel文件中的所有工作表名
                    for (int j = 0; j < P_list_SheetNames.Count; j++)//遍历所有工作表
                    {
                        if (rbtn_Access.Checked)//判断Access数据库连接设置单选按钮选中
                        {
                            ImportDataToAccess(P_str_Name, P_list_SheetNames[j]);//将将工作表内容导出到Access
                        }
                        else if (rbtn_Sql.Checked)//判断Sql Server数据库连接设置单选按钮选中
                        {
                            if (ckbox_Windows.Checked)//如果用Windows身份验证登录Sql Server
                                ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data Source=" + txt_Server.Text + ";Initial Catalog =" + cbox_Server.Text + ";Integrated Security=SSPI;");//将工作表内容导出到Sql Server
                            else if (ckbox_SQL.Checked)//如果用Sql Server身份验证登录Sql Server
                                ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data Source=" + txt_Server.Text + ";Database=" + cbox_Server.Text + ";Uid=" + txt_Name.Text + ";Pwd=" + txt_Pwd.Text + ";");//将工作表内容导出到Sql Server
                        }
                    }
                }
                MessageBox.Show("已经将所有选择的Excel工作表导入到了指定的数据库中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
    //将Excel文件所有工作表存储到泛型集合
    private List<string> GetSheetName(string P_str_Excel)//获取所有工作表名称
            {
                List<string> P_list_SheetName = new List<string>();//实例化泛型集合对象
                //连接Excel数据库
                OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + P_str_Excel + ";Extended Properties=Excel 8.0");
                olecon.Open();//打开数据库连接
                System.Data.DataTable DTable = olecon.GetSchema("Tables");//实例化表对象
                DataTableReader DTReader = new DataTableReader(DTable);//实例化表读取对象
                while (DTReader.Read())//循环读取
                {
                    string P_str_Name = DTReader["Table_Name"].ToString().Replace('$', ' ').Trim();//记录工作表名称
                    if (!P_list_SheetName.Contains(P_str_Name))//判断泛型集合中是否已经存在该工作表名称
                        P_list_SheetName.Add(P_str_Name);//将工作表名添加到泛型集合中
                }//CodeGo.net/
                DTable = null;//清空表对象
                DTReader = null;//清空表读取对象
                olecon.Close();//关闭数据库连接
                return P_list_SheetName;//返回得到的泛型集合
            }
    //导入到指定的Sql Server数据库
     private void ImportDataToSql(string P_str_Excel, string P_str_SheetName, string P_str_SqlCon)//将工作表内容导出到Sql Server
            {
                DataSet myds = new DataSet();//实例化数据集对象
                try
                {
                    CloseProcess("EXCEL");//关闭所有Excel进程
                    //获得全部数据    
                    string P_str_OledbCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + P_str_Excel + ";Extended Properties=Excel 8.0;";
                    OleDbConnection oledbcon = new OleDbConnection(P_str_OledbCon);//实例化Oledb数据库连接对象
                    string P_str_ExcelSql = "";//定义变量,用来记录要执行的Excel查询语句
                    OleDbDataAdapter oledbda = null;//实例化Oledb数据桥接器对象
                    P_str_ExcelSql = string.Format("select * from [{0}$]", P_str_SheetName);//记录要执行的Excel查询语句
                    oledbda = new OleDbDataAdapter(P_str_ExcelSql, P_str_OledbCon);//使用数据桥接器执行Excel查询
                    oledbda.Fill(myds, P_str_SheetName);//填充数据
                    string P_str_CreateSql = string.Format("use " + cbox_Server.Text + " if object_Id('" + P_str_SheetName + "') is not null drop table " + P_str_SheetName + " create table {0}(", P_str_SheetName);//定义变量,用来记录创建表的SQL语句
                    foreach (DataColumn c in myds.Tables[0].Columns)//遍历数据集中的所有行
                    {
                        P_str_CreateSql += string.Format("[{0}] text,", c.ColumnName);//在表中创建字段
                    }
                    P_str_CreateSql = P_str_CreateSql + ")";//完善创建表的SQL语句
                    using (SqlConnection sqlcon = new SqlConnection(P_str_SqlCon))//实例化SQL数据库连接对象
                    {
                        sqlcon.Open();//打开数据库连接
                        SqlCommand sqlcmd = sqlcon.CreateCommand();//实例化SqlCommand执行命令对象
                        sqlcmd.CommandText = P_str_CreateSql;//指定要执行的SQL语句
                        sqlcmd.ExecuteNonQuery();//执行操作
                        sqlcon.Close();//关闭数据连接
                    }
                    using (SqlBulkCopy bcp = new SqlBulkCopy(P_str_SqlCon))//用bcp导入数据 
                    {
                        bcp.BatchSize = 100;//每次传输的行数    
                        bcp.DestinationTableName = P_str_SheetName;//定义目标表    
                        bcp.WriteToServer(myds.Tables[0]);//将数据写入Sql Server数据表
                    }
                }
                catch
                {
                    MessageBox.Show("Sql Server数据库中已经存在" + P_str_SheetName + "表!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
    //关闭进程
     private void CloseProcess(string P_str_Process)//关闭进程
            {
                System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName(P_str_Process);//实例化进程对象
                foreach (System.Diagnostics.Process p in excelProcess)
                    p.Kill();//关闭进程
            }