关于网友提出的“excel求和怎么操作 关于C#操作Excel 资源释放”问题疑问,本网通过在网上对“excel求和怎么操作 关于C#操作Excel 资源释放”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:excel求和怎么操作 关于C#操作Excel 资源释放描述:
看一下代码 为什么就是释放不了。。。。。。。。。
Excel.Application XApp = new Excel.ApplicationClass();
//XApp.Visible = true;
Excel.Workbook xBook = XApp.Workbooks._Open(@"D:\Sample.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
Excel.Range oRange = ((Excel.Range)xSheet.UsedRange);
for (int i = 1; i < oRange.Cells.Rows.Count; i++)
{
for (int j = 1; j < oRange.Cells.Columns.Count; j++)
{
Excel.Range range = (Excel.Range)oRange[i, j];
if (range.Value2 != null)
{
string strValue = range.Value2.ToString();
Regex reg = new Regex("\\[.+\\]");
Match match = reg.Match(strValue);
if (match.Success)
{
// object models = model.GetType().Name; ;
//mode
// models.ToString();
string str = match.Value.Trim('[', ']');
PropertyInfo propertyinfo = typeof(CorprationInfo).GetProperty(str);
if (propertyinfo != null)
{
PropertyMap propertyMap = new PropertyMap(propertyinfo, new System.Drawing.Point(i, j));
List.Add(propertyMap);
}
else
{
Console.WriteLine("字段不存在:{0}",str);
}
}
}
// Console.WriteLine(range.Value2.ToString());
}
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSheet);
xSheet = null;
xBook.Close(false,Missing.Value,Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBook);
xBook = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(oRange);
oRange = null;
XApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(XApp);
XApp = null;
System.GC.Collect(0);