Click here to Skip to main content
15,897,187 members
Home / Discussions / C#
   

C#

 
AnswerRe:Disabling maximising form by doubleclicking on winform header Pin
MSilviu1-Aug-07 2:37
MSilviu1-Aug-07 2:37 
QuestionTreeView expand Pin
collapo1-Aug-07 0:22
collapo1-Aug-07 0:22 
AnswerRe: TreeView expand Pin
Robert Rohde1-Aug-07 0:48
Robert Rohde1-Aug-07 0:48 
GeneralRe: TreeView expand Pin
collapo1-Aug-07 1:16
collapo1-Aug-07 1:16 
QuestionConnectionString Pin
M Riaz Bashir1-Aug-07 0:05
M Riaz Bashir1-Aug-07 0:05 
AnswerRe: ConnectionString Pin
il_masacratore1-Aug-07 0:14
il_masacratore1-Aug-07 0:14 
GeneralRe: ConnectionString Pin
M Riaz Bashir1-Aug-07 0:23
M Riaz Bashir1-Aug-07 0:23 
Questionmemory problem with releasing excel object Pin
S Palip31-Jul-07 23:45
S Palip31-Jul-07 23:45 
Hi,

The following is the code i used to create, write to and save an excel file. Everything goes fine except there is a delay in releasing the object. Yes it persists till the application is closed. Sometimes i'm able still see the excel.exe in the task manager even after the application is closed. Is there any way to overcome this.

I've added a reference to excel 11.0 com component.


private void CreateExcel()
{
Excel.Application ExcelApp = null;
Excel._Workbook ExcelWorkbook = null;
Excel._Worksheet ExcelWorkSheet = null;
string strSavePath = "";
try
{
ExcelApp = new Excel.Application();
ExcelApp.Visible = false;
ExcelWorkbook = (Excel._Workbook)(ExcelApp.Workbooks.Add(Type.Missing));

ExcelWorkSheet = (Excel._Worksheet)ExcelWorkbook.Sheets["Sheet1"];
// ExcelWorkSheet.Name = "My Sheet";


//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
ExcelApp.Visible = false;
ExcelApp.UserControl = false;

/*********************/
DataSet dsData = MyDataLayer.GetDataSet("Select top(3) from XXXX");

if (dsData != null && dsData.Tables.Count > 0)
{
// the writing process
ExcelWorkSheet.Cells[iRowIndex, iColumnIndex] = data;

}
/*********************/
//ExcelWorkSheet.get_Range("A3", "F3").HorizontalAlignment =
//Excel.XlHAlign.xlHAlignCenter;

//ExcelWorkSheet.get_Range("A1", "A1").Font.Bold = true;
ExcelWorkSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;
ExcelWorkSheet.Activate();
ExcelWorkSheet.get_Range("A3", "F3").EntireColumn.AutoFit();
//ExcelWorkSheet.get_Range("E1","E1").Columns.EntireColumn.NumberFormat = "£###,###,##0.00";


strSavePath = Application.ExecutablePath.ToString();
strSavePath = strSavePath.Substring(0, strSavePath.LastIndexOf("\\"));
strSavePath = strSavePath + "\\TestExcelFile.xls";


ExcelWorkbook.SaveAs(strSavePath, Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// ExcelWorkbook.Close(false, strSavePath, null);

// return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
if (ExcelWorkSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkSheet);
ExcelWorkSheet = null;
}

if (ExcelWorkbook != null)
{
ExcelWorkbook.Close(false, strSavePath, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkbook);
ExcelWorkbook = null;
}

if (ExcelApp != null)
{
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
ExcelApp = null;
}
}
finally
{
if (ExcelWorkSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkSheet);
ExcelWorkSheet = null;
}

if (ExcelWorkbook != null)
{

ExcelWorkbook.Close(false, strSavePath, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkbook);
ExcelWorkbook = null;
}

if (ExcelApp != null)
{
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
ExcelApp = null;
}


//GC.WaitForPendingFinalizers();
}

GC.Collect();
}

Thanks,

PLS Smile | :)
AnswerRe: memory problem with releasing excel object Pin
Robert Rohde1-Aug-07 0:52
Robert Rohde1-Aug-07 0:52 
QuestionFolderBrowserDialog Pin
Try31-Jul-07 23:21
Try31-Jul-07 23:21 
AnswerRe: FolderBrowserDialog Pin
Stefan Troschuetz1-Aug-07 1:26
Stefan Troschuetz1-Aug-07 1:26 
GeneralRe: FolderBrowserDialog Pin
Try1-Aug-07 1:29
Try1-Aug-07 1:29 
QuestionDial a number and play a voice (wav file) Pin
Deviser31-Jul-07 23:19
Deviser31-Jul-07 23:19 
AnswerRe: Dial a number and play a voice (wav file) Pin
Malcolm Smart1-Aug-07 2:48
Malcolm Smart1-Aug-07 2:48 
QuestionSorting columns in a DataGridView Pin
auloepid31-Jul-07 23:19
auloepid31-Jul-07 23:19 
QuestionSerialize WinForms Pin
SakthiSurya31-Jul-07 23:10
SakthiSurya31-Jul-07 23:10 
AnswerRe: Serialize WinForms [modified] Pin
Martin#1-Aug-07 0:03
Martin#1-Aug-07 0:03 
AnswerRe: Serialize WinForms Pin
Pete O'Hanlon1-Aug-07 1:16
mvePete O'Hanlon1-Aug-07 1:16 
QuestionScreensaver software development Pin
Identity Undisclosed31-Jul-07 22:18
Identity Undisclosed31-Jul-07 22:18 
AnswerRe: Screensaver software development Pin
MarkB77731-Jul-07 23:43
MarkB77731-Jul-07 23:43 
GeneralRe: Screensaver software development Pin
Identity Undisclosed1-Aug-07 0:07
Identity Undisclosed1-Aug-07 0:07 
QuestionDOWNLOAD ERROR - URGENT Pin
RDAAC31-Jul-07 22:18
RDAAC31-Jul-07 22:18 
QuestionCombo Box (read only) Pin
T.EDY31-Jul-07 22:07
T.EDY31-Jul-07 22:07 
AnswerRe: Combo Box (read only) Pin
il_masacratore31-Jul-07 22:36
il_masacratore31-Jul-07 22:36 
GeneralRe: Combo Box (read only) Pin
T.EDY31-Jul-07 22:39
T.EDY31-Jul-07 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.