3

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

3 回答 3

10

我这样做:

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

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";
于 2014-04-07T23:59:31.337 回答
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;
于 2014-03-05T04:31:32.657 回答
0

重新发布上述评论作为答案,因此问题可能被标记为已回答。

请参阅:在 Excel 表格的开头添加新列以获取解决方案。您需要做的就是将“A1”值更改为您希望在之前插入的列(在您的示例中为“D1”)

于 2013-09-04T06:08:47.397 回答