Unable to open Excel connection in C# - c#

I tried these two connection strings:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + this.m_SourceFileName + ";Extended Properties='Excel 12.0;HDR=No; IMEX=1;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.m_SourceFileName + ";Extended Properties='Excel 8.0; HDR=No; IMEX=1;
I'm getting an exception
External table is not in the expected format
The same files are working after just open and close the file manually. Then tried to read the same code is working.
Kindly help to know the exact issue with Excel.
This is the code I tried:
this.m_ConnectionToExcelBook =
new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + this.m_SourceFileName + ";Extended Properties='Excel 12.0;HDR=Yes; IMEX=0;'");
try
{
this.m_ConnectionToExcelBook.Open();
}
catch
{
this.m_ConnectionToExcelBook =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.m_SourceFileName + ";Extended Properties='Excel 8.0; HDR=No; IMEX=1;'");
this.m_ConnectionToExcelBook.Open();
}

Related

This .XLSX connection string does not seem to work even with multiple modifications

I know there are several resources out there on ACE OLEDB connection strings for .xlsx files.
This is my current connection string:
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + uploadFilePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
I tried
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + uploadFilePath + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
Also tried
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + uploadFilePath + ";Extended Properties="Excel 12.0 Xml;HDR=YES";
I have tried several on connectionstrings.com and also from stackoverflow
Usually my errors are
{"External table is not in the expected format."}
or
{"Format of the initialization string does not conform to specification starting at index 118."}
What am I doing wrong? Any suggestions on the right connection string to use? .xls files are working just fine
Hello my friend please take a look in to my connection string :
mainFolder is a path to folder example : C:\Users\username\Desktop\Main
comboboxtext is full name of excel file example : SomeSample.xlsx
using (OleDbConnection connection =
new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + mainFolder + "" + #"\" + comboBox2.Text + ";Extended Properties=\"Excel 12.0;HDR=YES;\""))

Using oledb after fileupload

I am having a problem using the oledb engine to open an excel file after using a fileupload dialog. If I set up the file name for the oledb connection with a fixed file name it works, but if I try to get the filename from a fileupload control i get an error External table is not in the expected format. The file is not open in another application. I have tried disposing and closing all file content in the fileupload. My code is as follows
if (FileUpload1.HasFile)
{
//sXLConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strvirtpath + "\\Unlocked Upload-v5.xlsx " + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
sXLConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strvirtpath + "\\" + FileUpload1.FileName + " ;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
FileUpload1.FileContent.Flush();
FileUpload1.FileContent.Close();
FileUpload1.FileContent.Dispose();
FileUpload1.Dispose();
}
OleDbConnection xlConn = new OleDbConnection(sXLConnStr);
xlConn.Open();
Any help would be appreciated.
Save the file first and then access it using the path where it is saved.
FileUpload1.SaveAs(directory + #"\" + filename);

Reading Custom column name instead of Value from excel using Oledb

I have Excel Data Like above. Whenever i trying to get the value as 12/1/2015. I used to get 1-Dec,2-Dec etc Like this image
bool hasHeaders = true;
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (path.Substring(path.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml; HDR =" + HDR + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
retrieving the data from Excel.
OleDbDataAdapter dataHeader = new OleDbDataAdapter("SELECT * FROM [" + sheet + Coords + "]", Connection);
dataHeader.Fill(tableHeader);
please find the mistake.

Cannot parse some excel column

Good day!
I try to open and parse excel file into DataSet.
So, i use OleDbConnection:
if (_filePath.Substring(_filePath.LastIndexOf('.')).ToLower() == ".xlsx")
// strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
// + _filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ _filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=" + HDR + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text;\"";
// strConn="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _filePath + ";Extended Properties=Excel 12.0;";
else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\"";
But some column are empty!
The next column parses well (it with same data).
Can you tell me how to fix it?
Then i fill Dataset:
OleDbConnection conn = new OleDbConnection(strConn);
System.Data.DataSet dtSet;
System.Data.OleDb.OleDbDataAdapter oleCommand;
oleCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "]", conn);
oleCommand.TableMappings.Add("Table", sheetName);
dtSet = new System.Data.DataSet();
oleCommand.Fill(dtSet);
oleCommand.Dispose();
conn.Close();
return dtSet.Tables[0];
But, some columns are empty!
May be, it happens because excel file has format:
Cell1--------------|Value1------------|
Cell2---|Cell3-----|Value2---|Value4--|
So, dataset fill columns :
Cell1---|-------|--Value1------|-----|
Cell2---|Cell3--|---Empty(!)---|Value4|
So, i need to get Empty(!) column.
About invalid data at column.
I copy and paste this column at right column- and it works!
But,i should use last format, not mine.
HDR="NO";
Maybe you ran into this error:
OleDB & mixed Excel datatypes : missing data
What's the value of 'HDR'? Take a look at the Datatypes of the Columns, maybe they are mixed.

Opening an Excel (created with EPPLUS) with OleDB

I have code from a colleague, this code creates a few excel sheets with Epplus. With my code I would like to add an database extract 10k+/- lines. Because of the large amount of data it takes too long with Epplus, because you need to write each cell. With OleDB it only takes a few seconds. But I can't open a previously created excel by Epplus with OleDB. Even with different connection strings.
This my code works perfect if you separate the two code blocks.
var excelPath = "C:\\test_" + DateTime.Today.ToString("yyyyMMdd_") + DateTime.Now.ToString("hh") + DateTime.Now.Minute.ToString() + ".xlsx";
using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(excelPath)))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Schedule V");
worksheet.Cell(1, 1).Value = "test";
xlPackage.Save();
xlPackage.Dispose();
}
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml';";
using (OleDbConnection conn = new OleDbConnection(strCn))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "CREATE TABLE [table1] (id INT, name VARCHAR, datecol DATE );";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(1,'AAAA','2014-01-01');";
cmd.ExecuteNonQuery();
conn.Close();
}
I tried the following connection strings but they all give the same error:
OleDbException was unhandled, External table is not in the expected
format.
My diffrent connection string I tried:
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;HDR=YES';";
var strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
What am I doing wrong here?
I guess, it should be like this, rather than hardcoding you should use OleDbConnectionStringBuilder class
OleDbConnectionStringBuilder connectionStringBuilder = new OleDbConnectionStringBuilder();
connectionStringBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connectionStringBuilder.DataSource = excelPath; // This is your Excel File Full Path
connectionStringBuilder.Add("Mode", "Read");
const string extendedProperties = "Excel 12.0;IMEX=1;HDR=YES";
connectionStringBuilder.Add("Extended Properties", extendedProperties);
String connectionString = connectionStringBuilder.ToString();
// Create connection object by using the preceding connection string.
using (var objConn = new OleDbConnection(connectionString))
{
// Open connection with the database.
objConn.Open();
// Do operations with your File here
}
I was getting this error consistently with .xlsx files created with EPPlus 4.0.4. I remembered this was working prior to upgrading to EPPlus 4.x. I downgraded to 3.1.3.3 and I no longer get this error.

Categories

Resources