Search code examples
c#.net-4.0excel-interopexcel-4.0

Get Index out of range exception using Microsoft.Office.Interop.Excel.Range object for old version of Excel (2003)


Several years ago I was working with Excel in c# app to create report with statistic data. It was WPF application with .Net Framework 4.0 which allows me to process some data, calculate statistic parameters and create report in Excel.

I used Microsoft.Office.Interop.Excel COM library version 11 to work with Excel-2003 format. So now I have a goal to rewrite this application using Net Core 3.1. I have some limitations for programing now: OS-version and Excel-version (Win 7 and Excel 2003).

Also I try to left my code as more close to original as possible. I investigated and found that I will get a lot of efforts if I decide to rewrite this part of application which used Excel with using some library (Epplus for example). Some libraries are not free or their free versions are severely restricted. Also popular Epplus don't provide functionality which I need to customize my excel charts. For example I discovered that in Epplus I can't customize text labels orientation and gridlines for axes which predefined for particular chart types. So my goal is integrate old version of Microsoft.Office.Interop.Excel in my dotnet core 3.1 solution.

But I faced with issue during do that. I have provided part of the code below. When execution hit line with Range range1 = ws.Range[ws.Cells[kursor, i], ws.Cells[kursor, i]] I get Index out of range exception which occurs in COM library and this error makes me completely bewildered. Nothing has changed in my code to cause this error. Old version of app with Net Framework 4.0 works as expected on the same input data. I even discovered that if I switch to the Net Framework 4.8 and add reference to this excel COM library v.11 I don't have this error. So my app not working only in Net Core environment. A stupid idea, but maybe the fact that new versions of c# now have a System.Range and it somehow affect this? I don't think so, but...
I can't figure out why this is happening and it makes me sad. Could someone help me how I can fix this error? Grateful for any help on this issue.

using Microsoft.Office.Interop.Excel;
using System;
using System.Diagnostics;
using System.Linq;
using WpfStatistica.Events;
using WpfStatistica.Statistic;
using Range = Microsoft.Office.Interop.Excel.Range;

namespace WpfStatistica.Excel.Old
{
    public class ExelCreator
    {
        private readonly StatistCollector _statData;
        private readonly string _filePath;

        private Application _excel;
        private Workbook _workbook;
        private Sheets _sheets;


        public ExelCreator(StatistCollector statData, string filePath)
        {
            _statData = statData;
            _filePath = filePath;
        }

        public void CreateExcelFile()
        {
            _excel = new Application();
            _excel.Visible = false;
            _excel.SheetsInNewWorkbook = 4;
            _workbook = _excel.Workbooks.Add();
            _sheets = _excel.Worksheets;
            ProcessFirstSheet();
            ProcessSecondSheet();
            ProcessThirdSheet();
            ProcessFourthSheet();

            _workbook.SaveAs(_filePath, 1);
            _excel.Quit();
           
        }

        private void ProcessFirstSheet()
        {
            Worksheet ws = _excel.Worksheets[1];
            ws.Name = "Types of failures";

            // Place columns names
            ws.Cells[1, 1] = "Test";
            // Some code here
            ws.Cells[1, 34] = "StdErr";

            //Stat params output
            for (int i = 0; i < _statData.TestNum.Count; i++)
            {
                ws.Cells[i + 2, 1] = _statData.TestNum[i];
                // Some code here
            }

            // Create table with info
            int kursor = _statData.TestNum.Count + 5;
            Range range = null;
            for (int i = 1; i <= 9; i++)
            {
                Range range1 = ws.Range[ws.Cells[kursor, i], ws.Cells[kursor, i]]; //=> Get Index out of range error here
                Range range2 = ws.Range[ws.Cells[kursor - 1, i], ws.Cells[kursor - 1, i]];
                
                range = ws.Range[range1, range2];
                if (i == 1 || i == 2 || i == 4 || i == 7)
                {
                    range.Merge();
                    range.Font.Bold = true;
                }
                range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
                range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = 1;
                range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = 1;
                range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                range.VerticalAlignment = XlVAlign.xlVAlignCenter;
                range1.Font.Bold = true;
            }

            // The rest of the code for working with filling in the table and creating a Pareto chart
        } 
        // Other methods
    }
}

Solution

  • In previous version this construction Range range1 = ws.Range[ws.Cells[kursor, i], ws.Cells[kursor, i]] works as expected, but now I should cast ws.Cells[kursor, i] implicitly. So it works with (Range)ws.Cells[kursor, i].