c# - 在范围内使用 Excel.Region.get_Range() 会产生意外行为

标签 c# excel

我正在尝试在 Excel C# 中访问另一个范围内的范围,并出现意外行为。在第一个范围内,Cell[1,1].Value 给出了该范围内第一个单元格的值,如预期的那样。但是,在“子范围”中,Cell[1,1].Value 并没有给出子范围中第一个单元格的值。相反,它会向下跳几行。

Microsoft documentation

namespace RangeRange
{
    class RangeRange
    {
        private Excel.Application excelApp = new Excel.Application();
        private Excel.Workbook workbook;
        private Excel.Worksheet worksheet;

        public int Load(string filepath)
        {
            workbook = excelApp.Workbooks.Open(filepath);
            worksheet = (Excel.Worksheet)workbook.Sheets[1];

            return 0;
        }

        public int Close()
        {
            // Shut down excel.exe
            workbook.Close();
            excelApp.Quit();

            return 0;
        }

        public int Process()
        {
            Excel.Range data_range = worksheet.UsedRange.Cells;
            int rows = data_range.Rows.Count;
            int cols = data_range.Columns.Count;

            // Get the range containing rows 3 through N.
            Excel.Range c1 = worksheet.Cells[3, 1];
            Excel.Range c2 = worksheet.Cells[rows, cols];
            Excel.Range range1 = (Excel.Range)worksheet.get_Range(c1, c2);

            string value1 = range1.Cells[1, 1].Value;

            Console.WriteLine(value1);

            ProcessRange(range1);

            return 0;
        }

        private int ProcessRange(Excel.Range my_range)
        {
            // Get a range, containing all rows, within my_range.
            int rows = my_range.Rows.Count;
            int cols = my_range.Columns.Count;

            Excel.Range c1 = my_range.Cells[1, 1];
            Excel.Range c2 = my_range.Cells[rows, cols];
            Excel.Range range2 = (Excel.Range)my_range.get_Range(c1, c2);

            string value2 = range2.Cells[1, 1].Value;

            Console.WriteLine(value2);

            return 0;
        }
    }
}

我的输入数据是:

data.xlsx

输出是:

output window

最佳答案

通过从传入的范围中获取工作表,然后将所需的“子范围”设置为工作表上的范围,我能够使其工作。

    private int ProcessRange(Excel.Range my_range)
    {
        // Get a range, containing all rows, within my_range.
        int rows = my_range.Rows.Count;
        int cols = my_range.Columns.Count;
        Excel.Range c1 = my_range.Cells[1, 1];           
        Excel.Range c2 = my_range.Cells[rows, cols];

        Excel.Worksheet ws = my_range.Worksheet;
        Excel.Range range2 = (Excel.Range)ws.Range[c1, c2];

        string value2 = range2.Cells[1, 1].Value;

        Console.WriteLine(value2);

        return 0;
    }

关于c# - 在范围内使用 Excel.Region.get_Range() 会产生意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45199622/

相关文章:

excel - 取消时Application.InputBox错误424

c# - 由于 Json.Net TypeNameHandling auto 导致外部 json 易受攻击?

vba - 使用文本而不是值创建变体数组

Excel 列值随条件变化

C# new Dictionary<string, object>() is IDictionary<object, object> 返回 false

java - 解析内容中的封装器未正确转义的 CSV 文件

excel - vba - Excel 2010。局部变量未在我的函数中初始化

c# - .NET4.0 : Thread-Safe Manner of Updating A Dictionary and Its Values

c# - Windows 应用程序中 Model View Controller 的问题

c# - 无法从 C# 调用 C++ 函数