Excel Interop 的无效索引异常

Invalid index exception with Excel Interop

我用 C# winform 获取数据 excel。

private void ReleaseExcelObject(object obj)
        {
            try
            {
                if (obj != null)
                {
                    Marshal.ReleaseComObject(obj);
                    obj = null;
                }
            }
            catch (Exception ex)
            {
                obj = null;
                throw ex;
            }
            
        }
        private void buttonOpen_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application application = null;
            Workbook workBook = null;

            int tempNum = 0;
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                if (dlg.ShowDialog() != DialogResult.OK)
                    return;

                application = new Microsoft.Office.Interop.Excel.Application();
                application.Visible = true;
                workBook = application.Workbooks.Open(dlg.FileName);

                Worksheet newWorkSheet = workBook.Worksheets.Add(After: workBook.Worksheets.Item[workBook.Worksheets.Count]);
               

                int newSheetRow = 0;

                Dictionary<int, string> upData = new Dictionary<int, string>();
                for (int sheetIndex = 1; sheetIndex<workBook.Worksheets.Count; sheetIndex++)
                {

                    for (int row = 1; row < workBook.Worksheets[sheetIndex].UsedRange.Rows.Count; row++)
                    {
                        upData.Add(row, null);

                        for (int col = 1; col < workBook.Worksheets[sheetIndex].UsedRange.Columns.Count; col++)
                        {
                            tempNum++;

                            if (workBook.Worksheets[sheetIndex].Cells[col, row] != null)
                            {
                                string cellData = workBook.Worksheets[sheetIndex].Cells[col, row].value;
                                string[] datas = cellData.Split(' ');
                                string dataName = null;

                                for (int k = 1; k < datas.Length; k++)
                                {
                                    dataName += datas[k];
                                }
                                string icd11 = datas[0];

                                newWorkSheet.Cells[newSheetRow,0] = icd11;
                                newWorkSheet.Cells[newSheetRow,1] = dataName;
                                newWorkSheet.Cells[newSheetRow,2] = upData[col - 1];

                                upData[col] = dataName;


                                continue;
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(tempNum + exc.Message);   
            }
            finally
            {
                ReleaseExcelObject(workBook);
                ReleaseExcelObject(application);
            }
        }

出现错误的部分是 MessageBox.Show(workBook.Sheets[0].ToString()); 这部分。
并强制 workBookSheet.getitem("hardCording").
如果这样做,单元格的直接数据访问部分会出错。

我想知道如何查询和插入 excel 数据。

错误是Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

Excel数据:

编辑sheet[0] sheet[0] 不能

没看懂VBA

如何实现这个

excel树示例

  Worksheet newWorkSheet = workBook.Worksheets.Add(After: workBook.Worksheets.Item[workBook.Worksheets.Count]);
               

                int newSheetRow = 1;

                Dictionary<int, string> upData = new Dictionary<int, string>();
            

                for (int sheetIndex = 1; sheetIndex<workBook.Worksheets.Count; sheetIndex++)
                {
                    int rowMax=workBook.Worksheets[sheetIndex].UsedRange.Rows.Count;
                    int colMax = workBook.Worksheets[sheetIndex].UsedRange.Columns.Count;

                    for (int i = 0; i < workBook.Worksheets[sheetIndex].UsedRange.Rows.Count; i++)
                        upData.Add(i, "000000");

                    for (int row = 1; row < workBook.Worksheets[sheetIndex].UsedRange.Rows.Count; row++)
                    {
                        

                        for (int col = 1; col < workBook.Worksheets[sheetIndex].UsedRange.Columns.Count; col++)
                        {
                            tempRow = row;
                            tempCol = col;

                            if (workBook.Worksheets[sheetIndex].Cells[row, col] != null)
                            {
                                Range cell = workBook.Worksheets[sheetIndex].Cells[row, col];
                                string cellData = cell.Value;
                                if (cellData != null)
                                {
                                    string[] datas = cellData.Split(null);
                                    string dataName = null;

                                    for (int k = 1; k < datas.Length; k++)
                                    {
                                        dataName += datas[k];
                                    }
                                    string icd11 = datas[0];
                                    
                                    Range newCellData1 = newWorkSheet.Cells[newSheetRow, 1];
                                    newCellData1.Value = icd11;
                                    Range newCellData2 = newWorkSheet.Cells[newSheetRow, 2];
                                    newCellData2.Value = dataName;

                                    Range newCellData3 = newWorkSheet.Cells[newSheetRow, 3];
                                    newCellData3.Value = upData[col - 1];
                                    upData[col] = datas[0];
                                    newSheetRow++;
                                }

                                continue;
                            }
                        }
                    }
                    upData.Clear();
                }
            }

excel 调试在 visual studio 中不起作用 我修复了代码中的错误。 细胞。值和这些子项不可见