We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Excel Object close

Hello ASPOSE TEAM,

We have my workbook object in my application as
Workbook workbook = new Workbook(filePath);
can you tell us how to close this object or end the process.
The same thing we have it in Interop excel as
if (application != null &&
application.Workbooks != null &&
application.Workbooks.Count < 2)
{
application.DisplayAlerts = false;
application.Quit();
Kill(application.Hwnd);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(application);
application = null;
}
private static void Kill(int hwnd)
    {
int excelPID = 0;
int handle = hwnd;
GetWindowThreadProcessId(handle, ref excelPID);
    </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">Process</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> process </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(0, 0, 139);">null</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(0, 0, 139);">try</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
    </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        process </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">Process</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">GetProcessById</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">excelPID</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">

        </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: gray;">//</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: gray;">// If we found a matching Excel proceess with no main window</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: gray;">// associated main window, kill it.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: gray;">//</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(0, 0, 139);">if</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">process </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">!=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(0, 0, 139);">null</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
            </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(0, 0, 139);">if</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">process</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">ProcessName</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">ToUpper</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">()</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(128, 0, 0);">"EXCEL"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">&&</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">!</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">process</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">HasExited</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
                process</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(43, 145, 175);">Kill</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">();</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
        </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
    </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; color: rgb(0, 0, 139);">catch</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;">}</span></code></pre><pre class="lang-cs prettyprint prettyprinted" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; overflow: auto; width: auto; max-height: 600px;"><i><code style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.333333969116211px; vertical-align: baseline;"><span style="background-color: rgb(255, 255, 255);">here application object is</span><span style="background-color: rgb(245, 245, 220);"> </span></span></code><span class="typ" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175);">Microsoft</span><span class="pun" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">.</span><span class="typ" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175);">Office</span><span class="pun" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">.</span><span class="typ" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175);">Interop</span><span class="pun" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">.</span><span class="typ" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175);">Excel</span><span class="pun" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">.</span><span class="typ" style="font-size: 13.333333969116211px; line-height: 16.666667938232422px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175);">Application object</span></i></pre><pre class="lang-cs prettyprint prettyprinted" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.333333969116211px; line-height: 16.666667938232422px; background-color: rgb(255, 255, 255); margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; overflow: auto; width: auto; max-height: 600px;"><br></pre><pre class="lang-cs prettyprint prettyprinted" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 16.666667938232422px; background-color: rgb(255, 255, 255); margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; overflow: auto; width: auto; max-height: 600px;"><font size="2">With </font><font color="#ff1493" size="4">aspose </font><font size="2">we tried using like below</font></pre><pre class="lang-cs prettyprint prettyprinted" style="background-color: rgb(255, 255, 255); margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; overflow: auto; width: auto; max-height: 600px;"><font face="Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif"><span style="line-height: 16.65625px;">workbook.Save(HttpContext.Current.Response, fileName, ContentDisposition.Inline, new OoxmlSaveOptions(SaveFormat.Xlsm));</span></font>

and tried the below
workbook = null;
if (HttpContext.Current.Response.IsClientConnected)
{
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
HttpContext.Current.Response.End();
GC.Collect();
GC.WaitForPendingFinalizers();


But we could not find any improvement…
IS there any methods like workbook.close  in ASPOSE to close or end  EXCEL PROCESS
FYI, we are using latest ASPOSE version 7.5.2…
Can u Get back to us on this ASAP…
please its an urgent Requirement to Us… 

THANQ







Hi,

Thanks for your posting and using Aspose.Cells for .NET.

We are afraid, there is no such API as Workbook.Close() to close the Excel process. When you use Workbook.Save method, it automatically close the Excel process after saving the workbook onto disk or into stream.

Hi..

Thank you for Reply...

Workbook.Save is working fine, when we are saving or opening .XLS files.

The Process is not ending for .XLSM files. we are getting following error message

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException (0x800703E3): An error occurred while communicating with the remote host. The error code is 0x800703E3.
at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.Flush()
at Aspose.Cells.Workbook.Save(HttpResponse response, String fileName, ContentDisposition contentDisposition, SaveOptions saveOptions)
at CASTAdhocReporting.BAL.ExcelPivot.GetTemplateReport()
at CASTAdhocReporting.Presentation.DownloadExcelFile.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.presentation_downloadexcelfile_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\11a1ac2a\f89b2959\App_Web_nohogpkp.0.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)v

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please provide us your sample runnable project replicating this issue. We will look into your issue at our end and update you asap.

hi shakeel

if I use Workbook.save(filepath,SaveFormat.xlsm) method.. Issue is resolved.

Hi,

Thanks for your posting and using Aspose.Cells.

It is good to know that you were able to sort out this issue. If you encounter any other issue, please feel free to post on our forums, we will be glad to help you further.