C# Excel Interop prevent excel window from becoming visible

喜欢而已 提交于 2019-12-12 00:03:13

问题


I make a c# app that does some computations in an Excel file that is load by Excel Interop.

I use this to open the excel file using this function:

private Workbook OpenExcelFile(String path, String password = null)
    {
        if(!File.Exists(path))
        {
            MessageBox.Show("Error loading" + path);
        }

        if (null == excel)
        {
            excel = new Application();
        }
        excel.Visible = false;

        Workbook wb = null;
        if (password != null)
        {
            wb = excel.Workbooks.Open(path, ReadOnly: true, Password: password);
            return wb;
        }

        wb = excel.Workbooks.Open(path, ReadOnly: true);
        return wb;
    }

I can then use the Workbook in the application. like this OpenExcelFile("file.xlsx", "Password")

When I open the compiled exe I see a new Excel.exe instance in the Task Manager which is fine but the Excel Window is not show.

Now I have a problematic case: I use excel 2010 and do this:

I create a new excel file on the Desktop and open it. Now not 1 but 2 excel Windows are visible in the task bar. first is "The new Excel file" the second is "file.xlsx" which is used by the software and should not be visible to the user as it contains some secret business logic, we do not want the user to know. I can also use the switch between excel sheets button in excel to make see the "secret" excel file. Also the excel sheet is unlocked. No need to enter the password.

When I do the same with Excel 365 only the "The new Excel file" is opened. I do not see the hidden excel file and I can not see any content. I'm not promented to open the file or anything. The "file.xlsx" is not visible to me.

So my question is:

Is there a way to prevent the file to become visible in excel 2010. Is there a way to make the file even visible in excel 365 without entereing the password. This would also be a bad situation for us.

Oh, I know about this: Prevent Workbooks.Open() from making excel visible in windows explorer

来源:https://stackoverflow.com/questions/53836349/c-sharp-excel-interop-prevent-excel-window-from-becoming-visible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!