excel求和怎么操作 关于C#操作Excel 资源释放

来源:互联网  时间:2016/8/18 9:43:23

关于网友提出的“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);

上一篇关于防止自动投票的想法问问大家可行吗?
下一篇ajax的dropdown四级连动的问题
明星图片
相关文章
《excel求和怎么操作 关于C#操作Excel 资源释放》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)