C# 读取Excel数据问题

ghxwzh 2009-09-02 04:39:53
每次读完好像进程中的Excel都不消失,
二次总是提示打开该Excel文件(---用Office软件打开--)


请问这是什么问题

是我引用的组建问题么?
还是其他的问题

如果可以的话请大家推荐一个成熟的方法!!
谢谢

winform 和 web
...全文
234 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengzulin 2009-11-12
  • 打赏
  • 举报
回复
一定是你有些对象没有关闭.如果的一些对象没有关闭,excel.Quit()是会失败的.
psy0324 2009-11-12
  • 打赏
  • 举报
回复
对EXcel的操作后必须Kill该Excel.exe进程。
lzsh0622 2009-11-12
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 ghxwzh 的回复:]
问题没有解决哦!!!
[/Quote]

这100分给我留着呢,呵呵


private void button2_Click(object sender, EventArgs e)
{
RunExcelApp();
GC.Collect();
}

private void RunExcelApp() // Excel的有关代码都写个方法中
{
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = true;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
excel.Quit();
  // GC.Collect() // 写这无效.
}


原因:本辖区内GC.Collect()无效
dlcustom 2009-11-12
  • 打赏
  • 举报
回复
那就帮你顶一下
[Quote=引用 23 楼 ghxwzh 的回复:]
问题没有解决哦!!!
[/Quote]
ghxwzh 2009-11-12
  • 打赏
  • 举报
回复
问题没有解决哦!!!
24K純帥 2009-09-04
  • 打赏
  • 举报
回复
学习。。
aloneone 2009-09-03
  • 打赏
  • 举报
回复
定义:
类 友情支持{
公共 无 顶(){
循环(真){
发贴一次;
}
}
}

友情支持.顶();

a854468521 2009-09-03
  • 打赏
  • 举报
回复
1:使用第三方的组件:Aspose.Cells 来读取EXCEL文件速度超快。
2:如果使用EXCEL.APPLICATION 方式读取文件,可以在开始加上时间标识,在使用 Quit()方法无法结束该进程时KILL该进程可以避免误杀其他EXCEL进程。
十八道胡同 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 x0070704 的回复:]
using  System.Runtime.InteropServices;

[DllImport("User32.dll",  CharSet  =  CharSet.Auto)]
public  static  extern  int  GetWindowThreadProcessId(IntPtr  hwnd,  out  int  ID);
protected  void  Button1_Click(object  sender,  EventArgs  e)
{
      Excel.ApplicationClass  excel  =  new  Microsoft.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);
      IntPtr  t  =  new  IntPtr(excel.Hwnd);
      int  k  =  0;
    GetWindowThreadProcessId(t,  out  k);
      System.Diagnostics.Process  p  =  System.Diagnostics.Process.GetProcessById(k);
      p.Kill();
  }

我这暑假是用这串代码解决那个进程的,这代码只会删除你用的打开的那个excel进程,其它excel进程不受影响,这是winform的

[/Quote]
呵呵,暴力杀死进程,不推荐这么做
十八道胡同 2009-09-03
  • 打赏
  • 举报
回复
我11楼的代码可以终止excel进程的
x0070704 2009-09-03
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
protected void Button1_Click(object sender, EventArgs e)
{
Excel.ApplicationClass excel = new Microsoft.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);
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}

我这暑假是用这串代码解决那个进程的,这代码只会删除你用的打开的那个excel进程,其它excel进程不受影响,这是winform的
tjpu_hxy 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lcl_data 的回复:]
参考我以前写的
C# code wbook.Save();
wbook.Close(true,null,null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wsheet=null;
wbook=null;
app=null;
GC.Collect();
http://blog.csdn.net/LCL_data/archive/2009/05/06/4154784.aspx
[/Quote]
对于这个问题,各种方法我都尝试过了。
11楼的做法,在打开EXCEL,不做任何处理时,再调用Quit方法是可行的。
如果进行了读写操作,按时上面的做法EXCEL进程还是会有的,应该是Office.Excel.dll的一个bug
当然解决方法还是有的,可以使用终止进程的办法来终止EXCEL进程(很*很暴力-.-)
济南大飞哥 2009-09-03
  • 打赏
  • 举报
回复
mark
ghxwzh 2009-09-03
  • 打赏
  • 举报
回复
进程中 EXcEL.exe还是有
ghxwzh 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 freeboy827 的回复:]
想省事就下载个第三控件
[/Quote]

请推荐一个,谢谢
freeboy827 2009-09-02
  • 打赏
  • 举报
回复
想省事就下载个第三控件
十八道胡同 2009-09-02
  • 打赏
  • 举报
回复
参考我以前写的
 wbook.Save();
wbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wsheet = null;
wbook = null;
app = null;
GC.Collect();

http://blog.csdn.net/LCL_data/archive/2009/05/06/4154784.aspx
十八道胡同 2009-09-02
  • 打赏
  • 举报
回复
是你的excel进程还在用着你的数据,
ghxwzh 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 c416452311 的回复:]
最后 要调用  Quit()
[/Quote]

我用的
godion 2009-09-02
  • 打赏
  • 举报
回复
最后 要调用 Quit()
加载更多回复(6)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧