WinForm reading Excel data into the form

Source: Internet
Author: User

Recently teaching, need to use WinForm read Excel data, so do a simple, nonsense not much to say, direct codding ...

1 //read the Help class for Excel2 classSqexcellhelper3     {4          Public StaticDataTable GetData (stringtablename)5         {6DataTable dtemp =NewDataTable (tablename);7OleDbConnection con =NewOleDbConnection ();8             //Persist Security Info boolean type. When True, it is indicated that an integrated safety mechanism is used, and if false, an integrated security mechanism is not adopted. 9             //con. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +Ten             //"Data Source = employee.xlsx; Persist Security Info=false; Mode=readwrite; Extended properties=excel 8.0 "; One  A             //string strconn = "provider=microsoft.ace.oledb.12.0;" + "Data source=" + Server.MapPath ("excelfiles/ Mydata2007.xlsx ") +"; -             //Extended properties= ' Excel 12.0; Hdr=yes; Imex=1 ' ";  -             //This connection can manipulate the. xls and. xlsx files (connection strings that support Excel2003 and Excel2007) the             //remark: "Hdr=yes;" Is that the first line of the Excel file is the column name instead of the data, "HDR=NO;" It's just the opposite of the front.  -             //"Imex=1" if the data type in the column is inconsistent, the use of "imex=1" will avoid data type collisions.  -   -             //Note: Depending on the requirements, the data source here is the DataSource; change to the file name you need * +              -Con. ConnectionString ="provider=microsoft.ace.oledb.12.0;"+ +                 "Data Source = Prize.xls; Extended properties= ' Excel 12.0; Hdr=yes; Imex=1 ';"; A              at             Try -             { -  - con. Open (); -                 //String sql = "SELECT * FROM [sheet1$]"; -OleDbDataAdapter da =NewOleDbDataAdapter ("Select * FROM [sheet1$]", con); in da. Fill (dtemp); -             } to             Catch(Exception ex) +             { -  the Console.WriteLine (ex. Message); *             } $             finallyPanax Notoginseng             { - con. Close (); the             } +             returndtemp; A}
View Code

There is no use of layer three, the following code is to load the data directly into the WinForm data control, the code is as follows:

        /// <summary>        ///Form Load Events/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>Private voidMainyj_load (Objectsender, EventArgs e) {                         stringsql ="Select * FROM [sheet1$]"; DataTable DTP=sqlhelper.getdata (SQL);  This. Datagridview1.datasource =DTP; intRandnum = (DTP. Rows.Count-1);  This. TextBox1.Text = DTP. rows[randnum]["Prize name"].         ToString (); }
View Code

:

Due to the actual development of WinForm, not too much, here just do a simple demo, if there are errors, please point out the friend of the atom, thank you!

RELATED Links: http://blog.csdn.net/pan_junbiao/article/details/8872936

WinForm reading Excel data into the form

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.