C # import Excel into the dataset

Source: Internet
Author: User

classImport {/// <summary>        ///import Excel into the dataset/// </summary>        /// <param name= "Strfilesourse" >the file's path and full file name, with extension</param>        /// <returns></returns>         PublicDataSet Exceltodataset (stringstrfilesourse) {DataSet DS=NewDataSet (); //Excel Data source (compatible with 03/10)            stringstrconn ="Provider=microsoft.ace.oledb.12.0;data source="+ Strfilesourse +"; Extended properties=\ "Excel 12.0; Hdr=yes;imex=1\ ""; //ConnectionOleDbConnection conn =NewOleDbConnection (strconn); Try{Conn.                Open (); //get all the sheet in ExcelDataTable Dtsheet = conn. GetOleDbSchemaTable (OleDbSchemaGuid.Tables,New Object[] {NULL,NULL,NULL,"TABLE" }); //Read all the sheet data in Excel into a table                 for(inti =0; i < DtSheet.Rows.Count; i++)                {                    stringstrSheetName = dtsheet.rows[i]["table_name"].                    ToString (); OleDbDataAdapter Oleda=NewOleDbDataAdapter ("select * FROM ["+ strSheetName +"]", conn); Oleda.fill (DS,"TableName"); Conn.                Close (); }            }            Catch(Exception ex) {MessageBox.Show (ex).            Message); }            finally            {                if(Conn. State! =connectionstate.closed) {conn.                Close (); }            }            returnds; }    }

C # import Excel into the dataset

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.