access數據庫結構 表名ZZZ 結構內容: ID FF DD 1 2 3 4 5 6 7 8 9 新建一個c#窗體應用程序 添加一個ComboBox控件 和三個button按鍵 還有三個textbox文本框 button1 --->access數據庫位置 button2 --->EXCEL位置 button3 --->導入 textbox1--->access數據庫的路徑 textbox2--->EXCEL的路徑 textbox3--->EXCEL文件名 ComboBox1--->顯示數據庫表名 自定義函數1: [csharp] public void AccessGuideJoinExcel(string Access, string AccTable, string Excel) { try { string tem_sql = "";//定義字符串 string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Access + ";Persist Security Info=True";//記錄連接Access的語句 System.Data.OleDb.OleDbConnection tem_conn = new System.Data.OleDb.OleDbConnection(connstr);//連接Access數據庫 System.Data.OleDb.OleDbCommand tem_comm;//定義OleDbCommand類 tem_conn.Open();//打開連接的Access數據庫 tem_sql = "select Count(*) From " + AccTable;//設置SQL語句,獲取記錄個數 tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//實例化OleDbCommand類 int RecordCount = (int)tem_comm.ExecuteScalar();//執行SQL語句,並返回結果 //每個Sheet只能最多保存65536條記錄。 tem_sql = @"select top 65535 * into [Excel 8.0;database=" + Excel + @".xls].[Sheet2] from ZZZ";//記錄連接Excel的語句 tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//實例化OleDbCommand類 tem_comm.ExecuteNonQuery();//執行SQL語句,將數據表的內容導入到Excel中 tem_conn.Close();//關閉連接 tem_conn.Dispose();//釋放資源 tem_conn = null; MessageBox.Show("導入完成"); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示!"); } } public void AccessGuideJoinExcel(string Access, string AccTable, string Excel) { try { string tem_sql = "";//定義字符串 string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Access + ";Persist Security Info=True";//記錄連接Access的語句 System.Data.OleDb.OleDbConnection tem_conn = new System.Data.OleDb.OleDbConnection(connstr);//連接Access數據庫 System.Data.OleDb.OleDbCommand tem_comm;//定義OleDbCommand類 tem_conn.Open();//打開連接的Access數據庫 tem_sql = "select Count(*) From " + AccTable;//設置SQL語句,獲取記錄個數 tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//實例化OleDbCommand類 int RecordCount = (int)tem_comm.ExecuteScalar();//執行SQL語句,並返回結果 //每個Sheet只能最多保存65536條記錄。 tem_sql = @"select top 65535 * into [Excel 8.0;database=" + Excel + @".xls].[Sheet2] from ZZZ";//記錄連接Excel的語句 tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//實例化OleDbCommand類 tem_comm.ExecuteNonQuery();//執行SQL語句,將數據表的內容導入到Excel中 tem_conn.Close();//關閉連接 tem_conn.Dispose();//釋放資源 tem_conn = null; MessageBox.Show("導入完成"); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示!"); } }自定義函數2: [csharp] public void GetTable(string Apath, ComboBox ComBox) { string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Apath + ";Persist Security Info=True"; System.Data.OleDb.OleDbConnection tem_OleConn = new System.Data.OleDb.OleDbConnection(connstr); tem_OleConn.Open(); DataTable tem_DataTable = tem_OleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); tem_OleConn.Close(); ComBox.Items.Clear(); for (int i = 0; i < tem_DataTable.Rows.Count; i++) { ComBox.Items.Add(tem_DataTable.Rows[i][2]); } if (ComBox.Items.Count > 0) ComBox.SelectedIndex = 0; } public void GetTable(string Apath, ComboBox ComBox) { string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Apath + ";Persist Security Info=True"; System.Data.OleDb.OleDbConnection tem_OleConn = new System.Data.OleDb.OleDbConnection(connstr); tem_OleConn.Open(); DataTable tem_DataTable = tem_OleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); tem_OleConn.Close(); ComBox.Items.Clear(); for (int i = 0; i < tem_DataTable.Rows.Count; i++) { ComBox.Items.Add(tem_DataTable.Rows[i][2]); } if (ComBox.Items.Count > 0) ComBox.SelectedIndex = 0; } [csharp] private void button1_Click(object sender, EventArgs e) { openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; GetTable(textBox1.Text, comboBox1); } } private void button1_Click(object sender, EventArgs e) { openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; GetTable(textBox1.Text, comboBox1); } } [csharp] private void button2_Click(object sender, EventArgs e) { folderBrowserDialog1.SelectedPath = ""; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) textBox2.Text = folderBrowserDialog1.SelectedPath; } private void button2_Click(object sender, EventArgs e) { folderBrowserDialog1.SelectedPath = ""; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) textBox2.Text = folderBrowserDialog1.SelectedPath; } [csharp] view plaincopyprint?private void button3_Click(object sender, EventArgs e) { AccessGuideJoinExcel(textBox1.Text, comboBox1.Text, textBox2.Text + "\\" + textBox3.Text); } private void button3_Click(object sender, EventArgs e) { AccessGuideJoinExcel(textBox1.Text, comboBox1.Text, textBox2.Text + "\\" + textBox3.Text); }