在现有的Excel工作表的开头插入单元格区域?

我有一个工作表,有5行和3列。 我把这张表复印一份:

activeSheet.Copy(Type.Missing, activeSheet); var outputSheetIndex = activeSheet.Index + 1; var outputSheet = (Worksheet)application.Sheets[outputSheetIndex]; 

以上将表单复制到另一个表单中。 我现在想要在outputSheet中插入另一个范围,并将outputSheet列移到右侧。

我不确定在哪个范围对象上应用Insert方法。

我试图创build一个新的范围插入在开始,但是这不起作用:

  var startCell = outputSheet.Cells[1, 1]; var endCell = outputSheet.Cells[output.Count(), properties.Length + 1]; var writeRange = outputSheet.Range[startCell, endCell]; writeRange.Insert(XlInsertShiftDirection.xlShiftToRight); 

当我插入writeRange时,我想把现有的数据范围向右移动。

如果我明白了,您想要将OutputSheet中的所有数据向右推,留下一列。 例如:

如果是这样的话:

  • | 一| | 一| | 一| | 一
  • | 两个| 两个| 两个| 二

你想要这样,对吧? :

  • | “null”| One | 一| | 一| | 一
  • | “null”| Two | 两个| 两个| 二

然后做:

 outputSheet.Columns[1].Insert(); //creates empty column and pushes the actual data to the right 

如果你想从inputSheet(原始表单)插入一个范围到输出中,只需要:

 Excel.Range inputRange = ((Excel.Range)inputSheet.Cells[1, 4].EntireColumn; //Let's say you want to pass the 4th column (forget the 1, it doesn't do anything because you get the Entire Column, so it ignores the x-axis which are the Rows) Excel.Range outputRange = ((Excel.Range)outputSheet.Cells[1, 1].EntireColumn; //into the first Column of the outputSheet outputRange.Value = inputRange.Value; 

如果出现意想不到的情况(如单元格合并),只需获取特定的行或列并将其删除即可:

 outputSheet.Columns[2].Delete(); 

如果你想要做的只是在你的新的(复制的工作表)插入一个空白列,我认为这将工作:

 outputSheet.Range["A:A"].Insert( Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, Type.Missing); 

如果你想在其他地方的列,你会改变“A:A”到你想要的范围。