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)