Home>

I'm sorry for the rudimentary thing, but could you please tell me?

Current status

After starting EXCEL with Microsoft.Office.Interop.Excel in C #, I created a process to write characters in any cell and exit EXCEL after saving (repeating EXCEL from start to finish multiple times). The process doesn't end and keeps getting stuck in the task.

What you know

The same phenomenon occurs even when EXCEL is executed only by starting and ending.
When it is repeated about 50 times, it consumes the memory of the PC and the operation becomes considerably slow.
About 5 seconds of the process remains after the EXCEL termination process is completed.

Contents of question

I have confirmed that "C: \ test \ test.xlsx" closes at the timing of exiting the using process, but it takes about 5 seconds from exiting using to the end of the process.
1. Does it take a few seconds from exiting the using process with the following source to the end of the process?
2. What kind of modification is required to terminate the EXCEL process when the using process is exited?

Source
// Execute the following using process multiple times with the for statement
using (ExcelOpenClose excel = new ExcelOpenClose ())
{
excel.Open ("C: \ test \ test.xlsx");
// Write and save process to EXCEL
}
// Below, another class process (used in using process)
public class ExcelOpenClose: IDisposable
{
private Application application1 = null;
private Workbook workbook1 = null;
private Worksheet worksheet1 = null;
protected virtual void Dispose (bool disposing)
{
if (! DisposedValue)
{
if (worksheet1! = null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject (worksheet1);
worksheet1 = null;
}
if (workbook1! = null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook1);
workbook1 = null;
}
}
if (application1! = null)
{
try
{
application1.Quit ();
}
finally finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject (application1);
}
application1 = null;
}
GC.Collect ();GC.WaitForPendingFinalizers ();GC.Collect ();
}
}

public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
public void Open (string filePath)
{
try
{
application1 = new Application ()
{
Visible = true
};
string path = Directory.GetCurrentDirectory ();
workbook1 = application1.Workbooks.Open (filePath);
worksheet1 = workbook1.Worksheets ["Sheet1"];
}
catch (Exception ex)
{
throw (ex);
}
}
}
c#
  • Answer # 1

    Please stop using Interop.Excel and first consider whether you can substitute it with a library such as EPPlus or ClosedXML.
    Since the file is read and written directly without going through the Excel process, the speed is extremely fast and there is no worry that the process will remain.

    If you are eating up memory, it is possible that the release process has not been implemented correctly.
    I haven't read the code because the indentation is terrible for the time being, but please check if it is implemented as follows.
    Release objects assigned by Office Automation – Part1
    Release objects assigned by Office Automation --Part2