Fresh Eyes Needed: Help me find the reason why Excel process is not killed properly.. | Newbie | | Join Date: Jul 2008
Posts: 1
| |
Hello,
First of all, Hi, and sorry for the lengthy explanation. :) Take a deep breath.
I'm trying to create a form which displays data in an html table, and an Export to Excel button which (amazingly) exports the displayed data to Excel (with all the formatting). I got this to work already, locally on my dev PC.
BTW, I am using another Excel file as a template.
However, I have 2 major problems.
1.) Firstly, the Excel process is not being terminated properly (note that I thought I understood Excel already, since I got another similar page to terminate Excel properly!!!) with the code below. Please give me your suggestions, or if you can, locate the specific problem.
2.) Secondly, when deploying/publishing this web application and accessing it remotely, an error occurs... more specifically,
"Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current
custom error settings for this application prevent the details of the
application error from being viewed remotely (for security reasons). It
could, however, be viewed by browsers running on the local server
machine."
Actually, I managed to solve this problem, I just dunno how I did it. So this is not so much a problem for now, as long as its working. But I would like to know if any of you knows the true solution, I would appreciate knowing it.
I do understand (kinda) why this problem occurs. It's because of the web application. When I click the Export to Excel button, it uses the Excel Object on the Web Server, inserts data to the worksheet, and saves the file on the Web Server (for reference purposes by the company, of all generated files) and then sends (user download) the file to the user requesting for the export. -
-
protected void btnExport_Click(object sender, EventArgs e)
-
{
-
DataTable dt = (DataTable)Session["dtBEV"];
-
string[] dayClass = (string[])Session["holidays"];
-
bool showOTbefore10 = chkOTbefore10.Checked;
-
bool showOTpast10 = chkOTpast10.Checked;
-
bool showLateEarly = chkLateEarly.Checked;
-
bool showSubsti = chkSubsti.Checked;
-
bool showLeaves = chkLeaves.Checked;
-
bool showSL = chkSL.Checked;
-
bool showAWOL = chkAWOL.Checked;
-
bool showHoliday = chkHoliday.Checked;
-
int i, j;
-
int emps = dt.Rows.Count;
-
int month = int.Parse(DropDownListMonth.SelectedValue);
-
int year = int.Parse(DropDownListYear.SelectedValue);
-
int days = DateTime.DaysInMonth(year, month);
-
string sTemp;
-
string filename;
-
string sCol;
-
bool isHol = false;
-
-
string[,] header = new string[2, days];
-
string[,] content = new string[emps, days + 2];
-
-
Excel.Application xlApp = null;
-
Excel.Workbook xlWB = null;
-
Excel.Worksheet xlWS = null;
-
Excel.Range cell = null;
-
Excel.Style style = null;
-
-
// Create Header
-
for (i = 1; i <= days; i++)
-
{
-
header[0, i - 1] = i.ToString();
-
}
-
for (i = 0; i < days; i++)
-
{
-
header[1, i] = GetDayOfWeek(year, month, i + 1);
-
}
-
-
// Create Content
-
for (j = 0; j < emps; j++)
-
{
-
content[j, 0] = Convert.ToString(j + 1);
-
content[j, 1] = dt.Rows[j]["name"].ToString();
-
for (i = 0; i < days; i++)
-
{
-
content[j, i + 2] = dt.Rows[j]["day" + (i + 1)].ToString();
-
}
-
}
-
-
try
-
{
-
GC.Collect();
-
xlApp = new Excel.Application();
-
xlWB = xlApp.Workbooks.Open(Server.MapPath(".") + "\\..\\Templates\\DTR - Monthly Summary Birds Eye View.xls",
-
0, false, 5, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, true, false, Missing.Value, false);
-
xlWS = (Excel.Worksheet)xlWB.ActiveSheet;
-
-
// create styles
-
style = xlWB.Styles.Add("borderStyle", Type.Missing);
-
style.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
-
style.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
-
style.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
-
style.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
-
style.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
-
style.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
-
style.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
-
style.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
-
style.Font.Name = "Arial";
-
style.Font.Size = "10";
-
-
// Write Headers
-
cell = xlWS.get_Range("C2", Type.Missing);
-
cell.Value2 = year.ToString() + " " + new DateTime(year, month, 1).ToString("MMMM");
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C3", Type.Missing);
-
cell = cell.get_Resize(2, days);
-
cell.Value2 = header;
-
-
// Search for Weekends and Holidays
-
for (i = 1; i <= days; i++)
-
{
-
isHol = false;
-
sTemp = GetDayOfWeek(year, month, i);
-
-
if (dayClass[i - 1] != null)
-
{
-
isHol = true;
-
}
-
-
if (sTemp == "Sa" || sTemp == "Su" || isHol)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
sCol = ColumnString(1 + i);
-
cell = xlWS.get_Range(sCol + "3:" + sCol + "4", Type.Missing);
-
cell.Font.ColorIndex = DayColor.AWOL;
-
}
-
}
-
-
// Write Content
-
if (emps > 0)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("A5", Type.Missing);
-
cell = cell.get_Resize(emps, days + 2);
-
cell.Value2 = content;
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("A5:AG" + (4 + emps), Type.Missing);
-
cell.Style = "borderStyle";
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("A5:A" + (4 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlRight;
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C5:AG" + (5 + emps), Type.Missing);
-
-
FindCells(cell, "0", (int)DayColor.Normal, (int)DayColor.Normal); // Search for Normal
-
FindCells(cell, "-9", (int)DayColor.NotHired, (int)DayColor.NotHired); // Search for Not Hired
-
-
if (showHoliday) // Search for Holidays
-
{
-
FindCells(cell, "-8", (int)DayColor.Holiday, (int)DayColor.Holiday);
-
}
-
if (showAWOL) // Search for AWOL
-
{
-
FindCells(cell, "-7", (int)DayColor.AWOL, (int)DayColor.AWOL);
-
}
-
if (showSL) // Search for SL
-
{
-
FindCells(cell, "-6", (int)DayColor.SL, (int)DayColor.SL);
-
}
-
if (showLeaves) // Search for Leaves
-
{
-
FindCells(cell, "-5", (int)DayColor.Leaves, (int)DayColor.Leaves);
-
}
-
if (showSubsti) // Search for Substi
-
{
-
FindCells(cell, "-4", (int)DayColor.Substi, (int)DayColor.Substi);
-
}
-
if (showLateEarly) // Search for Late Early
-
{
-
FindCells(cell, "-3", (int)DayColor.LateEarly, (int)DayColor.LateEarly);
-
}
-
if (showOTpast10) // Search for OT Past 10
-
{
-
FindCells(cell, "-2", (int)DayColor.OTpast10, (int)DayColor.OTpast10);
-
}
-
if (showOTbefore10) // Search for OT Before 10
-
{
-
FindCells(cell, "-1", (int)DayColor.OTbefore10, (int)DayColor.OTbefore10);
-
}
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C5:AG" + (5 + emps), Type.Missing);
-
cell.Value2 = "";
-
}
-
-
// Write Footer
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (7 + emps), Type.Missing);
-
cell.Font.Bold = true;
-
cell.Value2 = "Legend:";
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (8 + emps) + ":D" + (15 + emps), Type.Missing);
-
cell.Style = "borderStyle";
-
-
if (showOTbefore10)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (8 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (8 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 33;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (8 + emps), Type.Missing);
-
cell.Value2 = "Overtime (end before 10pm)";
-
-
if (showOTpast10)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (9 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (9 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 5;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (9 + emps), Type.Missing);
-
cell.Value2 = "Overtime (end past 10pm)";
-
-
if (showLateEarly)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (10 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (10 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 38;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (10 + emps), Type.Missing);
-
cell.Value2 = "Late time-in / Early time-out";
-
-
if (showSubsti)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (11 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (11 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 44;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (11 + emps), Type.Missing);
-
cell.Value2 = "Substi";
-
-
if (showLeaves)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (12 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (12 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 10;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (12 + emps), Type.Missing);
-
cell.Value2 = "VL, MVL, BL, PL";
-
-
if (showSL)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (13 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (13 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 13;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (13 + emps), Type.Missing);
-
cell.Value2 = "SL";
-
-
if (showAWOL)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (14 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (14 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 3;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (14 + emps), Type.Missing);
-
cell.Value2 = "AWOL";
-
-
if (showHoliday)
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("C" + (15 + emps), Type.Missing);
-
cell.HorizontalAlignment = Excel.Constants.xlCenter;
-
cell.Value2 = "X";
-
}
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("D" + (15 + emps), Type.Missing);
-
cell.Interior.ColorIndex = 6;
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
cell = xlWS.get_Range("E" + (15 + emps), Type.Missing);
-
cell.Value2 = "Holiday";
-
-
// Close and Save file
-
filename = Request.PhysicalApplicationPath + "Templates\\" + Session["EmployeeID"].ToString() +
-
" - BEV Summary " + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
-
xlWB.Close(true, filename, false);
-
xlApp.Quit();
-
}
-
finally
-
{
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(style);
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWS);
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
-
style = null;
-
cell = null;
-
xlWS = null;
-
xlWB = null;
-
xlApp = null;
-
GC.Collect();
-
}
-
-
DownloadFile(filename, true);
-
}
-
As you can see, I think I've managed to release every Excel Object I could find! Believe me, I tried to add the codes bit by bit to find out the problem. But I just dont understand anymore..
Any help would be appreciated! Thanks!
PS:
And oh, for more information, I am using C#, Visuall Studio 2005, ASP.Net and Excel 2000 (which is Office 9.0, I think).
|  | Moderator | | Join Date: Jan 2008 Location: Winchester, UK
Posts: 930
| | | re: Fresh Eyes Needed: Help me find the reason why Excel process is not killed properly..
Hi treiz,
Welcome to Bytes.com! I hope you find the site useful.
You've posted your question in the ASP Forum which is for Classic ASP only - I've moved it for you but in future please post all ASP.NET questions in the .NET Forum.
Also, please could you wrap your code in CODE tags using the # button in the edit window - it makes it much easier for everybody to read your posts.
With regard to your second problem: the default customErrors mode is RemoteOnly which means that you won't see the true cause of any errors if you are not browsing the site from a local machine. Go to your web.config file and change the customErrors mode as below. -
-
<system.web>
-
<customErrors mode="Off" />
-
</system.web>
-
Then you will be able to view the real cause of the error on remote machines.
Thanks and good luck finding a solution to your other problem.
Dr B
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|