Excel.Worksheet.Cells有反转行为吗?

时间:2013-09-23 20:21:43

标签: c# excel transpose

嘿所有,我在使用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文档一致时,突出显示的单元格被转置。

1 个答案:

答案 0 :(得分:3)

如果我理解正确,那么当您使用说cells[1][2]时,则表示A2在这种情况下,首先是列,然后是行。如果您将其写为cells[1,2],那么您将获得B2。在这种情况下,Row首先出现,然后是Column。

在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();
            }
        }
    }
}

<强>截图

enter image description here

enter image description here