C # implement access Import and Export Excel

Source: Internet
Author: User
I, AccessSlaveExcelImport Data

1.UsedExcelTable format and content

 

Implementation

Oledbconnection con = new oledbconnection (); <br/> try <br/> {<br/> openfiledialog openfile = new openfiledialog (); // open the file dialog box. <Br/> openfile. Filter = ("Excel file (*. xls) | *. xls"); // suffix. <Br/> If (openfile. showdialog () = dialogresult. OK) <br/>{< br/> string filename = openfile. filename; <br/> int Index = filename. lastindexof ("//"); // name of the captured file <br/> filename = filename. substring (index + 1); <br/> conexcel. connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ <br/> application. startuppath + "// appdata. mdb "; <br/> // import EXCEL to access <br/> // distinct: delete duplicate rows in Excel. <br/>/ /[Excel name]. [sheet name] $ <br/> // where not in: inserts records that are not repeated. </P> <p> string SQL = "insert into users2 (User ID, user name) Select distinct * from [Excel 8.0; database = "+ <br/> filename +"]. [name $] Where user no. Not in (Select User No. From users2) "; <br/> oledbcommand COM = new oledbcommand (SQL, con); <br/> con. open (); <br/> COM. executenonquery (); <br/> MessageBox. show ("imported data succeeded", "imported data", messageboxbuttons. OK, messageboxicon. information); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. tostring (); <br/>}< br/> finally <br/> {<br/> con. close (); <br/>}

 

2. Access to export Excel

Oledbconnection con = new oledbconnection (); <br/> try <br/>{< br/> savefiledialog SaveFile = new savefiledialog (); <br/> SaveFile. filter = ("Excel file (*. XLS) | *. xls "); // specify the file suffix as an Excel file. <Br/> If (SaveFile. showdialog () = dialogresult. OK) <br/>{< br/> string filename = SaveFile. filename; <br/> If (system. io. file. exists (filename) <br/>{< br/> system. io. file. delete (filename); // delete a file if it exists. <Br/>}< br/> int Index = filename. lastindexof ("//"); // obtain the last/index <br/> filename = filename. substring (index + 1); // obtain the Excel name (the path of the new table to the savefiledialog path) <br/> // select * into create a new table. <Br/> // [[Excel 8.0; database = Excel name]. [sheet name] $ is not allowed for creating a new sheet table, and $ is required for inserting data into the sheet table. <br/> // sheet can store up to 65535 data records. <Br/> string SQL = "select top 65535 * into [Excel 8.0; database =" + filename + "]. [user information] From users2 "; <br/> con. connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ application. startuppath + "// appdata. mdb "; // put the database under the DEBUG directory. <Br/> oledbcommand COM = new oledbcommand (SQL, con); <br/> con. open (); <br/> COM. executenonquery (); </P> <p> MessageBox. show ("exported data", "exported data", messageboxbuttons. OK, messageboxicon. information); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. tostring (); <br/>}< br/> finally <br/> {<br/> con. close (); <br/>}

 

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.