首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Microsoft.Office.Interop.Excel.Range对象为旧版本的Excel (2003)获取超出范围的异常索引

使用Microsoft.Office.Interop.Excel.Range对象为旧版本的Excel (2003)获取超出范围的异常索引
EN

Stack Overflow用户
提问于 2022-05-15 17:23:46
回答 1查看 207关注 0票数 0

几年前,我在c#应用程序中使用Excel创建带有统计数据的报表。它是用.Net框架4.0开发的WPF应用程序,它允许我处理一些数据,计算统计参数,并在Excel中创建报表。

我使用Microsoft.Office.Interop.Excel COM库版本11来使用Excel-2003格式。现在我的目标是使用NetCore3.1重写这个应用程序。我现在对编程有一些限制:OS-版本和Excel -版本(Win 7和Excel 2003).

此外,我尽量让我的代码尽可能接近原始代码。我调查并发现,如果我决定重写应用程序的这一部分,使用Excel和一些库(例如Epplus),我会付出很大的努力。有些库不是免费的,或者它们的免费版本受到严格限制。此外,流行的Epplus不提供功能,我需要定制我的excel图表。例如,我发现在Epplus中,我不能自定义特定图表类型预定义的轴的文本标签、方向和网格线。因此,我的目标是在我的dotnet核心3.1解决方案中集成旧版本的Microsoft.Office.Interop.Excel。

但我在做这件事的时候遇到了问题。我提供了下面代码的一部分。当使用Range range1 = ws.Range[ws.Cellskursor,i,ws.Cellskursor,i]执行命中时,会发现索引超出了COM库中发生的range异常,这个错误使我完全迷惑。我的代码中没有发生任何导致此错误的更改。带有NETFramework4.0的应用程序的旧版本在相同的输入数据上可以像预期的那样工作。我甚至发现,如果切换到NetFramework4.8并添加对excel库v.11的引用,我就没有这个错误。所以我的应用程序不只是在Net环境下工作。这是一个愚蠢的想法,但也许新版本的c#现在有了一个System.Range,而它却以某种方式影响了这一点?我不这么认为但是..。

我不知道为什么会发生这种事,这让我很难过。有人能帮我解决这个错误吗?感谢在这个问题上的任何帮助。

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-17 04:55:53

在以前的版本中,这个构造范围range1 = ws.Range[ws.Cellskursor,i,ws.Cellskursor,i]按预期工作,但是现在我应该显式地转换ws.Cellskursor。所以它适用于(范围)ws.Cellskursor,i。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72250572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档