c#Excel互操作-在多个Range.Insert()s上丢失边框

本文关键字:边框 Insert 互操作 Range c#Excel | 更新日期: 2023-09-27 18:29:49

当我在一列上调用Range.Copy(),然后在另一列之前调用Range.Insert()时,它可以完美地工作;原始列被复制到我想要的位置。但是,如果我再次调用Range.Insert()而不调用Range.Copy(),它会在我想要的地方插入一个新列,但没有任何边框。

我发现,如果我每次都调用Range.Copy(),输出正是我想要的,但它需要大约5倍的时间。我真的需要那样做吗?

这是我的代码供参考:

Range sheetCols = sheet.Columns;
...
int col = routesStartCol;
foreach (RouteObject route in loc.Routes)
{
    Range thisCol = sheetCols[col]; com.Got(thisCol);
    if (col == routesStartCol)
        thisCol.Copy();
    else
        thisCol.Insert(XlInsertShiftDirection.xlShiftToRight);
    sheetCells[routeNameRow, col] = route.Name;
    col++;
}

c#Excel互操作-在多个Range.Insert()s上丢失边框

oRng1 = oSheet.get_Range("A1", "D1");
oRng2 = oSheet.get_Range("A2", "D2");
oRng1.Copy(oRng2); //copy r1 to r2
//to insert above r2
oRng2.Insert(Excel.XlInsertShiftDirection.xlShiftDown,Excel.XlInsertFormatOrigin.xlFormatFromLeftOrAbove);

看起来我真的每次都需要复制。当你不复制时,它真正做的只是插入一个空白列,默认情况下,它会将列的一些格式复制到左边。