3

大家好,我在使用 Excel.Worksheet.Cells 数组时遇到了转置行为。

我的第一个单元格需要位于[row = 10, column = 3]
我的第二个单元格需要位于[row = 11, column = 17]

然后使用这两个单元格作为边界,我创建一个范围并合并它。从上面提到的值可以看出,范围应该大部分是水平的。因此,为了帮助我,我创建了一个简单的辅助函数来合并单元格:

    public static void MergeRange(Excel.Worksheet worksheet, int startRowIdx, 
        int startColIdx, int endRowIdx, int endColIdx, bool across = false)
    {   //Get range boundaries.
        Excel.Range cells = worksheet.Cells;

        //commented out, resolved code is directly below - N. Miller, 9-24-2013
        //Excel.Range topleftCell = cells[startColIdx][startRowIdx];
        //Excel.Range bottomrightCell = cells[endColIdx][endRowIdx];
        Excel.Range topleftCell = cells[startRowIdx, startColIdx];
        Excel.Range bottomrightCell = cells[endRowIdx, endColIdx];

        //Merge range.
        Debug.WriteLine(String.Format("({0}, {1}) to ({2}, {3}).", startRowIdx, startColIdx, endRowIdx, endColIdx)); 
        Excel.Range range = worksheet.get_Range(topleftCell, bottomrightCell);
        Excel.Interior interior = range.Interior;
        interior.ColorIndex = XLColor.PERIWINKLE; //JUST HIGHLIGHTS RANGE FOR NOW.
        //range.Merge(across);

        //Cleanup
        Marshal.ReleaseComObject(interior);
        Marshal.ReleaseComObject(range);
        Marshal.ReleaseComObject(bottomrightCell);
        Marshal.ReleaseComObject(topleftCell);
        Marshal.ReleaseComObject(cells);
    }

在上面的代码中,我通过交换列和行来选择我想要的单元格。这与我的预期相反,会导致所需的行为,但它与 MSDN 文档相矛盾:

MSDN Worksheet.Cells

在 MSDN 文档中,他们给出了一个示例,其中首先列出了行,然后是列。

所以我的问题是……哪个是正确的? 应该在前,还是应该在前?当我修改我的代码以与 MSDN 文档一致时,突出显示的单元格被转置。

4

1 回答 1

3

如果我理解正确,那么当您使用 say 时,cells[1][2]这意味着A2在这种情况下,列首先出现,然后是行。如果你这样写,cells[1,2]你会得到B2. 在这种情况下,行首先出现,然后是列。

在 Excel-VBA 中也是如此。?Cells(1)(2).address在即时窗口中给出$A$2?Cells(1,2).address给出$B$1如果您觉得我没有理解您的查询,请为我重新措辞...

这是测试它的完整代码。

注意:使用 VS 2010 Ultimate + Excel 2010 测试

using System;
using System.Windows.Forms;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

Namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Excel.Application xlexcel;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            object misValue = Missing.Value;
            xlexcel = new Excel.Application();
            xlexcel.Visible = true;

            //~~> Open a File (Change filename as applicable)
            xlWorkBook = xlexcel.Workbooks.Open("C:\\SomeFile.xlsx",
                         0, true, 5, "", "", true,

            Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
            "\t", false, false, 0, true, 1, 0);

            //~~> Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            Excel.Range cells = xlWorkSheet.Cells;
            Excel.Range topleftCell = cells[1][2];

            MessageBox.Show(topleftCell.Address);

            topleftCell = cells[1,2];

            MessageBox.Show(topleftCell.Address);

            //~~> Once done close and quit Excel
            xlWorkBook.Close(false, misValue, misValue);
            xlexcel.Quit();

            //~~> CleanUp
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlexcel);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}

截屏

在此处输入图像描述

在此处输入图像描述

于 2013-09-23T21:11:40.380 回答