1

In the following code i am trying to check if an Excel file is open , if it is then I want to close it , when i run the code , the file does not get closed , can you please help?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

namespace CloseIfFileOpen
{
    class Program
    {
        public static void Main()
        {
            Excel.Application oApp;
            Excel.Workbook oBook;

            oApp = new Excel.Application();

            oBook =oApp.Workbooks.Add(@"C:\Users\user\Documents\WEF\Excel\Example.xlsx");


            string filePath;

            filePath = @"C:\Users\user\Documents\WEF\Excel\Example.xlsx";


            try
            {
                using (File.Open(filePath, FileMode.Open)) { }
            }
            catch (IOException e)
            {
                var errorCode = Marshal.GetHRForException(e) & ((1 << 16) - 1);

                //return errorCode == 32 || errorCode == 33;
                MessageBox.Show("the file is unavailable now");

                oBook.Save();
                oBook.Close();
                oApp.Quit();
            }
        }
   }
}
4

6 回答 6

1

我是否正确假设您要检查该文件是否正在被另一个进程使用,如果是,请关闭它?因为我认为在 Windows 中没有任何方法可以做到这一点,所以您只能关闭自己对文件的使用。可能有办法通过使用 Win32 API 来强制解锁文件,但 C# 中没有内置任何内容。对文件调用 close 只会关闭你自己对该文件的使用,它不会影响其他进程。

于 2013-11-04T13:02:58.220 回答
0

您并不是说要独占访问该文件。似乎不太可能,但也许 Excel 不会锁定文件,以便其他进程可以以读取模式打开文件。也许您的文件打开行应该是:

using (File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { }

于 2013-11-04T13:06:00.520 回答
0

我正在使用此方法检查是否可以打开文件,如果可以,则执行,如果不能,则不要将参数路径设置为 excel 工作表路径

    private Microsoft.Office.Interop.Excel.Application appExcel;
    private Workbook newWorkbook = null;
    private _Worksheet objsheet = null;

    public void excel_init(String path)
    {
        appExcel = new Microsoft.Office.Interop.Excel.Application();

        if (System.IO.File.Exists(path))
        {
            // then go and load this into excel
            newWorkbook = appExcel.Workbooks.Open(path, true, true);
            objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
        }
        else
        {
            Console.WriteLine("Unable to open workbook");
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
            appExcel = null;
        }

    }
于 2013-11-04T12:57:28.343 回答
0

这段代码肯定会帮助您检查文件是否打开(我检查了此代码是否为 excel 文件)

private bool check_file_oppen(string check)
    {//where string check is the path of required file

        try
        {
            Stream s = File.Open(check, FileMode.Open, FileAccess.Read, FileShare.None);
            s.Close();
            MessageBox.Show("FILE IS NOT OPEN");
            return true;
        }
        catch (Exception)
        {
            MessageBox.Show("FILE IS OPEN");

            return false;
        }
    }

现在,“如果这个 excel 文件是打开的,那么如何关闭它?” 是我仍在寻找的问题,............

如果你找到答案,请与我分享,......

谢谢。

方舟。

于 2014-07-06T15:55:04.107 回答
0
        foreach (var process in Process.GetProcessesByName("EXCEL"))
        {
            process.Kill();
        }
于 2017-11-02T15:35:28.380 回答
0

在java中,您可以使用这种方法-

//Open the excel file using Desktop.getDesktop()
    Desktop.getDesktop().open(new 
    File("C:\\Users\\AnubhavPatel\\Desktop\\Testing_Code_Macro.xlsm"));
    Thread.sleep(5000);
//Save and close it using robot class
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_F4);
    robot.keyRelease(KeyEvent.VK_F4);
    robot.keyRelease(KeyEvent.VK_ALT);

    Thread.sleep(5000);

//Then write to it using outputStream   
    FileOutputStream outputStream = new FileOutputStream(
            "C://Users//AnubhavPatel//Desktop/Testing_Code_Macro.xlsm");
    workbook.write(outputStream);
    outputStream.close();
于 2020-05-20T07:24:43.240 回答