Example #1
0
        public System.Data.DataSet ReadExcelFile(String filename)
        {
            // Create connection string variable.
            String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                       "Data Source=" + Server.MapPath("~/Images/" + filename) + ";" +
                                       "Extended Properties=Excel 8.0;";

            // Create connection object
            System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(sConnectionString);

            // Open connection with the database.
            objConn.Open();

            // The code to follow uses a SQL SELECT command to display the data from the worksheet.

            // Create new OleDbCommand to return data from worksheet.
            System.Data.OleDb.OleDbCommand objCmdSelect = new System.Data.OleDb.OleDbCommand("SELECT * FROM [Products$]", objConn);

            // Create new OleDbDataAdapter that is used to build a DataSet
            // based on the preceding SQL SELECT statement.
            System.Data.OleDb.OleDbDataAdapter objAdapter1 = new System.Data.OleDb.OleDbDataAdapter();

            // Pass the Select command to the adapter.
            objAdapter1.SelectCommand = objCmdSelect;

            // Create new DataSet to hold information from the worksheet.
            System.Data.DataSet objDataset1 = new System.Data.DataSet();
            try
            {
                // Fill the DataSet with the information from the worksheet.
                objAdapter1.Fill(objDataset1, "Products");
            }
            catch (Exception exp)
            {
                objConn.ResetState();
                objConn.Dispose();
                objConn.Close();
            }
            //objConn.Dispose();
            // Clean up objects.

            objConn.Close();


            return(objDataset1);
        }