C # reading Excel files

Source: Internet
Author: User

1. Introduce the corresponding namespace:

View plaincopy to clipboardprint?
    1. UsingSystem;
    2. UsingSystem. Collections. Generic;
    3. UsingSystem. text;
    4. UsingSystem. Windows. forms;
    5. UsingSystem. Data;
    6. UsingSystem. Data. oledb;
    7. UsingSystem. Data. sqlclient;

2. Read the Excel file and store the content in Dataset

View plaincopy to clipboardprint?
  1. /// <Summary>
  2. /// Read the Excel file and store the content in Dataset
  3. /// </Summary>
  4. /// <Param name = "opnfilename"> Excel file name with path </param>
  5. /// <Returns> dataset </returns>
  6. Public StaticDataset exceltodataset (StringOpnfilename)
  7. {
  8. StringStrconn ="Provider = Microsoft. Jet. oledb.4.0; Data Source ="+ Opnfilename +"; Extended properties = \" Excel 8.0; HDR = yes; IMEX = 1 \"";
  9. Oledbconnection conn =NewOledbconnection (strconn );
  10. StringStrexcel ="";
  11. Oledbdataadapter mycommand =Null;
  12. Dataset DS =NewDataset ();
  13. Strexcel ="Select * from [sheet1 $]";
  14. Try
  15. {
  16. Conn. open ();
  17. Mycommand =NewOledbdataadapter (strexcel, strconn );
  18. Mycommand. Fill (DS,"Dtsource");
  19. ReturnDS;
  20. }
  21. Catch(Exception ex)
  22. {
  23. // MessageBox. Show ("Import error:" + ex, "error message ");
  24. Console. writeline (Ex );
  25. ReturnDS;
  26. }
  27. Finally
  28. {
  29. Conn. Close ();
  30. Conn. Dispose ();
  31. }
  32. }

3. Insert the read data to the database.

View plaincopy to clipboardprint?
  1. /// <Summary>
  2. /// Save all information
  3. /// </Summary>
  4. Public Static VoidSaveall (Dataset DS)
  5. {
  6. Foreach(Datarow rowInDS. Tables [0]. Rows)
  7. {
  8. Console. writeline ("{0} \ t {1} \ t {2 }", Row [0], row [1], row [2]);
  9. Userinfo =NewUserinfo ();
  10. Userinfo. Username = (String) Row [1];
  11. Userinfo. userpwd = (String) Row [2];
  12. Add (userinfo );
  13. }
  14. }
  15. Private Static VoidAdd (userinfo)
  16. {
  17. StringSqlcon ="Server =.; uid = sa; Pwd = 123; database = ACCP";// Connection string
  18. Sqlconnection con =NewSqlconnection (sqlcon );// Obtain the connection object
  19. Sqlcommand COM =NewSqlcommand ("Insert into userinfo values ('"+ Userinfo. username +"','"+ Userinfo. userpwd +"')", Con );
  20. Con. open ();
  21. IntRows = com. executenonquery ();
  22. If(Con! =Null)
  23. {
  24. Con. Close ();
  25. }
  26. }

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.