[C#]调用Workbook.Save与Interop的xlExcel8失败 [英] [C#] Calling Workbook.SaveAs with Interop fails for xlExcel8

查看:455
本文介绍了[C#]调用Workbook.Save与Interop的xlExcel8失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我发现在今天申请4/12 Office Office 2010 Microsoft Office更新后,以下代码失败,使用SaveAs函数抛出COMException(0x800A03EC)JUST对于xlExcel8。从Excel 2010中对VBA执行相同操作成功没有问题。
我知道xlExcel8和xlExcel9795可能没什么不同,但这是新更新的错误还是我应该做些什么来防止这种情况?我使用VS2008和.NET 3.5,MS Interop ver 14。

I have found that after applying today 4/12 Microsoft Office Update for Office 2010, the following code fails, with SaveAs function throwing COMException (0x800A03EC) JUST for xlExcel8. Doing the same with VBA from within Excel 2010 succeeds without problem. I know xlExcel8 and xlExcel9795 probably isn't much different, but is this a bug with the new update or was there something I was supposed to do to prevent this? I am using VS2008 with .NET 3.5, with MS Interop ver 14.


using System;
using System.Reflection;
using System.Runtime.InteropServices;

class Program {
	static void Main(string[] args) {
		Microsoft.Office.Interop.Excel.Application xlApp = null;
		Microsoft.Office.Interop.Excel.Workbooks xlBooks = null;
		Microsoft.Office.Interop.Excel.Workbook xlBook = null;
		Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;
		Microsoft.Office.Interop.Excel.Range xlCell = null;

		Microsoft.Office.Interop.Excel.XlFileFormat xlFileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7;

		if (args.Length == 1) {
			switch (args[0]) {
			case "9795":
				xlFileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795;
				break;
			case "4":
				xlFileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4;
				break;
			case "5":
				xlFileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel5;
				break;
			case "7":
				xlFileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7;
				break;
			case "8":
				xlFileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8;
				break;
			default:
				Console.WriteLine("Unsupported!");
				return;
			}

			try {
				xlApp = new Microsoft.Office.Interop.Excel.Application();
				xlBooks = xlApp.Workbooks;
				xlBook = xlBooks.Add(Missing.Value);
				xlSheet = (Microsoft.Office.Interop.Excel.Worksheet) xlBook.ActiveSheet;
				xlCell = (Microsoft.Office.Interop.Excel.Range) xlSheet.Cells[1, 2];
				xlCell.Value2 = "Dummy Text";
				xlBook.SaveAs(@"D:\test.xls",
							 xlFileFormat,
							 Type.Missing,
							 Type.Missing,
							 Type.Missing,
							 Type.Missing,
							 Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
							 Type.Missing,
							 Type.Missing,
							 Type.Missing,
							 Type.Missing,
							 Type.Missing);

				Console.WriteLine("Success!");
			}
			catch {
				Console.WriteLine("Failed!");
			}
			finally {
				if (xlCell != null)
					Marshal.ReleaseComObject(xlCell);
				if (xlSheet != null)
					Marshal.ReleaseComObject(xlSheet);

				xlBook.Close(false, Type.Missing, Type.Missing);
				if (xlBook != null)
					Marshal.ReleaseComObject(xlBook);
				if (xlBooks != null)
					Marshal.ReleaseComObject(xlBooks);

				xlApp.Quit();
				if (xlApp != null)
					Marshal.ReleaseComObject(xlApp);
			}
		}
	}
}

推荐答案

Bumping thread up。

Bumping thread up.

我以为很多人可能会遇到同样的问题,但这似乎很少见?我已经看到PPT的一些问题与最新的更新,但它可能源于相同的问题?

I thought many people might be experiencing same problem, but this seems to be rare? I've seen some issues with PPT with the latest update, but could it possibly stem from the same problems?

Windows更新打破powerpoint 2007-2003兼容文件 - Microsoft Answers

windows update breaks powerpoint 2007-2003 compatible files - Microsoft Answers

 

 


这篇关于[C#]调用Workbook.Save与Interop的xlExcel8失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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