I have an issue that is very similar to a question already posted here but not resolved. This issue is that when my app reads from an excel file (can happen with a CSV too) with oledb and the file is already opened in Excel (not through the app, the user just has it opened separately) a new Excel will open the file in read-only mode. If the file isn't opened at all, no Excel comes up. It happens right when the connection is opened - conn.Open()
Here is a method for reference:
public static DataTable GetDataTableFromExcelSheet(string ExcelFilePath, string SheetName)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"";
string query = "SELECT * from [" + SheetName + "$]";
System.Data.DataTable dt = new System.Data.DataTable();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, conn))
{
dataAdapter.Fill(dt);
}
conn.Close();
}
return dt;
}
Is there a way I can prevent Excel opening? Or at least not make it visible? (the app doesn't try to run the Excel app anywhere, the class doesn't even have interop)
Related
I am building an app to read measurement data (saved as .xls files on our network) from comparators throughout our facility via a Webform. When I query this group of files I only retrieve the headers. My code (pretty standard) will read any other Excel file I can find. I tried to remove a file from the network and save it locally, as well as rename and save it from office 2016. Note - these are .xls files and I can't change that.
** - I just discovered that there are named ranges with the same name as the file. The results I am getting are just the values in the named range and not the data from the workbook.
Here is an example of how they are named (autogenerated) "R87_1RCR0009654S_COIN"
If I rename or remove the named range this works fine.
Is there a way I can change my select statement to read these?
Here is a code sample, not sure there if there is a change I can make here to read these files.
private string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
private string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
string conStr, sheetName;
conStr = string.Format(Excel03ConString, info.FullName, "YES");
string fullPathToExcel = info.FullName;
//Get the name of the First Sheet.
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
con.Close();
}
}
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = "SELECT * From [" + sheetName + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
}
}
}
I have a solution where i need to read values from Excel. I am using OLEDB connection to do the same. It all works fine when i run the solution in local. But when i host it in IIS i get the JIT compiler pop up when the oledb connection code is hit. Not able to figure out the problem.
Below is the OLEDB connection code :
public static DataSet FirstColumn(string filename)
{
string filepath = filename;
string sheetName = "First";
//Oledb connection to generate excel in temporary folder on server
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filepath + "';Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
conn.Open();
OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter
("select * from [" + sheetName + "$]", conn);
DataSet excelDataSet = new DataSet();
objDA.Fill(excelDataSet);
return excelDataSet;
}
The popup has an option to Debug or continue. When i debug i get "An unhandled win32 exception occurred in w3wp.exe"
Has anyone faced this issue?
I want to export data from an excel file to mysql database table. I use the following code to get the data from the excel file;
string path = label4.Text;
String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + path + ";" +
"Extended Properties=Excel 12.0;";
OleDbConnection xlConn = new OleDbConnection(connectionString);
xlConn.Open();
OleDbCommand selectCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", xlConn);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = selectCommand;
DataTable dataSet = new DataTable();
dataAdapter.Fill(dataSet);
xlConn.Close();
This code works fine when 'Enable Editing' is ON on the excel file. But, it throws the following exception when 'Enable Editing' is OFF:
System.Data.OleDb.OleDbException: 'External table is not in the expected format'
How can I access the excel file even when 'Enable Editing' is OFF?The design of excel file is not on my hands. Thanks.
I have set a password on my excel sheet. To unlock the worksheet, I modified my OLEDB connection string, but it didn't work. I got an error that "the source contains no dataRows", which means that it couldn't read the data from the excel file.
Before it was fine. What might be my problem?
This is my code:
public DataTable getExcelData(string fileName, string sheetName, ComboBox[] User_ComboBox)
{
this.m_comboBox = User_ComboBox;
// connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 12.0 XML;HDR=No;IMEX=1'";
connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Password=xyz;Extended Properties='Excel 8.0;HDR=YES'";
string errorMessage = "";
DataTable dt = new DataTable();
try
{
string query = "SELECT * FROM [" + sheetName + "]";
OleDbConnection con = new OleDbConnection(connectionString);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, con);
dataAdapter.Fill(dt);
}
catch (Exception exp)
{
// errorCode = ErrorDefinition.ERROR_OLEDBERROR;
errorMessage = exp.Message;
}
return dt;
}
Instead of using an OleDbConnection you might consider using an Excel reading library like EPPlus to read the excel file. EPPlus supports reading password protected excel files.
You would need to implement some code to build the actual DataTable, but that should be simple enough. Just google it if you need to.
I'm trying to write a class that will read information from an excel file, but for some reason, it will only run exception-free if the file in question is open in excel. Otherwise, the class throws an OleDbException. The code is as follows:
String filename = #"C:\Users\me\Documents\File.xls";
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=YES'";
using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();
OleDbCommand selectCommand = new OleDbCommand("select * from [Sheet1$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand);
DataTable dt = new DataTable();
adapter.Fill(dt);
int counter = 0;
foreach (DataRow row in dt.Rows)
{
String dataA= row["DataA"].ToString();
String dataB= row["DataB"].ToString();
Console.WriteLine(DataA+ " = " + dataB);
counter++;
if(counter>=40)
break;
}
The error is thrown on conn.Open(), and only occurs when I don't have File.xls open in excel at the same time. Can someone help me fix this? I am not well versed enough with OLEDB to figure out why this is happening. Thanks!