簡體   English   中英

在Excel中的列之間插入列

[英]Insert Columns between columns in 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 = 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";

將上述評論作為答案重新發布,以便將問題標記為已回答。

請參閱: 在excel表的開頭添加一個新列以獲得解決方案。 您所需要做的就是將“A1”值更改為您之前要插入的列(示例中為“D1”)

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM