在Excel中的列之间插入列

时间:2013-09-04 04:29:57

标签: c# excel

我有一个Excel文件,其中包含多列。现在我需要在“C”和“D”之间插入列..所以生成的列应该是“C”,“New Column(D)”,“E”..请帮我这个.. < / p>

打开Excel文件的代码部分如下...

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;

3 个答案:

答案 0 :(得分:7)

我这样做:

选择要在

旁边插入新列的列
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";

答案 1 :(得分:0)

将上述评论作为答案重新发布,以便将问题标记为已回答。

请参阅:Adding a new column at the start of an excel table in an excel了解解决方案。您所需要做的就是将“A1”值更改为您之前要插入的列(在您的示例中为“D1”)

答案 2 :(得分:0)

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;