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.
Pergunta
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
Solução
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.
Outras dicas
You can try this:
Put the correct version in your connection String. like:
Microsoft.ACE.OLEDB.12.0 or Microsoft.ACE.OLEDB.14.0
You can select all:
public string tables(string strFileName)
{
DataTable dt = null;
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.15.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception a)
{
MessageBox.Show("15 - " + a.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.14.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception b)
{
MessageBox.Show("14 - " + b.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.13.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception c)
{
MessageBox.Show("13 - " + c.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception d)
{
MessageBox.Show("12 - " + d.Message);
}
}
}
}
}