在程序中关闭Excel [英] Closing excel within program

查看:64
本文介绍了在程序中关闭Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Windows的应用程序,可以打开excel,加载文件,根据需要进行更改,保存并退出.

我以为我覆盖了以下内容:

Hi, I have a windows based application that opens excel,loads a file, makes changes as needed, saves it and quits.

I thought I had this covered with:

public void connectionClose()
{
xlApp.Save();
xlApp.Quit();

releaseObject(xlApp);
releaseObject(xlworkBook);
}



但是,在尝试打开文件后,我一直在进行更改以确保其按预期方式运行,但我收到一条错误消息,指出仅在使用中才能看到只读文件.打开任务管理器,然后进程中运行10-15 excel.exe.现在我以为我的解决方案会关闭它们,但显然不会.是否有办法确保我保存并退出时确实摆脱了excel的困扰,所以我不希望它占用内存,而且我还希望能够检查自己已完成的操作!

Cheers



however after trying to open the file I had been altering to make sure it was doing as expected, I got an error saying can only see read only as its in use. Opened up task manager and there was 10-15 excel.exe running in the processes. Now I thought my solution would have closed them but apparently not. Is there away to ensure that when I save and quit it does actually get rid of excel, I don''t want it taking up memory and also I want to be able to check what I''v done!

Cheers

推荐答案

如果要在保存excel文件后退出应用程序.然后尝试以下操作:
this.Close();

如果要释放程序使用的任何资源(托管或非托管),请使用dispose函数.
If you want to quit the application after saving excel file. Then try this:
this.Close();

If you want to release any resources (managed or unmanaged) that were used by your program, then use the dispose function.
<br />
this.dispose();




我最近有这个问题.我找到了解决方案,方法是将许多其他论坛帖子放在一起,这些帖子我都记不起来了.

我最后的缝合解决方案将起作用,如下所示:

Hi,

I recently had this problem. I found a solution from fitting together a lot of other forum posts which I can''t remember off the top of my head to credit.

My final stitched together solution will work, and is as follows:

using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.IO;
namespace ExcelExample
{
	class CreateExcelDoc
	{	
		Microsoft.Office.Interop.Excel.Application oXL;
		_Workbook oWB;
		_Worksheet oSheet;
		Range oRng;
		
		[DllImport("user32.dll")]
		private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
		Hashtable myHashtable = new Hashtable();
		public CreateExcelDoc()
		{
			//Start Excel and get Application object.
			oXL = new Microsoft.Office.Interop.Excel.Application();	
			//Get a new workbook.
			oWB = (_Workbook)(oXL.Workbooks.Add( Type.Missing ));
			oSheet = (_Worksheet)oWB.ActiveSheet;
		}
		
		public void SaveDocument()
		{
			try
			{
			oWB.SaveAs(@"Filename", XlFileFormat.xlWorkbookNormal,
			Type.Missing, Type.Missing,
			false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
			Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
			oWB.Close(null, null, null);
			oXL.Quit(); 			
			
			KillApplicationProcess(oXL);
			}
			catch( Exception theException ) 
			{
			    String errorMessage;
			    errorMessage = "Error: ";
			    errorMessage = String.Concat( errorMessage, theException.Message );
			    errorMessage = String.Concat( errorMessage, " Line: " );
			    errorMessage = String.Concat( errorMessage, theException.Source );
			}
		}
		public static void KillApplicationProcess(Microsoft.Office.Interop.Excel.Application oXL)
		{
			int hWnd = oXL.Application.Hwnd;
			uint processID;
			GetWindowThreadProcessId((IntPtr)hWnd, out processID);			
			Process.GetProcessById((int)processID).Kill();
		}
		
		public void AddData(int row, int col, string data)
		{
			oSheet.Cells[row, col] = data;		
		}
	}
}



希望对您有所帮助:)



Hope that helps :)


您可以始终使用Process.Close或使该Process.Kill失败.问题在于您将必须确保关闭正在打开的Excel进程,而不是一个用户在启动应用程序时碰巧已经打开的进程(或此后单独打开的进程).
但是,您有10-15个excel进程正在运行,这一事实表明问题出在其他地方.也许您是在没有意识到的情况下多次打开文件?我会非常仔细地检查您的代码,尤其是首先要创建xlApp和xlworkBook的代码,以确保您不会多次调用它.

另一个想法是,我没有以这种方式使用Excel,但是我确实使用过PowerPoint,工作簿上是否存在Close方法?您可能缺少xlworkBook.请先关闭xlApp.Quit.
You could always use Process.Close or failing that Process.Kill. The problem would be that you''d have to make sure you are closing an Excel process that you opened and not one that a user just happen to have open when they started your application (or have opened separately since).
However, the fact that you have 10-15 excel processes running suggests the problem is somewhere else. Perhaps you are opening the file multiple times without realizing it? I''d check your code very carefully, especially where you are creating xlApp and xlworkBook in the first place, to make sure you aren''t calling it multiple times.

Another thought, I haven''t used Excel this way, but I did play around with PowerPoint, is there a Close method on the workbook? You might be missing an xlworkBook.Close prior to your xlApp.Quit.


这篇关于在程序中关闭Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆