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.
Related
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();
}
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.
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.
here my code which need to save data from excel sheet to database access using C#. I don't get any error but my excel data could not be updated in database access.. What I have to do for this?
My Database Access Tablename is "addsales1".
My Excel sheet name is "addsales1".
string file_name = Application.StartupPath + "\\" + "databaseset.txt";
System.IO.StreamReader objreader;
objreader = new System.IO.StreamReader(file_name);
string sh = objreader.ReadLine();
string con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox9.Text
+ ";Extended Properties=Excel 8.0;";
OleDbConnection connection = new OleDbConnection(con);
OleDbCommand cmd = new OleDbCommand("INSERT INTO [MS Access;Database=" + sh +
"].[addsales1] SELECT * FROM [addsales1$]");
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Sales Details Imported Successfully!");
Here's the setup:
I have an excel spreadsheet that has a very simple page. It looks like so:
I use the following connection string to access this file:
string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO\";", fn)
My function to access the file looks like:
try
{
string select = string.Format("SELECT * FROM [{0}$]", tab.PageName);
OleDbDataAdapter adapter = new OleDbDataAdapter(select, con);
DataSet ds = new DataSet();
adapter.Fill(ds, tab.PageName);
// DEBUG: Let's just see what it is getting...
for (int x = 0; x < 13; x++)
{
for (int y = 0; y < 3; y++)
{
Console.Write(ds.Tables[0].Rows[x][y].ToString() + "\t");
}
Console.WriteLine("");
}
}
catch
{ ... }
QUESTION
Why would the code NOT read some cells? Note that there is the text "Profit" at C5. I can read B5 just fine as "Revenue". I can read C6 just fine as an integer value. But Profit seems to vanish.
This isn't such a big problem with the header information, but entire blocks of real data refuse to be read. Instead it returns DBNull, even when the cell contains real, valid, usable data. The cells are all formatted exactly the same between cells that can read and cells that return DBNull.
I'm truly stumped!!!
Any thoughts?
new OleDbConnection("...TypeGuessRows=0;ImportMixedTypes=Text");
I have a hunch you may be experiencing a problem that I had previously.
Try adding those parameters to your connection string.
I was having trouble getting the other answer+comment to work. The only setting needed was the IMEX=1 but it must be nested in single quotes in the Extended Properties, so here's an example of exactly how to format the additional IMEX setting:
connection.ConnectionString =
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\somepath\somefile.xls;Extended Properties='Excel 8.0;IMEX=1';";
if (ObjFile.Extension == ".xls")
conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srcFilePath + ";" + "Extended Properties='Excel 8.0;HDR=YES;'";
if (ObjFile.Extension == ".xlsx")
conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFilePath + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";
if (ObjFile.Extension == ".xlsm")
conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFilePath + ";Extended Properties='Excel 12.0 Macro;HDR=No;IMEX=1';";