C # Import and Export DataSet Execl Step by Step

Source: Internet
Author: User

Recently, the project needs to study the import and export of Execl.

This project generates another Execl from the data of one Execl, so I didn't use the database. -- Ps: It seems that Execl is also a database! Region ~
 

First, import Execl to DataSet

Import I selected the OLEDB connection method

The first step is to reference the namespace.

System. Data. OleDb;

 

Connect Execl

Code Try
{
// Use OleDbConnectionStringBuilder object to construct the connection string.
OleDbConnectionStringBuilder connectionStringBuilder = new OleDbConnectionStringBuilder ();
ConnectionStringBuilder. DataSource = @ "" + textBox1.Text; // textBox1.Text select the Execl file path + file name to be imported
ConnectionStringBuilder. Provider = "Microsoft. Jet. OLEDB.4.0 ";
ConnectionStringBuilder. Add ("Extended Properties", "Excel 8.0 ");
Using (OleDbConnection cn = new OleDbConnection (connectionStringBuilder. ConnectionString ))
{
Ds = new DataSet ();
OleDbCommand omd = new OleDbCommand (string. Format ("select * from [{0}]", comboBox1.Text), cn );
OleDbDataAdapter oda = new OleDbDataAdapter (omd );
Oda. Fill (ds );
DataGridView1.DataSource = ds. Tables [0];
}
}
Catch (Exception)
{
MessageBox. Show ("an exception occurred when connecting to Excel. Please check the background code! ");
}

 

To allow users to select different Sheet tables in the project, read the Sheet in Execl.

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.