C#Excel interop:来自HRESULT的异常(DISP_E_BADINDEX)

Rec*_*ipe 5 c# indexing excel interop

我正在尝试部署一个在我的开发PC和其他工作站上运行良好的应用程序.但是,有些用户会收到我似乎无法掌握的错误.

该程序是一个带有Excel.Interop功能的C#dotNet应用程序(Office 2003).

我似乎遇到了'索引'的问题.奇怪的是,这部分在某些机器上运行完美,但在其他机器上引发了致命的例外......所有机器都是带有Office 2003的Windows 7.

这是相关代码:

//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();
}
Run Code Online (Sandbox Code Playgroud)

这是我收到的日志:

... 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
Run Code Online (Sandbox Code Playgroud)

Rec*_*ipe 8

我说得太快了!这只是一个非常愚蠢的错误.我以为我会给出解决方案,所以其他人可能不会像我一样陷入同样的​​陷阱;-)

为了进一步分析问题,我在构造函数中添加了以下代码:

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));
Run Code Online (Sandbox Code Playgroud)

这导致以下登录我的机器:

Sheets in WB: 3
First Sheet index: 1, 
Last Sheet index: 3
Run Code Online (Sandbox Code Playgroud)

但是在以下登录目标机器上:

Sheets in WB: 1
First Sheet index: 1, 
Last Sheet index: 1
Run Code Online (Sandbox Code Playgroud)

结论:添加到新工作簿的标准工作表数量因用户而异.要记住的事情!