C # Writing a database to excel

Source: Internet
Author: User

Access database structure table name ZZZ structure content: id ff dd 1 2 3 4 5 6 7 8 9 create a c # form application Add a ComboBox control and three button buttons and three textbox text boxes button1 ---> access database location button2 ---> EXCEL location button3 ---> Import textbox1 ---> access Database path textbox2 ---> EXCEL path textbox3 ---> EXCEL file name combox1 ---> display database table name custom function 1: [csharp] public void AccessGuideJoinExcel (string Access, string AccTable, string Excel) {try {string tem_ SQL = ""; // define string connstr = @ "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Access +"; Persist Security Info = True "; // record the Access statement System. data. oleDb. oleDbConnection tem_conn = new System. data. oleDb. oleDbConnection (connstr); // connect to the Access database System. data. oleDb. oleDbCommand tem_comm; // defines the OleDbCommand class tem_conn.Open (); // opens the connected Access Database tem_ SQL = "select Count (*) From" + AccTable; // sets the SQL statement, obtain the number of records tem_comm = new System. data. oleDb. O LeDbCommand (tem_ SQL, tem_conn); // instantiate OleDbCommand class int RecordCount = (int) tem_comm.ExecuteScalar (); // execute the SQL statement, and return results // each Sheet can only save up to 65536 records. Tem_ SQL = @ "select top 65535 * into [Excel 8.0; database =" + Excel + @". xls]. [Sheet2] from ZZZ "; // record the statement tem_comm = new System. data. oleDb. oleDbCommand (tem_ SQL, tem_conn); // instantiate OleDbCommand class tem_comm.ExecuteNonQuery (); // execute the SQL statement to import the data table content to tem_conn.Close () in Excel (); // close the connection tem_conn.Dispose (); // release the resource tem_conn = null; MessageBox. show ("Import completed");} catch (Exception ex) {MessageBox. show (ex. message, "prompt! ") ;}} Public void AccessGuideJoinExcel (string Access, string AccTable, string Excel) {try {string tem_ SQL =" "; // define string connstr = @" Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Access +"; Persist Security Info = True "; // record the Access statement System. data. oleDb. oleDbConnection tem_conn = new System. data. oleDb. oleDbConnection (connstr); // connect to the Access database System. data. oleDb. oleDbCommand tem_comm; // defines OleDb Command class tem_conn.Open (); // open the connected Access Database tem_ SQL = "select Count (*) From" + AccTable; // set the SQL statement and obtain the number of records tem_comm = new System. data. oleDb. oleDbCommand (tem_ SQL, tem_conn); // instantiate OleDbCommand class int RecordCount = (int) tem_comm.ExecuteScalar (); // execute the SQL statement, and return results // each Sheet can only save up to 65536 records. Tem_ SQL = @ "select top 65535 * into [Excel 8.0; database =" + Excel + @". xls]. [Sheet2] from ZZZ "; // record the statement tem_comm = new System. data. oleDb. oleDbCommand (tem_ SQL, tem_conn); // instantiate OleDbCommand class tem_comm.ExecuteNonQuery (); // execute the SQL statement to import the data table content to tem_conn.Close () in Excel (); // close the connection tem_conn.Dispose (); // release the resource tem_conn = null; MessageBox. show ("Import completed");} catch (Exception ex) {MessageBox. show (ex. message, "prompt! ") ;}} Custom function 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, "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 = Tru E "; 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, "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 button#click (object sender, EventArgs e) {openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog () = DialogResult. OK) {textBox1.Text = openFileDialog1.FileName; GetTable (textBox1.Text, combox1) ;}} private void button#click (object sender, EventArgs e) {openFileDialog1.FileName = ""; if (openFileDia Log1.ShowDialog () = DialogResult. OK) {textBox1.Text = openFileDialog1.FileName; GetTable (textBox1.Text, combox1) ;}} [csharp] private void button2_Click (object sender, EventArgs e) {response = ""; if (partial () = DialogResult. OK) textBox2.Text = folderBrowserDialog1.SelectedPath;} private void button2_Click (object sender, EventArgs e) {fold ErBrowserDialog1.SelectedPath = ""; if (folderBrowserDialog1.ShowDialog () = DialogResult. OK) textBox2.Text = folderBrowserDialog1.SelectedPath;} [csharp] view plaincopyprint? Private void button3_Click (object sender, EventArgs e) {response (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 );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.