前几天做了一个程序,打开EXCEL并保存数据库中,发现Excel进程关不了,后来采取了下面的方法,希望对大家有用:
using System.Runtime.InteropServices;
namespace ProduceDayReport
{
public class ExceptionReport : System.Windows.Forms.Form
{
..................
private const int WM_CLOSE=0x0010;
private const int WM_QUIT=0x0012;
private const int WM_DESTROY=0x0002;
.....................................
..............
delegate DataTable AsynchronousManipulateDelegage();
private AsynchronousManipulateDelegage amd; [DllImport("user32.dll",CharSet=CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd,int wMsg,IntPtr wParam,IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool MessageBeep(BeepType beepType);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);.....................//关键是下面的话 excel.DisplayAlerts =false;
PostMessage(excel.HWND,WM_CLOSE,new IntPtr(0),new IntPtr(0));
PostMessage(excel.HWND,WM_DESTROY,new IntPtr(0),new IntPtr(0)); ///////////
以上是我从程序内拷贝的一部分,希望大家知道该么做!
如果你用的不是officeXP,则在打开EXCEL后,做这样的工作:
excel.Workbooks.Open (.......);
String WndClassCaption="Microsoft Excel - "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1);
IntPtr hwnd_win = FindWindow("XLMAIN", WndClassCaption);
if(hwnd_win==IntPtr.Zero)
{
hwnd_win = FindWindow("MS-SDIa",xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
if(hwnd_win==IntPtr.Zero)
{
MessageBox.Show ("没找到窗口类 "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
return null;
}
}
...................
并作如下更改:
excel.DisplayAlerts =false;
PostMessage((IntPtr)hwnd_win,WM_CLOSE,new IntPtr(0),new IntPtr(0));
PostMessage((IntPtr)hwnd_win,WM_DESTROY,new IntPtr(0),new IntPtr(0));
using System.Runtime.InteropServices;
namespace ProduceDayReport
{
public class ExceptionReport : System.Windows.Forms.Form
{
..................
private const int WM_CLOSE=0x0010;
private const int WM_QUIT=0x0012;
private const int WM_DESTROY=0x0002;
.....................................
..............
delegate DataTable AsynchronousManipulateDelegage();
private AsynchronousManipulateDelegage amd; [DllImport("user32.dll",CharSet=CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd,int wMsg,IntPtr wParam,IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool MessageBeep(BeepType beepType);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);.....................//关键是下面的话 excel.DisplayAlerts =false;
PostMessage(excel.HWND,WM_CLOSE,new IntPtr(0),new IntPtr(0));
PostMessage(excel.HWND,WM_DESTROY,new IntPtr(0),new IntPtr(0)); ///////////
以上是我从程序内拷贝的一部分,希望大家知道该么做!
如果你用的不是officeXP,则在打开EXCEL后,做这样的工作:
excel.Workbooks.Open (.......);
String WndClassCaption="Microsoft Excel - "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1);
IntPtr hwnd_win = FindWindow("XLMAIN", WndClassCaption);
if(hwnd_win==IntPtr.Zero)
{
hwnd_win = FindWindow("MS-SDIa",xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
if(hwnd_win==IntPtr.Zero)
{
MessageBox.Show ("没找到窗口类 "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
return null;
}
}
...................
并作如下更改:
excel.DisplayAlerts =false;
PostMessage((IntPtr)hwnd_win,WM_CLOSE,new IntPtr(0),new IntPtr(0));
PostMessage((IntPtr)hwnd_win,WM_DESTROY,new IntPtr(0),new IntPtr(0));
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货