Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Dear Friends'

I have one process in which i have to generate 7 Reports and Export in Excel format.
Once generation complete i have to consolidate this all Excel data in one single Excel file with required format change and Response to user.

All done on Web base application created using Dot Net 2.0 and C# language report using Crystal Report tool.

The issue occurs at the time of Consolidation, I am using Microsoft.Office.Interop.Excel.dll to Open two file at time (one Main and other Child) once Import and Formatting done I am using following code to Release that code and kill process, once all done the final Excel Save and close and pass to User.

All code try to manage as much as possible but still one of those file stuck and wont allow to generate next report (I have keep the name of file same as can't effort utilizing Server memory for this files which are not required once process complete as next time this file are overwrite by new file).

To Open
C#
Excel.Application xlobj = new Excel.ApplicationClass();
Excel.Workbook w;//= new Excel.Workbook(); 
Excel.Worksheet s;

w = xlobj.Workbooks.Open(path, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
   true, false, 0, true, false, false);

s = (Excel.Worksheet)w.Worksheets.get_Item("Sheet1");


To Close
C#
w.Save();
w.Save();
w.Close(true, path, Type.Missing);
Marshal.ReleaseComObject(w);

xlobj1.Quit();
TryKillProcessByMainWindowHwnd(xlobj1.Hwnd);
Marshal.ReleaseComObject(xlobj1);


C#
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
    uint processID;
    GetWindowThreadProcessId((IntPtr)hWnd, out processID);
    if (processID == 0)
        return false;
    try
    {
        System.Diagnostics.Process.GetProcessById((int)processID).Kill();
    }
    catch (ArgumentException) { return false; }
    catch (Win32Exception) { return false; }
    catch (NotSupportedException) { return false; }
    catch (InvalidOperationException) { return false; } return true;
}


I Just bang my head from last two days to make this code stream line, but in vain, help me if you have any best process and let me know.

I am thinking to create one window application which kill all those unwanted not in use Excel file from Task Manager but this is not logical but still searching for that as need to make customer happy.

Thanks and Regard's
Sasmi
Posted
Updated 7-Oct-14 3:17am
v2
Comments
Maciej Los 7-Oct-14 9:20am    
I suggest to use Google with the sentence: 'C# Excel release object'
[no name] 7-Oct-14 10:03am    
Looks quite reasonable, your comment I mean :) . But I understand OP with the problem he has. ... and these problems seems to change from release to release.
Anyway my virtual 5.
Kind regards, Bruno
Maciej Los 7-Oct-14 10:43am    
Thank you, Bruno ;)
The reason why it's so hard to answer this question is that the 'web application' sentence together with 'Office interop' could means everything.
qadirv 7-Oct-14 10:07am    
Not a solution to your problem, but maybe a suggestion for the future. Check out OpenXML for Excel. It allows you to read and write to excel at lightning fast speeds as compared to the interop. Unless there are calculations on the excel, you're far better off using OpenXMl.
Richard Deeming 7-Oct-14 11:03am    
http://support2.microsoft.com/kb/257757[^]:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900