如何防止重新打开打开的excel文件,如果它已经打开,只需将其称为Windows窗体应用程序 [英] how to Prevent reopening an opened excel file and if its already opened just call it like a windows form app

查看:153
本文介绍了如何防止重新打开打开的excel文件,如果它已经打开,只需将其称为Windows窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我希望有人能真正关注我的问题并帮助我..我迷路了...我想检查或确定如果 excel文件 已经打开,如果 已经打开然后我想调用那个excel文件并从带来,就像我们那样做  windows窗体应用中,如果  '  s尚未打开,然后从那里打开excel文件..这是我的代码,但我真的输了:

private void button1_Click (对象发送者,EventArgs e)
{
string mysheet = @C:\Users\John11\Desktop\Kamote.xlsx;
Excel.Application excelApp = new Excel.Application();
Excel.Workbooks books = excelApp.Workbooks;
Excel.Workbook sheet = books.Open(mysheet);

int i = 1;
if(excelApp.DisplayAlerts == null)
{
//如果它打开那么执行打开EXCEL文件
MessageBox.Show(尚未打开文件);

}


其他
{
//如果它已经打开我想要打电话给表格申请表,不创建EXCEL文件的另一个实例
MessageBox.Show(文件打开);

}

//这些代码只有在打开时才能工作,但是我已经
做了我自己的操作和操作我知道要达到我想要的功能的代码但我' M LOST ..所以我希望有人可以帮助我..

解决方案

我认为你可以像我们在Windows窗体中那样做,但我还没试过它



   每个 variable_name  as  Excel  in  application.openExcel 
if typeof variable_name Name_of_file 然后
variable_name。 activate()
end if
下一步


我当前的开发机器上没有安装Excel,但我过去使用过这段代码:

  using 系统; 
使用 System.Runtime.InteropServices;

命名空间 YourNameSpace
{
public static class ExcelUtilities
{
public 静态 bool IsExcelInstalled()
{
return ! (Type.GetTypeFromProgID( Excel.Application)== );
}

public static Microsoft.Office.Interop.Excel .Application StartExcel()
{
if (!IsExcelInstalled()) throw new ApplicationException( 未安装Excel。);

Microsoft.Office.Interop.Excel.Application instance = null ;

try
{
instance =(Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject(< span class =code-string>
Excel.Application);
}
catch (COMException ex)
{
instance = new Microsoft.Office.Interop.Excel.Application();
}

// 将Excel带到前面
instance.ActiveWindow.Activate();

return 实例;
}
}
}


Hi guys i hope that someone can really pay attention to my problem and could help me.. I am lost.. I want to check or determine if the excel file is already opened, if it is already opened then I want to call that excel file and bring to from like what we do in windows form app, and if it's not yet opened then from there just open that excel file.. HERE IS MY CODES but i am really lost:

private void button1_Click(object sender, EventArgs e)
{
string mysheet = @"C:\Users\John11\Desktop\Kamote.xlsx";
Excel.Application excelApp = new Excel.Application();
Excel.Workbooks books = excelApp.Workbooks;
Excel.Workbook sheet = books.Open(mysheet);

int i = 1;
if (excelApp.DisplayAlerts ==null)
{
//IF IT IS OPEN THEN EXECUTE OPEN THE EXCEL FILE
MessageBox.Show("no opened file yet");

}


else 
{ 
//AND IF IT ALREADY OPENED I WANT TO CALL THAT TO FRONT LIKE A FORM APPLICATION, NOT CREATING ANOTHER INSTANCE OF THAT EXCEL FILE 
MessageBox.Show("File is opened");

}

//THESE CODES WORKS ONLY WHEN OPENING IT BUT I'VE TRIED DOING MY OWN AND MANIPULATING CODES THAT I KNOW TO REACH THE FUNCTION I WANT BUT I'M LOST.. SO PLEASE I HOPE SOMEONE CAN HELP ME.. 

解决方案

i think you can do it like we do it in windows forms but i haven't try it

for each variable_name as Excel in application.openExcel
if typeof variable_name is Name_of_file then
variable_name.activate()
end if
Next


I don't have Excel installed on my current dev machine, but I used this code in the past:

using System;
using System.Runtime.InteropServices;

namespace YourNameSpace
{
    public static class ExcelUtilities
    {
        public static bool IsExcelInstalled()
        {
            return ! (Type.GetTypeFromProgID("Excel.Application") == null);
        }

        public static Microsoft.Office.Interop.Excel.Application StartExcel()
        {
            if (!IsExcelInstalled()) throw new ApplicationException("Excel is not installed.");

            Microsoft.Office.Interop.Excel.Application instance = null;
            
            try
            {
                instance = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
            }
            catch (COMException ex)
            {
                instance = new Microsoft.Office.Interop.Excel.Application();
            }

            // bring Excel to front
            instance.ActiveWindow.Activate();

            return instance;
        }
    }
}


这篇关于如何防止重新打开打开的excel文件,如果它已经打开,只需将其称为Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆