C# Excel 错误 HRESULT: 0x800A03EC 尝试使用单元格名称获取范围时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/891394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:40:22  来源:igfitidea点击:

Excel error HRESULT: 0x800A03EC while trying to get range with cell's name

c#excelexceptionvsto

提问by Teerasej

I am working with Window Service project. that have to write data to a sheet in Excel file in a sequence times.

我正在使用窗口服务项目。必须按顺序将数据写入 Excel 文件中的工作表。

But sometimes, just sometimes, the service throw out the exception "Exception from HRESULT: 0x800A03EC" while it's trying to get range with cell's name.

但有时,只是有时,该服务在尝试使用单元格名称获取范围时会抛出异常“来自 HRESULT 的异常:0x800A03EC”。

I have put the code of opening excel sheet, and getting cell here.

我把打开excel表和获取单元格的代码放在这里。

  • OS: window server 2003 Office:
  • Microsoft Office 2003 sp2
  • 操作系统:window server 2003 办公室:
  • 微软 Office 2003 sp2

1: Opening excel sheet

1:打开excel表

m_WorkBook = m_WorkBooks.Open(this.FilePath, 0, false, 5,
     "", "", true, Excels.XlPlatform.xlWindows, ";",
     true, false, 0, true, 0, 0);

2: Getting cell to write

2:让单元格写入

protected object m_MissingValue = System.Reflection.Missing.Value;
Range range = m_WorkSheet.get_Range(cell.CellName, m_MissingValue);
// error from this method, and cell name is string.

采纳答案by Dominic Zukiewicz

The error code 0x800A03EC(or -2146827284) means NAME_NOT_FOUND; in other words, you've asked for something, and Excel can't find it.

错误代码0x800A03EC(或 -2146827284)表示 NAME_NOT_FOUND;换句话说,您要求某事,而 Excel 找不到。

This is a generic code, which can apply to lots of things it can't find e.g. using properties which aren't valid at that time like PivotItem.SourceNameStandardthrows this when a PivotItem doesn't have a filter applied. Worksheets["BLAHBLAH"]throws this, when the sheet doesn't exist etc. In general, you are asking for something with a specific name and it doesn't exist. As for why, that will taking some digging on your part.

这是一个通用代码,它可以应用于很多它找不到的东西,例如使用当时无效的属性,例如在PivotItem.SourceNameStandardPivotItem 没有应用过滤器时抛出这个。Worksheets["BLAHBLAH"]抛出这个,当工作表不存在等时。一般来说,您要求具有特定名称的东西并且它不存在。至于为什么,这需要您进行一些挖掘。

Check your sheet definitely does have the Range you are asking for, or that the .CellNameis definitely giving back the name of the range you are asking for.

检查您的工作表确实有您要求的范围,或者.CellName肯定会返回您要求的范围的名称。

回答by Teerasej

I found a possible solution here: http://www.made4dotnet.com/Default.aspx?tabid=141&aid=15

我在这里找到了一个可能的解决方案:http: //www.made4dotnet.com/Default.aspx?tabid=141& aid=15

Edit:

编辑:

If you automate Microsoft Excel with Microsoft Visual Basic .NET, Microsoft Visual C# .NET, or Microsoft Visual C++, you may receive the following errors when calling certain methods because the machine has the locale set to something other than US English (locale ID or LCID 1033):

如果您使用 Microsoft Visual Basic .NET、Microsoft Visual C# .NET 或 Microsoft Visual C++ 自动化 Microsoft Excel,则在调用某些方法时您可能会收到以下错误,因为计算机的区域设置不是美国英语(区域 ID 或LCID 1033):

Exception from HRESULT: 0x800A03EC

HRESULT 异常:0x800A03EC

and/or

和/或

Old format or invalid type library

旧格式或无效类型库

SOLUTION 1:

解决方案1:



To get around this error you can set CurrentCulture to en-US when executing code related to Excel and reset back to your originale by using these 2 functions.

要解决此错误,您可以在执行与 Excel 相关的代码时将 CurrentCulture 设置为 en-US,并使用这 2 个函数重置回您的原始版本。

//declare a variable to hold the CurrentCulture
System.Globalization.CultureInfo oldCI;
//get the old CurrenCulture and set the new, en-US
void SetNewCurrentCulture()
{
  oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
  System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
}
//reset Current Culture back to the originale
void ResetCurrentCulture()
{
  System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
}

SOLUTION 2:

解决方案2:



Another solution that could work, create a 1033 directory under Microsoft Office\Office11 (or your corresponding office-version), copy excel.exe to the 1033 directory, and rename it to xllex.dll.

另一种可行的解决方案是,在Microsoft Office\Office11(或您对应的office-version)下创建一个1033目录,将excel.exe复制到1033目录中,并将其重命名为xllex.dll。

Although you might solve the problem using one off these solutions, when you call the Excel object model in locales other than US English, the Excel object model can act differently and your code can fail in ways you may not have thought of. For example, you might have code that sets the value of a range to a date:

尽管您可以使用这些解决方案中的一种来解决问题,但是当您在美国英语以外的语言环境中调用 Excel 对象模型时,Excel 对象模型的行为可能会有所不同,并且您的代码可能会以您可能没有想到的方式失败。例如,您可能有将范围值设置为日期的代码:

yourRange.Value2 = "10/10/09"

yourRange.Value2 = "10/10/09"

Depending on the locale this code can act differently resulting in Excel putting into the range any of the following values:

根据区域设置,此代码的行为可能有所不同,从而导致 Excel 放入以下任何值的范围:

October 10, 2009 September 10, 2009 October 9, 2010

2009年10月10日 2009年9月10日 2010年10月9日

回答by Jay

I have encountered this error code when enumerating names and calling worksheet.get_Range(name). It seems to occur when the name does NOT apply to a range, in my case it is the name of a macro.

我在枚举名称和调用 worksheet.get_Range(name) 时遇到了此错误代码。当名称不适用于范围时似乎会发生这种情况,在我的情况下它是宏的名称。

回答by Jeremy Thompson

I got the error with a space in a Sheet Name:

我在工作表名称中有空格时收到错误消息:

using (var range = _excelApp.Range["Sheet Name Had Space!$A"].WithComCleanup())

I fixed it by putting single quotes around Sheet Names with spaces:

我通过在带有空格的工作表名称周围加上单引号来修复它:

using (var range = _excelApp.Range["'Sheet Name Had Space'!$A"].WithComCleanup())

回答by Ehsan

If you can copy the whole exception it would be much more better, but once I faced with this Exception and this is because the function calling from your dll file which I guess is Aspose.dll hasn't been signed well. I think it would be the possible duplicate of this

如果您可以复制整个异常,那会好得多,但是一旦我遇到此异常,这是因为从您的 dll 文件调用的函数(我猜是 Aspose.dll)没有被很好地签名。我认为这是可能的重复

FYI, in order to find out if your dll hasn't been signed well you should right-click on that and go to the signiture and it will tell you if it has been electronically signed well or not.

仅供参考,为了确定您的 dll 是否未正确签名,您应该右键单击它并转到签名,它会告诉您它是否已通过电子签名。

回答by Jay

I had this problem when I was trying to use the range.AddComment() function. I was able to solve this by calling range.ClearComment() before adding the comment.

我在尝试使用 range.AddComment() 函数时遇到了这个问题。我能够通过在添加评论之前调用 range.ClearComment() 来解决这个问题。

回答by ebrts

I ran into this error because I was attempting to write a string to a cell which started with an "=".

我遇到了这个错误,因为我试图将一个字符串写入以“=”开头的单元格。

The solution was to put an "'" (apostrophe) before the equals sign, which is a way to tell excel that you're not, in fact, trying to write a formula, and just want to print the equals sign.

解决方案是在等号之前放置一个“'”(撇号),这是一种告诉 excel 您实际上不是在尝试编写公式,而只是想打印等号的方法。

回答by Makatun

I got this when I forgot to unprotect workbook or sheet.

当我忘记取消保护工作簿或工作表时,我得到了这个。

回答by Elmue

The meaning of the completely undocumentederror 800A03EC (shame on Microsoft!) is something like "OPERATION NOT SUPPORTED".

完全没有记录的错误 800A03EC(微软的耻辱!)的含义类似于“不支持操作”。

It may happen

它可能会发生

  • when you open a document that has a content created by a newer Excel version, which your current Excel version does not understand.
  • when you save a document to the same path where you have loaded it from (file is already open and locked)
  • 当您打开包含由较新 Excel 版本创建的内容的文档时,您当前的 Excel 版本无法识别该内容。
  • 当您将文档保存到加载它的同一路径时(文件已打开并锁定)

But mostly you will see this error due to severe bugsin Excel.

但大多数情况下,由于Excel 中的严重错误,您会看到此错误。

  • For example Microsoft.Office.Interop.Excel.Picture has a property "Enabled". When you call it you should receive a bool value. But instead you get an error 800A03EC. This is a bug.
  • And there is a very fat bug in Exel 2013 and 2016: When you automate an Excel process and set Application.Visible=trueand Application.WindowState = XlWindowState.xlMinimizedthen you will get hundreds of 800A03EC errors from different functions (like Range.Merge(), CheckBox.Text, Shape.TopLeftCell, Shape.Locked and many more). This bug does not exist in Excel 2007 and 2010.
  • 例如 Microsoft.Office.Interop.Excel.Picture 有一个属性“启用”。当您调用它时,您应该收到一个布尔值。但相反,您会收到错误 800A03EC。这是一个错误。
  • 并且在 Exel 2013 和 2016 中有一个非常大的错误:当你自动化一个 Excel 过程并设置时Application.Visible=trueApplication.WindowState = XlWindowState.xlMinimized你会从不同的函数(如 Range.Merge()、CheckBox.Text、Shape.TopLeftCell、Shape)中得到数百个 800A03EC 错误.Locked 等等)。Excel 2007 和 2010 中不存在此错误。

回答by aaron

Using Dominic's answer I found the answer to my problem specifically was an invalid DateTiime in the source data before it was applied to the range. Somewhere between the database, .NET and Excel the conversion of the date defaulted down to "1/1/1899 12:00:00 AM". I had to check it and convert it to an empty string and it fixed it for me.

使用 Dominic 的答案,我发现我的问题的答案特别是在将其应用于范围之前源数据中的无效 DateTiime。在数据库、.NET 和 Excel 之间的某个地方,日期的转换默认为“1/1/1899 12:00:00 AM”。我不得不检查它并将其转换为一个空字符串,它为我修复了它。

if (objectArray[row, col].ToString() == "1/1/1899 12:00:00 AM")
{
    objectArray[row, col] = string.Empty;
}

This is probably a pretty specific example but hopefully it will save somebody else some time if they are trying to track down a piece of invalid data.

这可能是一个非常具体的例子,但希望如果他们试图追踪一段无效数据,它会为其他人节省一些时间。