I am creating a sharepoint webpart solution where i need to upload a excel file
Here is my code:
string tempFilename = "";
SPSecurity.RunWithElevatedPrivileges(delegate
{
tempFilename = System.IO.Path.GetTempFileName();
flUpload.SaveAs(tempFilename);
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";", tempFilename);
connectionString = @"Provider=Microsoft.ACE.OLEDB.14.0;Data Source="+tempFilename+@";ExtendedProperties=""Excel 12.0;HDR=YES;""";
var adapter = new OleDbDataAdapter("SELECT * FROM [Failed Trades$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
DataTable data = ds.Tables["anyNameHere"];
The Connection String generated is:
Provider=Microsoft.ACE.OLEDB.14.0;Data Source=C:\Windows\ServiceProfiles
\NetworkService\AppData\Local\Temp\Demo1.xls;
ExtendedProperties="Excel 12.0;HDR=YES;"
I had looked hundrededs of solutions but none of them is workimg.
Here are few solutions what i tried:
I checked my DSN as well
Change the version number of the OLEDB driver in your connection string from:
Microsoft.ACE.OLEDB.14.0
to
Microsoft.ACE.OLEDB.12.0
This should work presuming you've installed the Microsoft Access Database Engine 2010 Redistributable.