C#中的Excel互操作:从HRESULT异常(DISP_E_BADINDEX) [英] C# Excel interop: Exception from HRESULT (DISP_E_BADINDEX)

查看:853
本文介绍了C#中的Excel互操作:从HRESULT异常(DISP_E_BADINDEX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图部署在我的开发PC和其他一些工作站工作正常的应用程序。
然而,一些用户收到我似乎无法掌握一个错误。



程序与功能Excel.Interop一个C#应用程序的dotNet(Office 2003中)。



我似乎得到一个问题索引。奇怪的是,这部分完美的作品在某些机器上,但引发他人致命异常...
所有的机器都是Windows 7与Office 2003。



这是相关代码:

  //改变表代码(指数为1,2,3) - >在#2 
公共无效ChangeWorksheet(INT sheetIndex)
{
错误,如果(_OnXLSEvent!= NULL)_OnXLSEvent(的String.Format(TEMP:网页上正常运行{0},sheetIndex) );
_WS = _WSs [sheetIndex]
_Shapes = _WS.Shapes;
_PageSetup = _WS.PageSetup;
如果(_OnXLSEvent!= NULL)_OnXLSEvent(的String.Format(TEMP:网页上正常运行{0},_WS.Name));
}

//构造函数(_App和_WBs是静态的)
公共ExcelProcessor(布尔SaveAutomatically =假,字符串SavePath =)
{
如果(_App == NULL)
_App =新XLS.Application();
如果(_WBs == NULL)
_WBs = _App.Workbooks;
_WB = _WBs.Add();
_WSs = _WB.Sheets;
_WS = _WSs [1];
_Shapes = _WS.Shapes;
_PageSetup = _WS.PageSetup;
_SavePath = SavePath;
_SaveOnDispose = SaveAutomatically;
_App.DisplayAlerts = FALSE;
ApplyPageSetup();
}

这是我收到的日志:

  ...无关
8:52:TEMP:TEMP:第1页
8:52工作页面上工作表Sheet1
8:52:TEMP:工作第2页
8:52:出现错误:
无效索引。 (从HRESULT异常:0x8002000B(DISP_E_BADINDEX))
在Microsoft.Office.Interop.Excel.Sheets.get__Default(对象索引)
在Classes.XLSInterop.ExcelProcessor.ChangeWorksheet(的Int32 sheetIndex)在C:\\ \\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\XLSInterop\ExcelProcessor.cs:在Classes.ApplicationManager.Manager线74
。 ProcessSingleDocument(InFileDocument DOC)在C:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\ApplicationManager\ApplicationManager.cs:行327
在Classes.ApplicationManager.Manager.ConvertFile(字符串文件)在C:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\ApplicationManager\\ \\ApplicationManager.cs:行172


解决方案

我说话太快!
这仅仅是一个非常愚蠢的错误。我想我给的解决方案,以便其他人可能不会陷入像我一样同样的陷阱; - )



要进一步分析这个问题,我添加以下代码到构造器:

 列表< XLS.Worksheet>张=新名单< XLS.Worksheet>()
的foreach(在_WSs XLS.Worksheet SH)
{
sheets.Add(SH);
}
如果(_OnXLSEvent!= NULL)_OnXLSEvent(的String.Format(\\\
\tSheets在WB:{0} \\\
\tFirst板指数:{1},\ n\tLast板指数:{2},
_WSs.Count,
片[0]的.index,
sheets.Last()指数));

这导致了我的机器上以下日志:

 钢板在WB:3 
第一张指数:1,
最后一页指数:3

但在下面的日志在目标机器上:

 表中WB:1 
第一张指数:1,
最后一页指数:1

结论:这是标准添加到一个新的工作簿的工作表的数量不同,从用户到用户。
的东西要记住!


I'm trying to deploy an application that works fine on my development pc and some other workstations. However, some users receive an error that I can't seem to grasp.

The program is a C# dotNet app with Excel.Interop functionality (Office 2003).

I seem to be getting a problem with 'indexes'. The weird thing is that this part works perfect on some machines but throws a fatal exception on others... All machines are Windows 7 with Office 2003.

This is the relevant code:

//Change sheet code (index is 1, 2, 3) -> errors at #2
public void ChangeWorksheet(int sheetIndex)
{
    if (_OnXLSEvent != null) _OnXLSEvent(string.Format("TEMP: working on page {0}", sheetIndex));
    _WS = _WSs[sheetIndex];
    _Shapes = _WS.Shapes;
    _PageSetup = _WS.PageSetup;
    if (_OnXLSEvent != null) _OnXLSEvent(string.Format("TEMP: working on page {0}", _WS.Name));
}

//Constructor (_App and _WBs are static)
public ExcelProcessor(bool SaveAutomatically = false, string SavePath = "")
{
    if (_App == null)
        _App = new XLS.Application();
    if (_WBs == null)
        _WBs = _App.Workbooks;
    _WB = _WBs.Add();
    _WSs = _WB.Sheets;
    _WS = _WSs[1];
    _Shapes = _WS.Shapes;
    _PageSetup = _WS.PageSetup;
    _SavePath = SavePath;
    _SaveOnDispose = SaveAutomatically;
    _App.DisplayAlerts = false;
    ApplyPageSetup();
}

This is the log that I'm receiving:

... Irrelevant
8:52:   TEMP: working on page 1
8:52:   TEMP: working on page Sheet1
8:52:   TEMP: working on page 2
8:52:   Error occurred:
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
at Microsoft.Office.Interop.Excel.Sheets.get__Default(Object Index)
at Classes.XLSInterop.ExcelProcessor.ChangeWorksheet(Int32 sheetIndex) in     c:\Users\panjaj\Documents\VS Projects\Projects\Client   Projects\ProFormaCreator\ProFormaCreator\Classes\XLSInterop\ExcelProcessor.cs:line 74
at Classes.ApplicationManager.Manager.ProcessSingleDocument(InFileDocument doc) in c:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\ApplicationManager\ApplicationManager.cs:line 327
at Classes.ApplicationManager.Manager.ConvertFile(String File) in c:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\ApplicationManager\ApplicationManager.cs:line 172

解决方案

I spoke too soon! This is just a really dumb error. I thought I'd give the solution so others might not fall into the same trap as I did ;-)

To analyse the problem further, I added following code to the constructor:

List<XLS.Worksheet> sheets = new List<XLS.Worksheet>()
foreach(XLS.Worksheet sh in _WSs)
{
    sheets.Add(sh);
}
if(_OnXLSEvent != null) _OnXLSEvent(String.Format("\n\tSheets in WB: {0}\n\tFirst Sheet index: {1}, \n\tLast Sheet index: {2}",
                                                  _WSs.Count,
                                                  sheets[0].Index,
                                                  sheets.Last().Index));

This resulted in following log on my machine:

Sheets in WB: 3
First Sheet index: 1, 
Last Sheet index: 3

But in following log on the target machine:

Sheets in WB: 1
First Sheet index: 1, 
Last Sheet index: 1

Conclusion: the amount of worksheets that are standard added to a new workbook differ from user to user. Something to keep in mind!

这篇关于C#中的Excel互操作:从HRESULT异常(DISP_E_BADINDEX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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