Jedhi Posted June 2, 2005 Posted June 2, 2005 I am making a test that by every 4 hours report an excelfile. While running the test I want to be able to look in the present and previos excel file. But somehow it would not let me do it, it is like the previous file is not released. Could anyone tell me what I am doing wrong ? private Excel.Worksheet ws; private ExcelClass logfile; ws = logfile.Activate(); // 4 hours has gone save file and make a new if (timesp.Hours > 4) { logfile.Save(); logfile.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(ws); ws = null; StartNewReport(); // Waiting for a new worksheet is generated. while (ws == null){}; } } ----------------------------------------------------- ExcelClass() { Excel.Workbook wb; Excel.Application _excel; public Worksheet Activate() { { string Filename = DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00")+ "-" + DateTime.Now.Hour.ToString("00") + "_" + DateTime.Now.Minute.ToString("00")".XLS"; // Create a new workbook if (_excel == null) { _excel = new Application(); } wb = _excel.Workbooks.Add(Type.Missing); wb.SaveAs(Filename,Excel.XlFileFormat.xlExcel9795,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,Type.Missing,Type.Missing,Type.Missing); Excel.Worksheet ws =(Worksheet) _excel.ActiveSheet; return ws; } catch(Exception ex) { Console.WriteLine(ex.ToString()); } return null; } public void Save() { wb.Save(); } public void Close() { // Need all following code to clean up and extingush all references!!! wb.Close(null,null,null); _excel.Workbooks.Close(); _excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(_excel); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); wb = null; _excel = null; GC.Collect(); } Quote
Recommended Posts