在Excel中的列之间插入列 [英] Insert Columns between columns in excel

查看:94
本文介绍了在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;

解决方案

我这样做是这样的:

选择要在其旁边插入新列的列

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";

I have an Excel File in which I have number of columns. Now I need to insert columns for example between "C" and "D".. so that the resulting columns should be "C" ,"New Column(D)", "E".. Please help me with this..

Parts of Code to open the Excel file is as below...

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;

解决方案

I do it like this:

Select the column you want to insert the new column next to

Excel.Range oRng = oSheet.Range["I1"];

Insert the new column, specifying the direction you want to shift existing columns. In this case, we insert a new column to the left of I1; I1 will become H1

oRng.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, 
                    Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow);

To do something with the new column, such as set the header value, select the I1 range again.

oRng = oSheet.Range["I1"];

Set the column header text

oRng.Value2 = "Discount";

这篇关于在Excel中的列之间插入列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆