C# C | Forms.NET |如何制作一个组合框来显示Excel表格,然后将其显示在dataViewGrid中?

C# C | Forms.NET |如何制作一个组合框来显示Excel表格,然后将其显示在dataViewGrid中?,c#,.net,ms-access,visual-studio-code,C#,.net,Ms Access,Visual Studio Code,所以我一直在开发一个小应用程序,到目前为止,它非常令人愉快。您可以选择所需的任何Excel文件和所需的任何Access数据库,然后将Excel数据保存到其中。它还将在dataGridview中显示Excel数据。最终,我希望能够自己更改Gridview中的数据,并保存这些数据,但这是另一个时间。现在我想知道如何制作一个组合框,让我可以选择Excel文件的工作表 我当前的GUI如下所示: 项目名称: btnrun - "Runs" the excel file in the

所以我一直在开发一个小应用程序,到目前为止,它非常令人愉快。您可以选择所需的任何Excel文件和所需的任何Access数据库,然后将Excel数据保存到其中。它还将在dataGridview中显示Excel数据。最终,我希望能够自己更改Gridview中的数据,并保存这些数据,但这是另一个时间。现在我想知道如何制作一个组合框,让我可以选择Excel文件的工作表

我当前的GUI如下所示:

项目名称:

btnrun - "Runs" the excel file in the datagridview
btnbrowse - chooses which Excel file you want to use
btnbrowse2 - chooses which Access Database file you want to use
btnsave - saves Excel data to Access Database
textBox1 - Shows the file path for Excel
textBox2 - Shows the file path for Access Database
comboBox1 - This is where i need help :)
dataGridView1 - Shows the Excel data
这是我目前的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Datatestje
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        //Buttons
        private void btnrun_Click(object sender, EventArgs e) //Run
        {
            string EXpath = textBox1.Text;
            string PathConn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + EXpath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
            OleDbConnection conn = new OleDbConnection(PathConn);

            var sqlQuery = "Select * from [Sheet1$]";
            OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(sqlQuery, conn);
            DataTable dt = new DataTable();

            myDataAdapter.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void btnsave_Click(object sender, EventArgs e) //Save
        {
            {
                //File Path
                string EXpath = textBox1.Text;
                string fileNameExcel = @EXpath;
                string ACpath = textBox2.Text;
                string fileNameAccess = @ACpath;

                //Connection string for Excel
                string connectionStringExcel =
                    string.Format("Data Source= {0};Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;", fileNameExcel);

                //Connection string for Access
                string ConnectionStringAccess =
                    string.Format("Data Source= {0}; Provider=Microsoft.Jet.OLEDB.4.0; Persist security Info = false", fileNameAccess);

                OleDbConnection connExcel = new OleDbConnection(connectionStringExcel);
                OleDbConnection connAccess = new OleDbConnection(ConnectionStringAccess);
                OleDbCommand cmdExcel = connExcel.CreateCommand();
                cmdExcel.CommandType = CommandType.Text;

                //Excel Sheet
                cmdExcel.CommandText = "SELECT * FROM [Sheet1$]";

                //Command object for Access
                OleDbCommand cmdAccess = connAccess.CreateCommand();
                cmdAccess.CommandType = CommandType.Text;
                
                //Add parameters *
                cmdAccess.CommandText = "INSERT INTO Informatie (Naam, Achternaam, Land, Stad, Huisnummer, Postcode, Telefoonnummer) VALUES(@Naam, @Achternaam, @Land, @Stad, @Huisnummer, @Postcode, @Telefoonnummer)";

                //Add parameters to Access command object **
                OleDbParameter param1 = new OleDbParameter("@Naam", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param1);
                OleDbParameter param2 = new OleDbParameter("@Achternaam", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param2);
                OleDbParameter param3 = new OleDbParameter("@Land", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param3);
                OleDbParameter param4 = new OleDbParameter("@Stad", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param4);
                OleDbParameter param5 = new OleDbParameter("@Huisnummer", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param5);
                OleDbParameter param6 = new OleDbParameter("@Postcode", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param6);
                OleDbParameter param7 = new OleDbParameter("@Telefoonnummer", OleDbType.VarChar);
                cmdAccess.Parameters.Add(param7);


                //Open connections
                connExcel.Open();
                connAccess.Open();
                OleDbDataReader drExcel = cmdExcel.ExecuteReader();

                while (drExcel.Read())
                {
                    //Assign values to access command parameters ***
                    param1.Value = drExcel[0].ToString();
                    param2.Value = drExcel[1].ToString();
                    param3.Value = drExcel[2].ToString();
                    param4.Value = drExcel[3].ToString();
                    param5.Value = drExcel[4].ToString();
                    param6.Value = drExcel[5].ToString();
                    param7.Value = drExcel[6].ToString();

                    //Insert values in access
                    cmdAccess.ExecuteNonQuery();
                }

                //close connections
                connAccess.Close();
                connExcel.Close();
                MessageBox.Show("Succesfully uploaded Excel data to Database.");

            }
        }

        private void btnbrowse_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog1 = new OpenFileDialog();
            openfiledialog1.ShowDialog();
            openfiledialog1.Filter = "allfiles|*.xls";
            textBox1.Text = openfiledialog1.FileName;
        }

        private void btnbrowse2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog1 = new OpenFileDialog();
            openfiledialog1.ShowDialog();
            openfiledialog1.Filter = "allfiles|*.mdb";
            textBox2.Text = openfiledialog1.FileName;
        }
    }
}
所以,是的。任何帮助都将不胜感激


谢谢:

您应该能够通过以下方式获取工作表名称:

DataTable dtSheets = conn.GetSchema("Tables")
您可以通过简单的谷歌搜索找到如何填充组合框。 只需用所选工作表的名称替换“Sheet1$”即可使用工作表内容填充DataTable

 var sqlQuery = "Select * from [" + strSheetName + "]";

祝贺你迄今为止的成功!你被卡在哪一部分?如何列出表格?如何填充组合框?我以前从未使用过组合框,因此我不知道如何添加组合框以便从中选择Excel工作表。例如:如何在组合框中获取工作表名称,以及如何使其在从下拉菜单中选择工作表时在dataGridView中打开工作表的信息。此外,我知道我有var sqlQuery=Select*from[Sheet1$];在我的代码中。但这仅仅是因为我一直在慢慢地将应用程序从一个按钮扩展到一个具有多个功能的实际应用程序。所以我知道我需要替换它,但我还不知道用什么替换。我已经尝试了一些我在网上找到的代码,但遗憾的是它们不起作用。进展如何?AVG的回答解决了你的问题吗?那么,这可能是以下内容:private void comboBox1_SelectedIndexChangedobject sender,EventArgs e{DataTable dtSheets=conn.GetSchemaTables var sqlQuery=Select*from[+strSheetName+];}否。GetSchema用于使用图纸列表填充DataTable。这就是您想要填充组合框的内容。无法使用SelectedIndexChanged事件填充组合框,因为不会有条目。用户选择文件后,应填充组合框。然后在用户从组合框中选择条目后填充网格。