为什么这个Using()会给我一个错误?

发布于 2024-12-01 18:41:54 字数 371 浏览 2 评论 0原文

我正在尝试打开一个(实际上数百个)Excel 文件。我打开应用程序,但想要在我打开的每个工作簿周围使用Using() 功能。为什么这会导致错误?

using (Excel.Workbook wbXL = appXL.Workbooks.Open(_sourceFullPath, Type.Missing, Excel.XlFileAccess.xlReadOnly))
{
    //stuff with wbXL
}

using 得到红色下划线并显示“'Microsoft.Office.Interop.excel.Workbook':using 语句中使用的类型必须隐式转换为'System.IDisposable'。

如何使其工作?

I am trying to open an (hundreds actually) excel file(s). I open the application but want to use the Using() functionality around each of the workbooks I open. Why is this resulting in an error?

using (Excel.Workbook wbXL = appXL.Workbooks.Open(_sourceFullPath, Type.Missing, Excel.XlFileAccess.xlReadOnly))
{
    //stuff with wbXL
}

using gets the red underline and says "'Microsoft.Office.Interop.excel.Workbook':Type used in a using statement must be implicitly convertible to 'System.IDisposable'.

How to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

¢好甜 2024-12-08 18:41:54

基本上就是它所说的 - 您只能将 using 与实现 IDisposable 的类一起使用,以便编译器在幕后知道在终结时调用哪个函数 - yourclass.Dispose()。 Excel 互操作类不实现此功能。

因此,您有两个选择:

  1. 为 Excel.Workbook 编写自己的包装类,该类实现 IDispose,并且公开对象本身以调用方法,或者也包装这些方法,例如

    公共类 DisposableWorkbook : IDisposable
    {
        私人 Excel.Workbook _workbook = null;
    
        公共DisposableWorkbook(Excel.Application appXL,字符串路径,
                                  NotSureOfType otherArgument,
                                  Excel.XlFileAccess 访问)
        {
            _workbook = appXL.Workbooks.Open(路径, otherArgument, 访问);
        }
    
        公共 Excel.Workbook 工作簿
        {
            获取{返回_workbook; }
        }
    
        公共无效处置()
        {
            if(工作簿!= null)
            {
                工作簿.关闭(Excel.XlSaveAction.xlDoNotSaveChanges,
                               工作簿关闭);
                工作簿=空;
            }
        }
    }
    
    使用 (DisposableWorkbook dwbXL = new DisposableWorkbook(appXL,
              _sourceFullPath、Type.Missing、Excel.XlFileAccess.xlReadOnly))
    {
         Excel.Workbook wbXL = dwbXL.Workbook;
         // wbXL 的东西
    }
    
  2. Implement using你自己,例如

    Excel.Workbook wbXL = null;
    尝试
    {
        wbxl = appXL.Workbooks.Open(_sourceFullPath, Type.Missing,
                                    Excel.XlFileAccess.xlReadOnly);
        //与wbXL相关的东西
    }
    最后
    {
        if (wbxl != null) wbxl.Close();
    }
    

Pretty much what it says - you can only use using with classes that implement IDisposable, so that under the covers the compiler knows which function to call on finalisation - yourclass.Dispose(). The Excel interop classes don't implement this.

So you've got two choices:

  1. Write your own wrapper class for Excel.Workbook that implements IDispose and either exposes the object itself to call methods on, or wraps those methods too, e.g.

    public class DisposableWorkbook : IDisposable
    {
        private Excel.Workbook _workbook = null;
    
        public DisposableWorkbook(Excel.Application appXL, String path,
                                  NotSureOfType otherArgument,
                                  Excel.XlFileAccess access)
        {
            _workbook = appXL.Workbooks.Open(path, otherArgument, access);
        }
    
        public Excel.Workbook Workbook
        {
            get { return _workbook; }
        }
    
        public void Dispose()
        {
            if (workbook != null)
            {
                workbook.Close(Excel.XlSaveAction.xlDoNotSaveChanges,
                               workbookToClose);
                workbook = null;
            }
        }
    }
    
    using (DisposableWorkbook dwbXL = new DisposableWorkbook(appXL,
              _sourceFullPath, Type.Missing, Excel.XlFileAccess.xlReadOnly))
    {
         Excel.Workbook wbXL = dwbXL.Workbook;
         // stuff with wbXL
    }
    
  2. Implement using yourself, e.g.

    Excel.Workbook wbXL = null;
    try
    {
        wbxl = appXL.Workbooks.Open(_sourceFullPath, Type.Missing,
                                    Excel.XlFileAccess.xlReadOnly);
        //stuff with wbXL
    }
    finally
    {
        if (wbxl != null) wbxl.Close();
    }
    
两个我 2024-12-08 18:41:54

using 语句中的任何项都必须实现 IDisposable 接口。我手头没有文档,但我猜 Excel.Workbook 没有实现此接口。

Any item in a using statement must implement the IDisposable interface. I haven't got the documentation to hand, but I'll guess Excel.Workbook doesn't implement this interface.

星光不落少年眉 2024-12-08 18:41:54

using 语句(C# 参考)

提供方便的语法,确保正确使用
IDisposable 对象。

Excel.Workbook 未实现 IDisposable,因此您不能使用 using 来实现它。

using Statement (C# Reference):

Provides a convenient syntax that ensures the correct use of
IDisposable objects.

Excel.Workbook does not implement IDisposable, so you can't use using for it..

流心雨 2024-12-08 18:41:54

你无法让它发挥作用。

using 块用于尽可能快速、安全地从实现 IDisposable 接口的对象中释放资源。

Excel.Workbook 未实现 IDisposable,因此您无法声明它在 using 块中使用。

You can't make it work.

The using block is used to free resources from objects that implement the IDisposable interface as quickly and as safely as possible.

Excel.Workbook does not implement IDisposable so you can't declare it for use in a using block.

猛虎独行 2024-12-08 18:41:54
Businessmanger emb = new Businessmanger();
        try
        {


            TempData["deparmentList"] = Deplist;
            return PartialView("create");
        }
        catch (Exception)
        {


            throw;
        }
        finally {
            //object dispose here
                            ((IDisposable)emb).Dispose();
        }
Businessmanger emb = new Businessmanger();
        try
        {


            TempData["deparmentList"] = Deplist;
            return PartialView("create");
        }
        catch (Exception)
        {


            throw;
        }
        finally {
            //object dispose here
                            ((IDisposable)emb).Dispose();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文