所以我一直在研究一个小应用程序,到目前为止它非常愉快。您可以选择所需的任何 Excel 文件和所需的任何 Access 数据库,然后将 Excel 数据保存到其中。它还在 dataGridview 中显示 Excel 数据。最终,我希望能够自行更改 Gridview 中的数据并能够保存它,但那是另一次了。现在我想知道如何制作一个组合框,让我选择 Excel 文件的工作表。
我当前的 GUI 如下所示: 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;
}
}
}
是的。任何帮助,将不胜感激。
谢谢 :)