Excel自动化:如何设置范围?

Kas*_*sen 5 c# excel

获取正在使用的Excel电子表格范围的标准方法是UsedRange方法.我可以像这样复制所有使用过的单元格:

xlWorkSheet.UsedRange.Copy(misValue);
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不是一个好方法,因为如果用户在一个中写入内容然后再次删除它,则"激活"单元格.它可能会产生无意识的后果,标记出数千个空行和列(并在我的情况下打印出来).

所以我已将此方法转换为C#以获得更准确的范围.在调试时,它给出FirstRow = 1,FirstColumn = 1,LastRow = 600,LastColumn = 2,这是我想要使用我的测试工作表作为输入.

但是我仍然需要设置实际使用的范围并复制它.在VB中,它是这样完成的:

Set RealUsedRange = Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn))
Run Code Online (Sandbox Code Playgroud)

如何设置范围并将其复制到C#中?我想替换这一行:

xlWorkSheet.UsedRange.Copy(misValue);
Run Code Online (Sandbox Code Playgroud)

具有(1,1)到(600,2)范围.

我试过这个:

return Excel.Range(xlWorkSheet.Cells[FirstRow, FirstColumn], xlWorkSheet.Cells[LastRow, LastColumn]);
Run Code Online (Sandbox Code Playgroud)

但它给Excel.Range是一个'类型',它在给定的上下文中无效.我不知道写它的正确方法.

    // this struct is just to make the result more readable
    public struct RealRange
    {
        public int FirstRow;
        public int FirstColumn;
        public int LastRow;
        public int LastColumn;

        public RealRange(int fr, int fc, int lr, int lc)
        {
            FirstRow = fr;
            FirstColumn = fc;
            LastRow = lr;
            LastColumn = lc;
        }
    }

    public RealRange RealUsedRange()
    {
        int FirstRow = xlWorkSheet.Cells.Find(
            "*",
            xlWorkSheet.get_Range("IV65536", misValue),
            Excel.XlFindLookIn.xlValues,
            Excel.XlLookAt.xlPart,
            Excel.XlSearchOrder.xlByRows,
            Excel.XlSearchDirection.xlNext,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value
            ).Row;

        int FirstColumn = xlWorkSheet.Cells.Find(
            "*",
            xlWorkSheet.get_Range("IV65536", misValue),
            Excel.XlFindLookIn.xlValues,
            Excel.XlLookAt.xlPart,
            Excel.XlSearchOrder.xlByColumns,
            Excel.XlSearchDirection.xlNext,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value
            ).Column;

        int LastRow = xlWorkSheet.Cells.Find(
            "*",
            xlWorkSheet.get_Range("IV65536", misValue),
            Excel.XlFindLookIn.xlValues,
            Excel.XlLookAt.xlPart,
            Excel.XlSearchOrder.xlByRows,
            Excel.XlSearchDirection.xlPrevious,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value
            ).Row;

        int LastColumn = xlWorkSheet.Cells.Find(
            "*",
            xlWorkSheet.get_Range("IV65536", misValue),
            Excel.XlFindLookIn.xlValues,
            Excel.XlLookAt.xlPart,
            Excel.XlSearchOrder.xlByColumns,
            Excel.XlSearchDirection.xlPrevious,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value
            ).Column;

       return new RealRange(FirstRow, FirstColumn, LastRow, LastColumn);
    }
Run Code Online (Sandbox Code Playgroud)

解决方案 注意我已将返回值从void更改为struct RealRange.这是为了使结果更具可读性.电子表格中的"范围"是2个单元格之间的跨度.您可以使用get_range-function复制范围,就像我在下面所做的那样.感谢D. Hilgarth的帮助.

rr = RealUsedRange();
this.workSheet.get_Range(
   this.workSheet.Cells[rr.FirstRow, rr.FirstColumn], 
   this.workSheet.Cells[rr.LastRow, rr.LastColumn]).Copy(missing);
Run Code Online (Sandbox Code Playgroud)