PRB: Office Application Does Not Quit After Automation from Visual Studio .NET Client http://support.microsoft.com/default.aspx?scid=kb;EN-US;317109
saucer(思归, MS .NET MVP) 其实像微软所说的(http://support.microsoft.com/default.aspx?scid=kb;EN-US;317109)并不能释放掉资源,我以前在Office 2000下试过,怎么都释放不了,就算是打开任务管理器强行关闭都不行(会弹出访问错误的报错信息),后来重新装了Office 2000就可以了,不过现在到了Office XP了,不知道又是什么问题?
as the article said, you have to call System.Runtime.InteropServices.Marshal.ReleaseComObject(OBJECT) to release the object
已经试过你说得方法,仍然存在问题! 还有另外一个问题,如何把Excel的所有提示屏蔽掉?
>>>已经试过你说得方法,仍然存在问题! what problem?>>>如何把Excel的所有提示屏蔽掉? set the application's DisplayAlerts property to false, it also has a AskToUpdateLinks property
>>what problem? 资源仍然无法释放!
>>set the application's DisplayAlerts property to false, it also has a AskToUpdateLinks property没有找到DisplayAlerts这个属性!
you calledSystem.Runtime.InteropServices.Marshal.ReleaseComObject(OBJECT)on all object references and set all references to null and called System.GC.Collect()? and still doesn't work? Excel.Application myApp = new Excel.Application(); myApp.Visible = false; myApp.UserControl = false;myWorkbook = (Excel._Workbook)(myApp.Workbooks.Add( Missing.Value )); mySheet = (Excel._Worksheet)myWorkbook.ActiveSheet; mySheet.Cells[1, 1] = "Test";myApp.DisplayAlerts = false;
do not just call System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);call it on all references, void NAR (Object o) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(o); } catch{}
o = null; }NAR(oMissing); NAR(sheets); NAR(worksheet); NAR(book); excel.Quit(); NAR(excel);
http://support.microsoft.com/default.aspx?scid=kb;EN-US;317109
还有另外一个问题,如何把Excel的所有提示屏蔽掉?
what problem?>>>如何把Excel的所有提示屏蔽掉?
set the application's DisplayAlerts property to false, it also has a AskToUpdateLinks property
资源仍然无法释放!
Excel.Application myApp = new Excel.Application();
myApp.Visible = false;
myApp.UserControl = false;myWorkbook = (Excel._Workbook)(myApp.Workbooks.Add( Missing.Value ));
mySheet = (Excel._Worksheet)myWorkbook.ActiveSheet;
mySheet.Cells[1, 1] = "Test";myApp.DisplayAlerts = false;
释放资源部分代码:
excel.Quit();
oMissing =null;
sheets =null;
worksheet =null;
book =null;
excel =null;
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
catch{}
GC.Collect();
//检查发现没一句都执行了,但仍然没有释放资源
又有新问题,昨天还好好的,今天在建立Excel对象时候就报错了
Excel._Application excel =new Excel.ApplicationClass();//执行到这里报错,但可以编译通过
报错信息:00000ffff内存不能为"read"
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch{}
o = null;
}NAR(oMissing);
NAR(sheets);
NAR(worksheet);
NAR(book);
excel.Quit();
NAR(excel);
Excel._Application excel =new Excel.ApplicationClass();
报错的现象,但是在另一个正在开发中的项目中又会报错。真他妈的见鬼了。
public void CreateExcel(DataView dv,string path,string title1)
{
Excel._Application excel;
try
{
excel =new Excel.ApplicationClass();
}
catch
{
Response.Write("服务器端Excel繁忙!请稍后!");
return;
}
object oMissing =System.Reflection.Missing.Value;
Excel.Workbook book =excel.Workbooks.Add(oMissing);
excel.UserControl =false;
excel.DisplayAlerts=false;
excel.AlertBeforeOverwriting=false;
int rowIndex =2;
int colIndex =0;
excel.Cells[1,1] =title1;
foreach(DataColumn dc in dv.Table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex] =dc.ColumnName;
((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =8.8;
} foreach(DataRow row in dv.Table.Rows)
{
rowIndex++;
colIndex =0;
foreach(DataColumn dc in dv.Table.Columns)
{
colIndex ++;
excel.Cells[rowIndex,colIndex] = row[dc.ColumnName].ToString();
((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =8.8;
}
} Excel.Sheets sheets =book.Worksheets;
Excel._Worksheet worksheet =(Excel._Worksheet)sheets.get_Item(1);
Excel.Range range;
range =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,14]);
range.MergeCells =true;//合并
range.HorizontalAlignment =Excel.XlHAlign.xlHAlignCenter;//居中
range.Font.Bold =true;
range.Font.Size =14;
Excel.Range range1;
range1 =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[rowIndex,colIndex]);
range1.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle =Excel.XlLineStyle.xlLineStyleNone;
range1.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle =Excel.XlLineStyle.xlLineStyleNone;
range1.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =Excel.XlLineStyle.xlContinuous;
range1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =Excel.XlLineStyle.xlContinuous;
range1.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =Excel.XlLineStyle.xlContinuous;
range1.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =Excel.XlLineStyle.xlContinuous;
range1.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range1.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle =Excel.XlLineStyle.xlContinuous; //拉宽表格
Excel.Range range2;
range2 =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,1]);
range2.ColumnWidth =25.8; //填充颜色
Excel.Range range3;
range3 =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[rowIndex,1]); range3.Font.Bold =true;
range3 =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[2,colIndex]);
range3.Font.Bold =true; book.Saved =true; worksheet.SaveAs(path,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
NAR(oMissing);
NAR(sheets);
NAR(worksheet);
NAR(book);
excel.Quit();
NAR(excel);
GC.Collect();
}
private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch{}
o=null;
}
public void CreateExcel(DataView dv,string path,string title1)
{
Excel.Application excel;
try
{
excel =new Excel.Application();
}
catch
{
Response.Write("服务器端Excel繁忙!请稍后!");
return;
} object oMissing =System.Reflection.Missing.Value;
excel.UserControl =true;
excel.DisplayAlerts=false;
excel.AlertBeforeOverwriting=false;
Excel._Workbook book=null;
book =excel.Workbooks.Add(oMissing);//如果将这句去掉,可以顺利释放资源
book.Close(false,null,null);
this.NAR(book);
excel.Quit();
this.NAR(excel); GC.Collect();
GC.WaitForPendingFinalizers();
}private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch{}
o=null;
}只要对workbook进行操作过,就不可以释放资源了,这是为什么?他妈的,这些小问题烦死了,ms的专家们快发表发表意见!
加上
this.NAR(oMissing);
也是同样的结果(也就是说所有可以释放的东西都释放了),可是excel仍然在进程中