Hi, I have problem to terminate the process EXCEL.EXE after I write into it. After the program finish execute. The EXCEL.EXE is still in the proceses list in the task manager. Anyway to terminate it?
Below is my coding:
SaveFileDialog s_Lvl_Max = new SaveFileDialog();
s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
s_Lvl_Max.FilterIndex = 1;
s_Lvl_Max.FileName = "FileKK" + "_" + comboBox_baseTP.Text;
s_Lvl_Max.OverwritePrompt = true;
if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.Worksheets[1];
//Add table headers going cell by cell.
oSheet.Cells[2, 1] = "Level (Max VCC) Guardband";
oSheet.Cells[4, 1] = "Test Name";
oSheet.Cells[4, 2] = "Frequency";
oSheet.Cells[4, 3] = "[Spec]";
oSheet.Cells[4, 4] = "Actual";
oSheet.Cells[4, 5] = "temp(V)";
//Format A1:D1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "E1").Font.Bold = true;
oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
int i = 5;
foreach (DataRow dr in dtable2.Rows)
{
oSheet.Cells[i, 1] = dr[0];
oSheet.Cells[i, 2] = dr[1];
oSheet.Cells[i, 3] = dr[2];
oSheet.Cells[i, 4] = dr[3];
oSheet.Cells[i, 5] = dr[4];
i++;
}
i--;
//AutoFit columns A:D.
oRng = oSheet.get_Range("A1", "E1");
oRng.EntireColumn.AutoFit();
oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oXL.Quit(); // <--------- try to use this to terminate the process, not working
MessageBox.Show("File has been successfully saved. :)");
}