This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23175|Reply: 2

[Source] C# reads Excel 2007 files

[Copy link]
Posted on 8/11/2015 10:28:08 AM | | |

Read the access dataset   

For excel 2007:

    private DataSet GetExcelData(string str)
    {
        string strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + str + "; Extended Properties=\"Excel 12.0; HDR=YES\"";
        OleDbConnection myConn = new OleDbConnection(strCon);
        string strCom = " SELECT * FROM [Sheet1$]";
        myConn.Open();
        OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
        DataSet myDataSet = new DataSet();
        myCommand.Fill(myDataSet, "[Sheet1$]");
        myConn.Close();
        return myDataSet;
    }

For excel2003:

The connection string is changed to:

string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+str+"; Extended Properties=Excel 8.0";




Previous:Still failed, return to normal life next week.
Next:Asp.Net Get the file path, file name, and extension of the FileUpload control
 Landlord| Posted on 8/11/2015 11:37:40 AM |
Optimized the code

  1. public static DataSet GetExcelData(string str, string version)
  2.         {
  3.             string strCon = "";
  4.             if (version.Equals("2003")) {
  5.                 strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + str + ";Extended Properties=Excel 8.0";
  6.             }
  7.             if (version.Equals("2007")) {
  8.                 strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str + ";Extended Properties="Excel 12.0;HDR=YES"";
  9.             }
  10.             OleDbConnection myConn = new OleDbConnection(strCon);
  11.             string strCom = " SELECT * FROM [Sheet1$]";
  12.             myConn.Open();
  13.             OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
  14.             DataSet myDataSet = new DataSet();
  15.             myCommand.Fill(myDataSet, "[Sheet1$]");
  16.             myConn.Close();
  17.             return myDataSet;
  18.             
  19.         }
Copy code


Posted on 5/2/2018 4:07:53 PM |
mark
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com