how to stop excel from appearing when opening in c# using Microsoft.Office.Interop.Excel;

Richard collier 1 Reputation point
2022-06-14T19:59:13.65+00:00

using Microsoft.Office.Interop.Excel; i open the excel file using Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(location/filename);
when i use this the file opens and i would like for that not to happen and for the program to read the excel file in the background. I tried
lApp.Visible = false;
xlApp.DisplayAlerts = false;
before the Open statement but it still appears.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,488 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,641 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Richard collier 1 Reputation point
    2022-06-15T13:29:33.413+00:00

    Hi I tried it again and still get the same result (excel opens for a second and closes)
    private void button1_Click(object sender, EventArgs e)
    {
    Excel.Application spreadsheet = new Excel.Application();
    spreadsheet.Visible = false;
    Excel.Workbook excelWorkbook = spreadsheet.Workbooks.Open(location workbook);
    excelWorkbook.Close();
    spreadsheet.Quit();
    }


  2. Karen Payne MVP 35,036 Reputation points
    2022-06-15T19:17:44.757+00:00

    Usually one needs to make more of an effort for what they want out of Excel via automation rather than Open XML.

    I invite you to examine the following code which I've used the pattern shown and never had your issue plus the code ensure (unless there is a runtime exception) all objects are released used.

    0 comments No comments

  3. Richard collier 1 Reputation point
    2022-06-20T14:34:13.867+00:00

    Sorry for the delay i believe my issue was not adding the visible=false till after i had the range done