最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
.net中清除EXCEL进程最有效的方法
时间:2022-07-02 10:52:31 编辑:袖梨 来源:一聚教程网
最近用C#写winform,将EXCEL文件中的数据写入数据库中,将DataGrid中的数据导出为EXCEL格式。最后发现EXCEL内存泄漏,在应用程序不退出的情况下,总是有一个EXCEL进程不能清除!在网上找了许多答案,都是无用的答案!什么不管三七二十一杀EXCEL进程啦,不是最有效的方法!其实最有效的方法就是下面这个方法:
1、对excel操作做成一个函数,然后调用此函数。在函数中调用GC.Collect();无用,因为GC不回收调用自己的那一段代码块!
2、在函数的下面调用GC.Collect();语句。你会发现EXCEL进程没有了!
例如:
private void Import() {
.Application myExcel = new Excel.Application();
myExcel.Workbooks.Add(openFileDialog1.FileName);
//........
//读取EXCEL文件,导入到数据库.
//清除excel垃圾进程
myExcel.Workbooks.Close();
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myExcel = null;
}
private void ExcelImport() {
Import();
GC.Collect();
}
//以下按button1按钮,使用多线程读取EXCEL文件,导入到数据库.
private void button1_Click(object sender, System.EventArgs e) {
if(openFileDialog1.ShowDialog() == DialogResult.OK) {
System.Threading.Thread t=new System.Threading.Thread(new System.Threading.ThreadStart(ExcelImport));
t.Start();
}
}
1、对excel操作做成一个函数,然后调用此函数。在函数中调用GC.Collect();无用,因为GC不回收调用自己的那一段代码块!
2、在函数的下面调用GC.Collect();语句。你会发现EXCEL进程没有了!
例如:
private void Import() {
.Application myExcel = new Excel.Application();
myExcel.Workbooks.Add(openFileDialog1.FileName);
//........
//读取EXCEL文件,导入到数据库.
//清除excel垃圾进程
myExcel.Workbooks.Close();
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myExcel = null;
}
private void ExcelImport() {
Import();
GC.Collect();
}
//以下按button1按钮,使用多线程读取EXCEL文件,导入到数据库.
private void button1_Click(object sender, System.EventArgs e) {
if(openFileDialog1.ShowDialog() == DialogResult.OK) {
System.Threading.Thread t=new System.Threading.Thread(new System.Threading.ThreadStart(ExcelImport));
t.Start();
}
}
相关文章
- 污污漫画入口风险警示-合规访问指南与安全指引 12-13
- 海棠文学城网页版官网入口-2025在线网址直达 12-13
- 土豪漫画官方APP免费下载入口-正版无广告畅读保障 12-13
- 歪歪漫画app最新版下载入口-首页畅读无广告直接进 12-13
- SkrBT磁力官网最新入口-skrbt磁力引擎极速版入口 12-13
- 微博热搜网页版极速入口-微博热搜榜官方直达链接 12-13