ASP. NET read (import) Execl. [dedicated to us who are still cainiao] (similar to databases)

Source: Internet
Author: User

The company asked me to import an Execl to SQL Server. However, fields are different from databases. Then the content of the corresponding field does not comply with the database standards. No way... you can only read it.

Public DataSet GetExcel ()
{
String strCon = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = c: \ sample.xls; Extended Properties = Excel 8.0"; // connect to Excel
OleDbConnection myConn = new OleDbConnection (strCon );
String strCom = "SELECT * FROM [Sheet2 $]"; // The table name of Sheet2 comes FROM the lower left corner of Excel. I didn't understand what this is.
MyConn. Open ();
// Open the data link to obtain a dataset
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom, myConn );
// Create a DataSet object
DataSet myDataSet = new DataSet ();
// Get your own DataSet object
MyCommand. Fill (myDataSet, "[Sheet2 $]");
// Close the data link
MyConn. Close ();
Return myDataSet;
}

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.