跳至页脚内容
使用 IRONXL

如何在 Excel 中移动行

Excel has been one of the most popular forms of data manipulation for many years, with millions of computer users employing this spreadsheet every day for work or personal accounting needs. It is available for over 1.4 billion Windows-based PCs, which translates to about 45% of the world's population. Excel is a powerful tool for data analysis, back-and-forth editing, sorting, filtering, pivot tables, and charts. It is also one of the most popular tools in the business world with over four million people actively using it every day. Column and row headers, column letters, and row numbers are principal features of Excel spreadsheets. Using column letters and row numbers, you can address any cell — the main element in Excel.

The popularity of Excel among businesses has created an environment where there are several expensive editions to choose from that offer more features and financial analysis, including the "Professional" edition. This version of Excel is typically used in business settings where the functionality enables higher levels of data analysis, advanced graphing capabilities, and coordination with third-party applications. Excel is a spreadsheet program with a Microsoft Word-like interface that allows users to create and analyze data using formulas.

Let's take a look at how we can move rows or columns in Excel.

Move an entire row in Microsoft Excel

We can move rows or selected cells in Microsoft Excel. There are several methods to do this. Let's suppose we have these rows in Microsoft Excel and that we need to move a row. We can now examine how to get this done.

Drag and drop

How to Move Rows in Excel, Figure 1: Original data rows in Excel
Original data rows in Excel

Follow the following steps to move the row:

  • Select the row that you want to move.
  • Hold the Shift Key and take your cursor to the corner of the selected row. An icon of 4 sides will pop up.

How to Move Rows in Excel, Figure 2: Moving data using drag and drop
Moving data using drag and drop

  • Click on the edge (with the left mouse button) while still holding the Shift key.
  • Move it to the row where you want this row to be shifted.
  • When you see a bold line at the bottom of the row, release the mouse button. After that, release the Shift key too. It is important to keep the Shift key pressed until the last moment.

These steps can be followed for selected cells too. If you want to move the entire column, you can select the entire column and then follow the same steps.

Cut and paste method

The cut-and-paste method is an easy method for moving rows and columns. You can do it without the drag-and-drop method, and without inserting a new row or new column. Follow the steps to move an entire row or multiple adjacent rows in Excel.

  • Select the row or multiple rows to be moved in Microsoft Excel.
  • Press the Ctrl key and simultaneously press the X key to cut the selected row. (Ctrl + X)

How to Move Rows in Excel, Figure 3: Cutting the selected row
Cutting the selected row

  • Now go to the row where you want to move the row.
  • Right-click on the row in Excel and from the right-click menu, select the insert cut cells option.

How to Move Rows in Excel, Figure 4: Pasting a row from the right-click menu
Pasting a row from the right-click menu

  • It will paste the selected row and the other row will move upward.

How to Move Rows in Excel, Figure 5: The result after the cut-and-paste method
The result after the cut-and-paste method

Using this method, it becomes very easy to move entire rows or columns in Excel. You can move columns too by following the same technique. Simply select columns and all operations will be performed on the selected column.

IronXL: C# Excel Library

IronXL is a .NET C# Excel Library for developers to edit and save Excel files. It has been designed to help developers quickly create, edit, and save Excel files without the need for Microsoft Excel to be installed.

The IronXL Excel library has been designed for .NET C# developers to edit and save Excel files without having Microsoft Excel installed on their machines. The IronXL Excel library is a powerful tool that can be used by any developer who needs to work with Excel spreadsheets in their projects. The IronXL Library includes many features that are not found in the standard Microsoft Excel libraries. These include:

  • Visualization of data tables with column filtering, sorting, and grouping capabilities
  • Data validation functionality
  • Data extraction from an existing spreadsheet into a new one
  • Interactive charts with customizations such as color schemes, titles, axis labels, etc.
  • Ability to export charts as images or PDF files
  • Customizable pivot tables for displaying complex datasets

Here is the code to add a row at the first position in the Excel file:

using IronXL;

static void Main(string[] args)
{
    // Load the Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");

    // Get the worksheet named "Sheet1"
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Start from the last row and prepare to push all rows down by one
    int j = ws.Rows.Count() + 1;

    // Shift all existing rows down by one and set new row at the top
    for (int i = 1; i <= ws.Rows.Count(); i++)
    {
        // If reached the first position, insert new row
        if (j == 0)
        {
            ws.Rows[0].Value = "new row";
            break;
        }

        // Assign values from row j to row (j + 1)
        ws["A" + (j + 1)].Value = ws["A" + j].Value;
        ws["B" + (j + 1)].Value = ws["B" + j].Value;
        ws["C" + (j + 1)].Value = ws["C" + j].Value;
        ws["D" + (j + 1)].Value = ws["D" + j].Value;
        ws["E" + (j + 1)].Value = ws["E" + j].Value;

        // Move to the previous row
        j = j - 1;
    }

    // Save the changes to the Excel file
    wb.SaveAs("sample.xlsx");
}
using IronXL;

static void Main(string[] args)
{
    // Load the Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");

    // Get the worksheet named "Sheet1"
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Start from the last row and prepare to push all rows down by one
    int j = ws.Rows.Count() + 1;

    // Shift all existing rows down by one and set new row at the top
    for (int i = 1; i <= ws.Rows.Count(); i++)
    {
        // If reached the first position, insert new row
        if (j == 0)
        {
            ws.Rows[0].Value = "new row";
            break;
        }

        // Assign values from row j to row (j + 1)
        ws["A" + (j + 1)].Value = ws["A" + j].Value;
        ws["B" + (j + 1)].Value = ws["B" + j].Value;
        ws["C" + (j + 1)].Value = ws["C" + j].Value;
        ws["D" + (j + 1)].Value = ws["D" + j].Value;
        ws["E" + (j + 1)].Value = ws["E" + j].Value;

        // Move to the previous row
        j = j - 1;
    }

    // Save the changes to the Excel file
    wb.SaveAs("sample.xlsx");
}
Imports IronXL

Shared Sub Main(ByVal args() As String)
	' Load the Excel file
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")

	' Get the worksheet named "Sheet1"
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

	' Start from the last row and prepare to push all rows down by one
	Dim j As Integer = ws.Rows.Count() + 1

	' Shift all existing rows down by one and set new row at the top
	For i As Integer = 1 To ws.Rows.Count()
		' If reached the first position, insert new row
		If j = 0 Then
			ws.Rows(0).Value = "new row"
			Exit For
		End If

		' Assign values from row j to row (j + 1)
		ws("A" & (j + 1)).Value = ws("A" & j).Value
		ws("B" & (j + 1)).Value = ws("B" & j).Value
		ws("C" & (j + 1)).Value = ws("C" & j).Value
		ws("D" & (j + 1)).Value = ws("D" & j).Value
		ws("E" & (j + 1)).Value = ws("E" & j).Value

		' Move to the previous row
		j = j - 1
	Next i

	' Save the changes to the Excel file
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

This code will move the rows down and a new row will be placed at the first position of the document. The for loop will be moving all rows down and adding the new row data at the first position. We can add the rows at the end of the document too. Let's take a look at how we can do it:

using IronXL;

static void Main(string[] args)
{
    // Load the Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");

    // Get the worksheet named "Sheet1"
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Determine the new row number, just after the last current row
    int i = ws.Rows.Count() + 1;

    // Add a new row with specific values at the end
    ws["A" + i].Value = "New Row";
    ws["B" + i].Value = "New Row";
    ws["C" + i].Value = "New Row";
    ws["D" + i].Value = "New Row";
    ws["E" + i].Value = "New Row";

    // Save the changes to the Excel file
    wb.SaveAs("sample.xlsx");
}
using IronXL;

static void Main(string[] args)
{
    // Load the Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");

    // Get the worksheet named "Sheet1"
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Determine the new row number, just after the last current row
    int i = ws.Rows.Count() + 1;

    // Add a new row with specific values at the end
    ws["A" + i].Value = "New Row";
    ws["B" + i].Value = "New Row";
    ws["C" + i].Value = "New Row";
    ws["D" + i].Value = "New Row";
    ws["E" + i].Value = "New Row";

    // Save the changes to the Excel file
    wb.SaveAs("sample.xlsx");
}
Imports IronXL

Shared Sub Main(ByVal args() As String)
	' Load the Excel file
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")

	' Get the worksheet named "Sheet1"
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

	' Determine the new row number, just after the last current row
	Dim i As Integer = ws.Rows.Count() + 1

	' Add a new row with specific values at the end
	ws("A" & i).Value = "New Row"
	ws("B" & i).Value = "New Row"
	ws("C" & i).Value = "New Row"
	ws("D" & i).Value = "New Row"
	ws("E" & i).Value = "New Row"

	' Save the changes to the Excel file
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

This code will add the new row at the bottom of the document. You can see more details on how to add a new row in our how-to pages.

IronXL has a variety of pricing plans. You can even get a free trial — the IronXL free trial can be activated without any payment or card information. However, IronXL is free for development. Visit our licensing page for more information.

常见问题解答

如何通过编程在Excel中将一行移到新位置?

您可以使用IronXL的C#方法选择要移动的行并将其放置在新位置。这可以通过操控WorkBookWorkSheet对象重新安排行来实现。

手动在Excel中移动行的流程是什么?

要手动在Excel中移动行,您可以使用拖放方法,选择行,按住Shift键并将其拖到所需位置。或者,使用剪切和粘贴方法,按Ctrl + X并在新位置插入剪切的单元格。

我可以不安装Excel来自动化Excel文件操作吗?

可以,IronXL允许您使用C#在无需安装Microsoft Excel的情况下编程创建、编辑和保存Excel文件,提供无缝的自动化体验。

如何以编程方式在Excel表的顶部添加新行?

使用IronXL,您可以将现有行下移一位,并使用C#代码在首位置插入新行,从而实现动态数据输入至Excel表的顶部。

是否可以使用C#将新行追加到Excel表的末尾?

可以,通过确定最后一行的编号,您可以使用IronXL在表末尾追加新行,并使用C#代码为此新行设置值。

IronXL有哪些高级数据操作功能?

IronXL提供高级功能,如数据可视化、验证和提取,以及创建交互式图表和可定制的数据透视表,所有这些都可以通过C#编程来访问。

IronXL如何通过免费选项支持开发人员?

IronXL提供免费试用版,并允许免费用于开发目的,详细的许可信息可在其官方网站上查阅。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。