0

I am working with c# in WPF. Pulling data into datatables. Everything was working OK until I changed to using the actual worksheet name as pulled from the sheet via a foreach (worksheet in workbook) loop.

Now that I have the actual worksheet name and include it in my OLEDbCommand, the worksheet open on the screen.

I would like to prevent/stop the Excel file from opening on the screen as it is not needed nor desired.

Below is the connection string and the beginning of the try/catch that has the commands and query.

string con_string = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullFilePath + ";Extended Properties='Excel 12.0 Xml;HDR=Yes'";
try
{
    OleDbConnection con = new OleDbConnection(con_string);
    con.Open();
    //OleDbCommand ocon = new OleDbCommand("SELECT * FROM [" + myMonth + " " + year + "$]", con);
    OleDbCommand ocon = new OleDbCommand("SELECT * FROM [" + myWorkSheet + "$]", con);
    OleDbDataAdapter sda = new OleDbDataAdapter(ocon);
    sda.Fill(data);
    dGrid.DataContext = data;
}

If I revert back to the commented out line using the myMonth and year variables (created in a SelectionChanged method from a Calendar object), the spreadsheet does not open.

The following is the code that access and creates the list of actual worksheets I use to populate a comboBox dropdown.

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelBook = xlApp.Workbooks.Open(fullFilePath);
String[] excelSheets = new String[excelBook.Worksheets.Count];
int i = 0;
foreach (Microsoft.Office.Interop.Excel.Worksheet wSheet in excelBook.Worksheets)
{
    excelSheets[i] = wSheet.Name;
    cmbBox2.Items.Add(excelSheets[i]);
    i++;
}
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
lsieting
  • 85
  • 2
  • 13
  • the oledbconnection would not open a visible Excel instance. I guess that somewhere, `xlApp` is still instantiated and used. – Cee McSharpface Jun 06 '17 at 15:51
  • @dlatikay -- The Excel xlsx file opens when I execute the OleDbCommand. Maybe because I am directly referencing a worksheet with the myWorksheet string? – lsieting Jun 06 '17 at 20:12
  • I cannot reproduce this and also cannot think of a scenario where connecting with OleDb *alone* would launch and show Excel... as it is [not even required to be installed](https://stackoverflow.com/a/3971925/1132334) on the computer. What are the runtime values of `ocon.CommandText` in both cases? – Cee McSharpface Jun 06 '17 at 22:04
  • @dlatikay -- this is interesting. This morning my program runs and does NOT bring up the xlxs file. Works as it is designed. I hate transient problems like these. Thank you for your help. (No changes made to the methods) – lsieting Jun 07 '17 at 12:23
  • My case I can duplicate this case in vb.net, I use System.Data.OleDb.OleDbConnection to get excel table. Case#1 If I already open excel and call ExcelCNN = New System.Data.OleDb.OleDbConnection(vCNNstring) then ExcelCNN.Open(), excel is opened. Case#2 do as case #1 but no excel opened before, no new excel opend. Then adding xlApp.DisplayAlerts = false; and xlApp.Visible = false; Do not help to prevent. – ปรีดา ตั้งนภากร Apr 10 '19 at 15:04

1 Answers1

0

Add these two lines-

xlApp.DisplayAlerts = false;
xlApp.Visible = false;

below this line-

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • @souvik-ghoush --- tried this. Excel sheet still opens the .xlsx file when referenced by the OleDbCommand. Thanks – lsieting Jun 06 '17 at 20:09