Problem reading .dbf file using OleDbConnection. How to set up the encoding correctly?
My Code:
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + inpath + ";Extended Properties=dBASE IV;Persist Security Info=False;");
I tried like this but got an error:
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + inpath + ";Extended Properties=dBASE IV;Persist Security Info=False;CharacterSet=WIN1251;");
import.txt
OEEAOOAI_A?EEIAOE
Related
I'm using OleDbDataAdapter
I'm trying to get data from excel to DataTable, Here is my code;
var conn = new OleDbConnection(strConn);
var myCommand = new OleDbDataAdapter(" SELECT * FROM [UPLOADFILE$] ", strConn);
var inputTable = new DataTable();
try
{
myCommand.Fill(inputTable);
}
Here is my Conn Str;
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
Somehow, It eliminates the strings starting with "0", but others are okay.
I used the following combinations, as well;
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;IMEX=0;TypeGuessRows=0;ImportMixedTypes=Text\"";
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;HDR=No;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
Thanks in advance!
My Tries;
Excel 8.0;HDR=No;IMEX=1;
For what it's worth, this is my go-to connection string for reading excel via OleDb.
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'; ";
For what it's worth I tried it on some data and had no issues when I had text with leading zeroes.
Is your data in a table or is it just a standard range? Does it have headers? Can you show some sample data that is erroring out?
I'm using an oledbconnection to connect to an excel spreadsheet
Connection string:
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" +
Request.PhysicalApplicationPath + "tmp\\" + Session["Id"] + ".xlsx;" + "Extended Properties=\"Excel 8.0;HDR=NO;\"";
Update query:
cmdUpdate = new OleDbCommand("UPDATE [Contributions$B6:B6] SET F1 = 'value'", excelConn);
However, the data is being set in cell B1, not B6. All other queries are also adding the data in row 1, but the correct column...any ideas?
I'm developing an MVC .Net web application, and I want to read some data from Excel file.
I am using those connection strings:
if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
}
else if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
}
Reading .xls files works great, but trying to read .xlsx files (created in Excel 2013) is not working, giving me the exception:
External table is not in the expected format.
I've been trying all the possible connection strings that I found:
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=2\"";
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=2\"";
...
...
...
but the I can't solve this problem.
thanks!
Hey your connection is looks good.like this
public static string path = #"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
But make sure Microsoft Access Database Engine 2010 Redistributable package is installed on your machine.You can download from here Link
For excel connection string you can navigate this url Link
OleDbConnection excelConnection=null;
try
{
if (Path.GetExtension(excelFileName).Equals(".xls"))
{
string conStr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFileName + ";Extended Properties=" + "\"Excel 8.0 Xml;HDR=YES;IMEX=1;\"";
excelConnection = new OleDbConnection(conStr);
}
else
{
string conStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\";";
excelConnection = new OleDbConnection(conStr);
}
excelConnection.Open(); ***// this statement get the error!!!***
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
This is my connection strings:
string 1.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\~Projects IW\Desktop APPS\Maga\Book1.xls;Extended Properties="Excel 8.0 Xml;HDR=YES;IMEX=1;"
string 2.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\~Projects IW\Desktop APPS\Maga\Book1.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1;";
When reading xls file using connection string 1 get error
"could not find installable isam"
But reading xlsx file using connection string 2 ;it is works fine: NO Errors!
Error is because of connection
for .xls try this connection string:
StrConn = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
and for .xlsx try this connection string:
StrConn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
Your first connection string includes Excel 8.0 Xml; which will not work. Use Excel 8.0; instead.
I Work On C# Project
I Use Below Code For Import XLS Or XLSX File On DataSet.
public static DataSet ImportExcelXLS(string FileName, bool hasHeaders)
{
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
}
else
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
}
DataSet output = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
try
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
sheet = sheet.Replace("$", "");
DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
}
}
}
}
return output;
}
This Code Work Correctly For Load Excel File Without Password. But When Run For Load a File With password Show Under error:
Cannot start your application. The workgroup information file is
missing or opened exclusively by another user.
Now How Change My Code For Load Encrypt File?
Please : use this lib:
using Microsoft.Office.Interop.Excel
Please , provide password :
WorkbookObject.Password = password;
Please, Change ConnString:
string conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + s + ";Password=password;Extended Properties='Excel 8.0;HDR=YES'";
Here, your answer is:
if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Password=password;Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
}
else
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Password=password;Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
}
Try for this code:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Password=password;Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
but still the exception popped:
Cannot start your application. The workgroup information file is missing or opened exclusively by another user.