I am using the following piece of code to write into the excel file dynamically on a ASP.net form that can save the data table to excel.
//Create Excel Object
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(target);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
worksheet.Name = "Worksheet1";
excel.Visible = false;
//Generate Fields Name
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
object[,] objData = new object[rowNo, columnNo];
for (int row = 0; row < rowNo; row++)
{
for (int col = 0; col < columnNo; col++)
{
objData[row, col] = dataTable.Rows[row][col];
}
}
Microsoft.Office.Interop.Excel.Range range;
range = worksheet.Range[excel.Cells[2, 1], excel.Cells[rowNo + 1, columnNo]];
range.Value2 = objData;
worksheet.Columns.AutoFit();
workbook.Save();
workbook.Close();
IntPtr hwnd = new IntPtr(excel.Hwnd);
IntPtr processId;
IntPtr foo = GetWindowThreadProcessId(hwnd, out processId);
Process proc = Process.GetProcessById(processId.ToInt32());
proc.Kill();
//excel.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
The data is written fine into the excel file. However, when I try to kill the process using proc.Kill() as above, I get the following exception
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: Access is denied
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[Win32Exception (0x80004005): Access is denied]
System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited) +1985316
System.Diagnostics.Process.Kill() +49
Faculty_Report1.FetchData_EXCEL(String prog, String cls, String spcln, String fromdate, String todate) +2413
Faculty_Report1.fetchselectedvalues_EXCEL() +1044
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +187
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +165
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
Instead of using the proc.Kill() method, I have tried using excel.Quit() method but the process does not terminate. When I do this, I end up with a new excel process everytime a user tries to generate a report.
I have user impersonation in the web.config file, but this does not change anything.
<system.web>
<identity impersonate="true" userName="xxxxxx" password="xxxxxxxx" />
</system.web>
Any pointers for me on how to avoid the exception and make sure that the excel process terminates fine? I have tried the COM clean up solution which I found on here but it doesn't work..