Hi. I'm able to create and save an excel file, no problems. But i cannot seem to close the instance of Excel. I've tried lots of websites, and the Microsoft site with no luck. Maybe someone here can help me with the following problems. Below is my code (basically).
1 Excel.Application objApp = null;
2 Excel._Workbook objBook = null;
3 Excel.Workbooks objBooks = null;
4 Excel.Sheets objSheets = null;
5 Excel._Worksheet oSheet = null;
6 Excel.Range objRange = null;
7
8 try
9 {
10 objApp = new Excel.Application();
11 objBooks = objApp.Workbooks;
12 objBook = objBooks.Add(Missing.Value);
13 objSheets = objBook.Worksheets;
14 oSheet = (Excel._Worksheet)objSheets.get_Item(1);
15
16 objApp.Visible = true;
17
18 int row = 2;
19 int column = 1;
20
21 //Column Titles
22 oSheet.Cells[1, column] = "Something";
23
24 //Data
25 ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["Something"];
26 string connectionString = settings.ConnectionString;
27 using (SqlConnection connection = new SqlConnection(connectionString))
28 {
29 String queryString = "SELECT QUERY HERE";
30 SqlCommand command = new SqlCommand(queryString, connection);
31 connection.Open();
32 SqlDataReader reader = command.ExecuteReader();
33 try
34 {
35 while (reader.Read())
36 {
37 for (column = 1; column < reader.FieldCount; column++)
38 {
39 oSheet.Cells[row, column] = reader[column - 1].ToString();
40 }
41 row++;
42 }
43 }
44 finally
45 {
46 reader.Close();
47 connection.Close();
48 }
49 }
50
51 objRange = (Excel.Range)oSheet.Cells[1, 1];
52 objRange = objRange.EntireRow;
53 objRange.Font.Bold = true;
54
55 //Keep Excel Hidden
56 oXL.Visible = false;
57 oXL.UserControl = false;
58
59 objBook.SaveAs(strCurrentDir + strFile, Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false,
60 Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);
61 }
62 catch (Exception theException)
63 {
64 String errorMessage;
65 errorMessage = "Error: ";
66 errorMessage = String.Concat(errorMessage, theException.Message);
67 errorMessage = String.Concat(errorMessage, " Line: ");
68 errorMessage = String.Concat(errorMessage, theException.Source);
69 }
70 finally
71 {
72 if (objRange != null)
73 {
74 System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);
75 objRange = null;
76 }
77 if (oSheet != null)
78 {
79 System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
80 oSheet = null;
81 }
82 if (objSheets != null)
83 {
84 System.Runtime.InteropServices.Marshal.ReleaseComObject(objSheets);
85 objSheets = null;
86 }
87 if (objBook !=null)
88 {
89 objBook.Close(null, null, null);
90 System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook);
91 objBook = null;
92 }
93 if (objBooks !=null)
94 {
95 objBooks.Close();
96 System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks);
97 objBooks = null;
98 }
99 if (objApp != null)
100 {
101 objApp.Quit();
102 System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
103 objApp = null;
104 }
105 GC.Collect(); // force final cleanup!/
106 GC.WaitForPendingFinalizers();
107 GC.Collect();
108 Response.Redirect("./Reports/" + strFile);
109 }
If i remove everything except creating the objects and closing them, it works fine. oSheet.Cells will still allow it to close. Using the range won't let Excel close (Only declaring it or declaring and using it). Using sqldatareadere won't let Excel Close, Using ConnectionStringSettings settings won't let it close. In short, almost anything i do in between causes Excel to stay open in the processes. Any ideas?