问题描述
利用C#向excel文件中预先写入部分数据,然后,利用webBrower打开excel文件,添加数据,但保存总是有问题,主要是Excel进程无法在前一次彻底关闭,困扰了很久,求助大神帮助!
解决方案
解决方案二:
无法在前一次彻底关闭-》什么叫无法彻底关闭?写入保存后,在线程中彻底的Killexcel的进程。
解决方案三:
usingSystem.Runtime.InteropServices;[DllImport("User32.dll",CharSet=CharSet.Auto)]publicstaticexternintGetWindowThreadProcessId(IntPtrhwnd,outintID);protectedvoidButton1_Click(objectsender,EventArgse){Excel.ApplicationClassexcel=newMicrosoft.Office.Interop.Excel.ApplicationClass();excel.Workbooks.Open("d:aaa.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);IntPtrt=newIntPtr(excel.Hwnd);intk=0;GetWindowThreadProcessId(t,outk);System.Diagnostics.Processp=System.Diagnostics.Process.GetProcessById(k);p.Kill();}
解决方案四:
用npoi代替vba
解决方案五:
引用2楼道玄希言的回复:
usingSystem.Runtime.InteropServices;[DllImport("User32.dll",CharSet=CharSet.Auto)]publicstaticexternintGetWindowThreadProcessId(IntPtrhwnd,outintID);protectedvoidButton1_Click(objectsender,EventArgse){Excel.ApplicationClassexcel=newMicrosoft.Office.Interop.Excel.ApplicationClass();excel.Workbooks.Open("d:aaa.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);IntPtrt=newIntPtr(excel.Hwnd);intk=0;GetWindowThreadProcessId(t,outk);System.Diagnostics.Processp=System.Diagnostics.Process.GetProcessById(k);p.Kill();}
pia使用的非托管对象需要释放
解决方案六:
一般情况下,excel.book都需要在close之前save一下,然后将book和app都设置为null我操作excel和word基本都不会有进程残留啊