ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-Excel.Worksheet.Cells具有相反的行为?

2019-11-22 19:06:38  阅读:937  来源: 互联网

标签:transpose c excel


嘿,我在处理Excel.Worksheet.Cells数组时遇到了转置行为.

我的第一个单元格必须位于[行= 10,列= 3]
我的第二个单元格必须位于[行= 11,列= 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文档一致时,突出显示的单元格将被转置.

解决方法:

如果我对您的理解正确,那么当您使用cells [1] [2]时,则表示A2.在这种情况下,列首先出现,然后是行.如果将其写为单元格[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();
            }
        }
    }
}

屏幕截图

标签:transpose,c,excel
来源: https://codeday.me/bug/20191122/2062034.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有