【问题标题】:C# Excel interop: Exception from HRESULT (DISP_E_BADINDEX)C# Excel 互操作:来自 HRESULT 的异常 (DISP_E_BADINDEX)
【发布时间】:2013-05-26 22:36:22
【问题描述】:

我正在尝试部署一个可以在我的开发电脑和其他一些工作站上正常运行的应用程序。 但是,有些用户收到了我似乎无法理解的错误。

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

我似乎遇到了“索引”的问题。奇怪的是,这部分在某些机器上运行完美,但在其他机器上却抛出了一个致命的异常...... 所有机器都是 Windows 7 和 Office 2003。

这是相关代码:

//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();
}

这是我收到的日志:

... 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

【问题讨论】:

  • 这是 IndexOutOfRangeException 的 Office 互操作版本。电子表格根本没有第二张纸。哦,床单!

标签: c# excel indexing interop


【解决方案1】:

我说得太早了! 这只是一个非常愚蠢的错误。我想我会给出解决方案,这样其他人就不会像我一样落入同样的陷阱;-)

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

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));

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

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

但是在目标机器上的以下日志中:

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

结论:标准添加到新工作簿的工作表数量因用户而异。 需要记住的一点!

【讨论】:

  • 谢谢!办公室升级导致我公司出现这种情况。
【解决方案2】:

您可以检查工作表是否不存在,然后添加它们。通常默认情况下会创建第一个工作表,您可以按如下方式检查其余部分。我有类似的问题并解决如下。

           // Add Worksheet 2 if not present
            if (workbook.Worksheets.Count < 2)
            {
                workbook.Worksheets.Add();
            }

            // Add Worksheet 3 if not present
            if (workbook.Worksheets.Count < 3)
            {
                workbook.Worksheets.Add();
            }

【讨论】:

    猜你喜欢
    • 2015-05-04
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多