Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataset from excel file

Status
Not open for further replies.

fegdvbna22

Programmer
Aug 9, 2006
52
GB
I'm using this code to obtain a dataset from a csv file. What do I need to change to obtain a dataset from an excel file?

public DataTable GetCSVDataSet(string strCheckFile)
{
string connString =
string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;",
Path.GetDirectoryName(strCheckFile));

string cmdString =
string.Format("SELECT * FROM {0} ", Path.GetFileName(strCheckFile));

DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();

OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(cmdString, conn);
adapter.Fill(dt);
}

return dt;
}
 
You need to change your connection string to include a reference to Excel (something like Extended Properties=Excel if I remember). Anyway, check out faq855-5780 for more info on connection strings.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
try this:

Code:
Function ExecuteSQL(ByVal FilePath As String, ByVal SQL As String) As DataSet
        Me.cError = ""
        Me.cData = New DataSet
        Try


            '"SELECT * FROM [Sheet1$]"
            'You must use the $ after the object you reference in the spreadsheet
            Dim myOleDbDataAdapter As New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & FilePath & "; Extended Properties=""Excel 8.0;""")

            myOleDbDataAdapter.Fill(Me.cData, "Data")
            Me.cError = Err.Description

        Catch objError As Exception

            Me.cError = Err.Description


        Finally

        End Try
        Return Me.cData


Babloome
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top