i have the following problem with an excel file that i want to uppercase the values of its respective cells in C# with the next code:
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connection = factory.CreateConnection();
string stringConnection = String.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=NO;IMEX=1'",
excelPath);
connection.ConnectionString = stringConnection;
connection.Open();
DbCommand updateExcel =
factory.CreateCommand();
updateExcel.CommandText =
"UPDATE [sheet1$] SET lastname = UCASE(lastname), name = ucase(name)";
updateExcel.Connection = connection;
updateExcel.ExecuteNonQuery();
conection.Close();
connection.Dispose();
and it raise an oledbexception about parameters doesnt specified, anybody could help me?
If you are trying to connect to an Excel 2007 or 2010 spreadsheet you will need the following connection string...
OleDbConnection xlconnection = new OleDbConnection();
xlconnection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + #";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"
Related
When I am trying to open a excel into one of my windows service using following code it is throwing "Value cannot be null. Parameter name: source" on objConn.Open(); Can any one please help me.
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
LogManager LogWrite = new LogManager();
try
{
string conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Repository\RuleExcel\Rules_Repository_2018-06-28_03-41-29-133.xlsx;Extended Properties='Excel 12.0;HDR=YES;';";
LogWrite.WriteLog(conn);
// Create connection object by using the preceding connection string.
objConn = new OleDbConnection(conn);
LogWrite.WriteLog(objConn.DataSource);
// Open connection with the database.
objConn.Open();
try this below code, it works for me :
using (OleDbConnection objConn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + #";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""))
{
objConn.Open();
}
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 ?
try
{
string connectionString = string.Empty;
if (Path.GetExtension(fileName) == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
";Extended Properties=Excel 12.0;";
}
else
{
Debug.Print(connectionString);
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.Combine(Server.MapPath("~/Content"), fileName) + ";Extended Properties=Excel 8.0;";
//connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path.Combine(Server.MapPath("~/Content"), fileName) + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
}
OleDbCommand selectCommand = new OleDbCommand();
OleDbConnection connection = new OleDbConnection();
OleDbDataAdapter adapter = new OleDbDataAdapter();
connection.ConnectionString = connectionString;
if (connection.State != ConnectionState.Open)
connection.Open();
//connection.Get
DataTable dtSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
connection.Open() seems to be connecting just fine. Now I have gotten a dtSchema Data Table object. I am needing to get the name of the Excel worksheet or worksheets that were gotten if any. I am also scanning the object to find out where the column names are. There is supposed to be some columns in the schema. Is it documented where the columns are?
When I do some output in my immediate window:
dtSchema.Columns[0].ToString()
"TABLE_CATALOG"
dtSchema.Columns[1].ToString()
"TABLE_SCHEMA"
dtSchema.Columns[2].ToString()
"TABLE_NAME"
dtSchema.Columns[3].ToString()
"TABLE_TYPE"
These are not the column names of the first worksheet, which is what I am looking for mainly the name of the first worksheet and its columns.
Thank you for posting..
If you want to look for column names. See the resultant COLUMN_NAME column
DataTable dtCols = this.connection.GetSchema("Columns");
TABLE_NAME is also helpful for your case to identify the Sheet.
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;
Is it possible to write data using Oledb into a common excel ?
There are no table structure or anything, it's a user document.
When I tried, i had always an OleDbException
"INSERT" query reply :
Operation must use an application that can be updated.
"UPDATE" query reply :
No value given for one or more required parameters.
My code:
using (OleDbConnection connection = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + #"C:\Users\[...]\Classeur.xls" + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;READONLY=FALSE\""))
{
connection.Open();
OleDbCommand commande = new OleDbCommand(
"INSERT INTO [Feuil1$](F1,F2,F3) VALUES ('A3','B3','C3');", connection);
commande.ExecuteNonQuery();
connection.Close();
connection.Dispose();
}
New test (without sucess !) :
using (OleDbConnection connection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + #"C:\Users\[...]\Classeur.xls" + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;READONLY=FALSE\""))
{
string column = "A";
string row = "1";
string worksheetName = "Feuil1";
string data = "TEST";
connection.Open();
string commandString = String.Format("UPDATE [{0}${1}{2}:{1}{2}] SET F1='{3}'", worksheetName, column, row, data);
OleDbCommand commande = new OleDbCommand(
commandString, connection);
connection.Close();
connection.Dispose();
}
I finally found !
Simple question of IMEX ( So many hours lost for that !)
So if anyone have the same issue :
//for reading data
Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;READONLY=FALSE\"
//for writing data
Extended Properties=\"Excel 8.0;HDR=NO;IMEX=3;READONLY=FALSE\"
This IMEX situation for Writing Data was driving me crazy for months, I had to remove it to make it work.
I just found [CheapD] answer and it works flawless, Thank you Cheap.
I would suggest to add the MODE parameter:
Extended Properties='Excel 12.0; HDR=Yes; IMEX=3; MODE=Share; READONLY=False';