简体   繁体   中英

Epplus Excel Library C#

I want to know how to close the ExcelPackage (epplus)? I have searched about it everywhere, but all the epplus codes are written with using keyword.

For example, once we open the file stream (fstream), we need to essentially close the file back. In Java, once we use BufferedReader or Scanner object, we need to close it with .close() or .dispose() .

Here is my purpose:
I want to implement a class that has attributes of ExcelPackage and ExcelWorksheet and some other methods and attributes. I am implementing a custom Excel Object using OOP technique so that I can use my custom Excel library to manipulate Excel files for my company.
My custom Excel Class has
constructor with a filename parameter
init() - initialize the ExcelPackage and store it in the attribute ,
create() - for creating excel with our data
some methods for excel functionalities
save() - for saving the Excel File
close() - for closing the ExcelPackage <= I need to know how to dispose/close the excel package I instantiate with init() method

You can use using statement to automatically disposes of the object. As msdn says:

The using statement in C# and the Using statement in Visual Basic simplify the code that you must write to cleanup an object. The using statement obtains one or more resources, executes the statements that you specify, and automatically disposes of the object. However, the using statement is useful only for objects that are used within the scope of the method in which they are constructed.

In addition, ExcelPackage implements IDisposable :

public sealed class ExcelPackage : IDisposable

An example of using:

using (ExcelPackage package = new ExcelPackage(someNewFile))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM