This question already has answers here:
Excel "External table is not in the expected format."
(25 answers)
Closed 8 years ago.
Iam trying to open an XLSX file (so that I can create a datatable )
Iam using the the below code.
System.Data.OleDb.OleDbConnection oleDbCon;
System.Data.OleDb.OleDbDataAdapter oleDbDataAd;
oleDbCon = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filePath + "';Extended Properties=Excel 8.0;");
but when file path contains a file with extension xlsx , i get an error
"External table is not in the expected format."
Above code works fine when file is of extention xls.
Do I have to change the connection string ?
Any help ?Thanks in Advance.
Changed the connection string to
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ path + ";Extended Properties=Excel 12.0;";
see Excel "External table is not in the expected format."
.xlsx has different connectionstrings
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="
+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel="select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds,"table1");
return ds;
Related
string connStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
OleDbConnection MyConnection;
OleDbCommand MyCommand = new OleDbCommand();
MyConnection = new OleDbConnection(#connStr);
MyConnection.Open(); // Here the error come...
Im try to using this connStr to connect to My xls file. The problem was "External table is not in the expected format."
Im have aleady installed AccessDatabaseEngine_x64. But the error still remain.
How to connect to this .xls file ?
I have an excel file and an oledb connection to it. When reading data while file is opened in windows, it throws the following error (at Adapter.Fill method).
However, the code runs fine when file is not opened manually.
private System.Data.DataSet GetExcelData()
{
// Create new DataSet to hold information from the worksheet.
System.Data.DataSet objDataset1 = new System.Data.DataSet();
DataTable dt = new DataTable();
try
{
string path = ConfigurationManager.AppSettings["ExcelFilePath"];
//string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
OleDbConnection objConn = new OleDbConnection(connectionString);
objConn.Open();
//String strConString = "SELECT * FROM [Data Version5.2$A2:ZZ] where [Status] = 'aa'";//Status
String strConString = "SELECT * FROM [Data Version5.2$A2:ZZ] where [Status] IS NULL OR [Status]='SubReport'";//Status SubReport
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Fill the DataSet with the information from the work sheet.
objAdapter1.Fill(objDataset1, "ExcelData");
objConn.Close();
}
catch (Exception ex)
{
throw ex;
}
return objDataset1;
}
The error message is
Assuming you don't need to write to the file, try adjusting your connection string to include the read only mode (Mode=Read). I have that in all of mine (where I don't need to write to the file) and I've never had a problem reading from workbooks that are already open:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
path + ";Mode=Read;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
I also don't tend to read Excel files as XML, so the Extended Properties for my connection strings are Excel 12.0;HDR=YES;IMEX=1;
I am working on a webform which has a file upload and a button and a gridview. user can upload excel file to database and also see preview in gridview .It is workin fine.
my code is
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/Publisher/ExcelFiles/") + filename);
DataSet ds = new DataSet();
string path = #"~/Publisher/ExcelFiles/" + filename;
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand myComm = new OleDbCommand("select * from [GIRLS$] ", myCon);
OleDbDataAdapter da = new OleDbDataAdapter(myComm);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
but my problem is path of excel file it gives me an error at da.Fill(ds);
'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Publisher\ExcelFiles\glist.xls' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
excel file resides in my website directory. This error don't come if I changed the path to
string path= #"D:\Vikas Rana\New folder (4)\glist.xls";
any help will be appreciated.
Thanks in advance.
try this
string filename = Path.GetFileName(FileUpload1.FileName);
DataSet ds = new DataSet();
string path = Server.MapPath("~/Publisher/ExcelFiles/" + filename);
FileUpload1.SaveAs(path);
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;
DataSource=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand myComm = new OleDbCommand("select * from [GIRLS$] ", myCon);
OleDbDataAdapter da = new OleDbDataAdapter(myComm);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
if you upload .xls file use this
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path(which you prefer);Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"
if you upload .xlsx file use this
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=path(which you prefer);Extended Properties="Excel 12.0;HDR=YES;IMEX=1;"
I mean
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;
DataSource=" + path + ";Extended Properties=Excel 8.0;HDR=YES;IMEX=1;");
I am creating a C# program where I can browse and view the file into a datagridview in C#. I've been tested it for so many times and it worked well. But yesterday when I tried to do it again an error messaged displayed stating "Provider=Microsoft.Jet.OLEDB.4.0;". The file that I am testing is the same file that I used before(with no errors). I didn't do anything with the file(excel file).
This is the print screen of the error message:
This is the code I used:
private void buttonUpload_Click(object sender, EventArgs e)
{
string OleDBConnection = String.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text);
string query = String.Format("select * from [{0}$]", "Sheet1");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, OleDBConnection);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
If Path.GetExtension(pth).ToLower().Equals(".xls") Then
strcon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & pth & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;"""
Else
strcon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & pth & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"""
End If
I am getting an error when I attempt to read the records from my xlsx file.
The error is "Cannot update. Database or object is read-only."
I have verified that the file is not read-only. Any ideas what could be causing the error?
My code is:
string strFileName = System.IO.Path.GetFileName(txtSourcePath.Text);
string strFilePath = txtSourcePath.Text;
string strDirectoryPath = strFilePath.Substring(0, (txtSourcePath.TextLength - (strFileName.Length + 1)));
string conn = string.Format(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""text;HDR=YES;FMT=DELIMITED""", strDirectoryPath);
OleDbConnection oleDBConn = new OleDbConnection(conn);
oleDBConn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * FROM [" + strFileName + "]", conn);
DataSet ds = new DataSet();
da.Fill(ds);
When I hover over ds I don't see any records
Try This
for xls
<add key="xlsConnection" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=##PATH##;Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"/>
for xlsx
<add key="xlsxConnection" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=##PATH##;Extended Properties='Excel 12.0;HDR=YES;'"/>
Required to install : microsoft.ace.oledb.12.0 driver
Replace Path of your excel file path