static void Main(string[] args) { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWb = xlApp.Workbooks.Open(@"C:\stackoverflow.xlsx"); Excel.Worksheet xlWs = (Excel.Worksheet)xlWb.Sheets[1]; // Sheet1 xlApp.Visible = true; // cut column B and insert into A, shifting columns right Excel.Range copyRange = xlWs.Range["B:B"]; Excel.Range insertRange = xlWs.Range["A:A"]; insertRange.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, copyRange.Cut()); }
/// <summary> /// 剪切粘贴,若结束单元格与开始单元格一直则输入空字符 /// </summary> /// <param name="cutCell1">剪切开始单元格</param> /// <param name="cutCell2">剪切结束单元格,若结束单元格与开始单元格一直则输入空字符</param> /// <param name="pasteCell1">粘贴开始单元格</param> /// <param name="pasteCell2">粘贴结束单元格,若结束单元格与开始单元格一直则输入空字符</param> public void cutAndPaste(string cutCell1, string cutCell2, string pasteCell1, string pasteCell2) { Excel.Range range = null; if (string.IsNullOrEmpty(cutCell2)) { range = sheet.get_Range(cutCell1, miss); } else { range = sheet.get_Range(cutCell1, cutCell2); } range.Cut(miss); if (string.IsNullOrEmpty(pasteCell2)) { range = sheet.get_Range(pasteCell1, miss); } else { range = sheet.get_Range(pasteCell1, pasteCell2); } range.Select(); sheet.Paste(miss, miss); }
private static void copyAndInsertTotalFooter(Worksheet _worksheet, int _totalRows) { Microsoft.Office.Interop.Excel.Range _copyRange = _worksheet.Range["B165:E168"]; Microsoft.Office.Interop.Excel.Range _insertRange = _worksheet.Range["B" + (_totalRows + 3) + ":E" + (_totalRows + 6) + ""]; _insertRange.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, _copyRange.Cut()); }