Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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();

}

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...