[英]C# | Forms .NET | How can I make a ComboBox that show's Excel sheets which it then displays in the dataViewGrid?
So I have been working on a little application for a bit and its pretty enjoyable so far.所以我一直在研究一个小应用程序,到目前为止它非常愉快。 You can pick any Excel file you want and any Access Database you want and then save the Excel data to it.您可以选择所需的任何 Excel 文件和所需的任何 Access 数据库,然后将 Excel 数据保存到其中。 It also displays the Excel data in the dataGridview.它还在 dataGridview 中显示 Excel 数据。 Eventually I want to be able to change data in the Gridview it self and be able to save that as well, but that's for another time.最终,我希望能够自行更改 Gridview 中的数据并能够保存它,但那是另一次了。 right now I'd like to know how I can make a ComboBox that lets me pick the Sheet of my Excel file.现在我想知道如何制作 ComboBox 让我选择 Excel 文件的工作表。
My current GUI looks like this: GUI我当前的 GUI 如下所示: GUI
Item Names:物品名称:
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
Here is my current code:这是我当前的代码:
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;
}
}
}
So, yeah.是的。 Any help would be appreciated.任何帮助,将不胜感激。
Thanks:)谢谢:)
You should be able to get sheet names via:您应该能够通过以下方式获取工作表名称:
DataTable dtSheets = conn.GetSchema("Tables")
You can find how to populate a ComboBox with a simple Google search.您可以通过简单的 Google 搜索找到如何填充 ComboBox。 You can populate your DataTable with the sheet contents simply by substituting 'Sheet1$' with the name of the selected sheet您只需将“Sheet1$”替换为所选工作表的名称即可使用工作表内容填充数据表
var sqlQuery = "Select * from [" + strSheetName + "]";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.