C# 在excel中的列之间插入列
我有一个Excel文件,其中有许多列。现在我需要在“C”和“D”之间插入列。。因此,结果列应该是“C”、“新列(D)”、“E”。。请帮我做这个 打开Excel文件的部分代码如下所示C# 在excel中的列之间插入列,c#,excel,C#,Excel,我有一个Excel文件,其中有许多列。现在我需要在“C”和“D”之间插入列。。因此,结果列应该是“C”、“新列(D)”、“E”。。请帮我做这个 打开Excel文件的部分代码如下所示 Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbook = a
Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(txtDestination.Text.ToString() + "\\" + Path.GetFileName(File_Name, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
将上述评论作为答案重新发布,以便将问题标记为已回答 请参阅:以获取解决方案。您只需将“A1”值更改为您希望在前面插入的列(“示例中的D1”)我这样做: 选择要在旁边插入新列的列
Excel.Range oRng = oSheet.Range["I1"];
插入新列,指定要移动现有列的方向。
在本例中,我们在I1的左侧插入一个新列;I1将成为H1
oRng.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight,
Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow);
要对新列执行某些操作,例如设置标题值,请再次选择I1范围
oRng = oSheet.Range["I1"];
设置列标题文本
oRng.Value2 = "Discount";
选中此项:请注意,插入的列数与活动选择中的列数成比例。
oRng.Value2 = "Discount";