Knowledgebase (2300)
The proper adjustment of the columns' order in Excel can improve readability. For example, by setting the date data as the first column, we can quickly locate data based on a specific date. It is easy to move columns in MS Excel by using Shift and Drag. This article, however, focuses on how to rearrange columns in Excel in C# and VB.NET by using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Reorder Excel Columns in C# and VB.NET
The following are the steps to rearrange columns in Excel using Spire.XLS for .NET.
- Create a Workbook object, and load a sample Excel file using Workbook.LoadFromFile() method.
- Get the target worksheet using Workbook.Worksheets[index] property.
- Specify the new column order in an int array.
- Create a temporary sheet and copy the data from the target sheet into it.
- Copy the columns from the temporary sheet to the target sheet and store them in the new order.
- Remove the temporary sheet.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using System.Linq;
using Spire.Xls;
namespace MoveColumn
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Set the new column order (the column index starts from 0)
int[] newColumnOrder = new int[] { 3, 0, 1, 2, 4, 5 };
//Add a temporary worksheet
Worksheet newSheet = workbook.Worksheets.Add("temp");
//Copy data from the first worksheet to the temporary sheet
newSheet.CopyFrom(worksheet);
//Loop through the newColumnOrder array
for (int i = 0; i < newColumnOrder.Count(); i++)
{
//Copy the column from the temporary sheet to the first sheet
newSheet.Columns[newColumnOrder[i]].Copy(worksheet.Columns[i], true, true);
//Set the width of a certain column the first sheet to that of the temporary sheet
worksheet.Columns[i].ColumnWidth = newSheet.Columns[newColumnOrder[i]].ColumnWidth;
}
//Remove temporary sheet
workbook.Worksheets.Remove(newSheet);
//Save the workbook to another Excel file
workbook.SaveToFile("MoveColumn.xlsx", FileFormat.Version2016);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
When you are dealing with the data in a worksheet, you may need to rearrange the columns so as to make it easier to find and read the specific data. It is easy to move columns in MS Excel by using Shift and Drag. This article introduces how to programmatically reorder columns in Excel using Spire.XLS for Java.
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>15.11.3</version>
</dependency>
</dependencies>
Rearrange Columns in Excel in Java
The following are the steps to reorder columns in Excel using Spire.XLS for Java.
- Create a Workbook object, and load a sample Excel file using Workbook.loadFromFile() method.
- Get the target worksheet where you’d like to adjust the order using Workbook.getWorksheets().get() method.
- Specify the new column order in an int array.
- Create a temporary sheet and copy the data from the target sheet into it.
- Copy the columns from the temporary sheet to the target sheet and store them in the new order.
- Remove the temporary sheet.
- Save the workbook to another Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class RearrangeColumns {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile( "C:\\Users\\Jack\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
//Set the new column order (the column index starts from 0)
int[] newColumnOrder = new int[]{3, 0, 1, 2, 4, 5};
//Add a temporary worksheet
Worksheet newSheet = workbook.getWorksheets().add("temp");
//Copy data from the first worksheet to the temporary sheet
newSheet.copyFrom(worksheet);
//Loop through the newColumnOrder array
for (int i = 0; i < newColumnOrder.length; i++) {
//Copy the column from the temporary sheet to the first sheet
newSheet.getColumns()[newColumnOrder[i]].copy(worksheet.getColumns()[i],true,true);
//Set the width of a certain column the first sheet to that of the temporary sheet
worksheet.getColumns()[i].setColumnWidth(newSheet.getColumns()[newColumnOrder[i]].getColumnWidth());
}
//Remove temporary sheet
workbook.getWorksheets().remove(newSheet);
//Save the workbook to another Excel file
workbook.saveToFile("output/MoveColumn.xlsx", FileFormat.Version2016);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Excel spreadsheets are scalable grid-based files that are used to organize data and perform calculations. People all across the world use spreadsheets to create tables for personal and business usage. To write a large amount of data into an Excel spreadsheet, it is recommended to use the programming method, which saves time and is less error-prone. In this article, you will learn how to write data into Excel in C# and VB.NET using Spire.XLS for .NET.
- Write Text or Number Values to Specific Cells
- Write Arrays to a Worksheet
- Write a DataTable to a Worksheet
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Write Text or Number Values to Specific Cells
A certain cell in a worksheet can be accessed by Worksheet.Range[int row, int column] property. Then, you can add a text value or a number value to the cell through the XlsRange.Value or XlsRange.Value2 property. The following are the detailed steps.
- Create a Workbook object.
- Get the first worksheet through Workbook.Worksheets[] property.
- Get a specific cell through Workhseet.Range[] property.
- Add a text value or a number value to the cell through XlsRange.Value or XlsRange.Value2 property.
- Save the workbook to an Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace WriteDataToCells
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Write data to specific cells
worksheet.Range[1, 1].Value = "Name";
worksheet.Range[1, 2].Value = "Age";
worksheet.Range[1, 3].Value = "Department";
worksheet.Range[1, 4].Value = "Hiredate";
worksheet.Range[1, 2].Value = "Hazel";
worksheet.Range[2, 2].Value2 = 29;
worksheet.Range[2, 3].Value = "Marketing";
worksheet.Range[2, 4].Value = "2019-07-01";
worksheet.Range[3, 1].Value = "Tina";
worksheet.Range[3, 2].Value2 = 31;
worksheet.Range[3, 3].Value = "Technical Support";
worksheet.Range[3, 4].Value = "2015-04-27";
//Auto fit column widths
worksheet.AllocatedRange.AutoFitColumns();
//Apply a style to the first row
CellStyle style = workbook.Styles.Add("newStyle");
style.Font.IsBold = true;
worksheet.Range[1, 1, 1, 4].Style = style;
//Save to an Excel file
workbook.SaveToFile("WriteToCells.xlsx", ExcelVersion.Version2016);
}
}
}

Write Arrays to a Worksheet
Spire.XLS for .NET provides the Worksheet.InsertArrary() method, allowing programmers to write one-dimensional arrays or two-dimensional arrays into the specified cell range of a worksheet. The steps to write arrays to a worksheet are as follows:
- Create a Workbook object.
- Get the first worksheet through Workbook.Worksheets[] property.
- Create a one-dimensional array and a two-dimensional array.
- Insert the arrays to worksheet using Worksheet.InsertArray() method.
- Save the workbook to an Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace WriteArraysToWorksheet
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Create a one-dimensional array
string[] oneDimensionalArray = new string[] { "January", "February", "March", "April", "May", "June" };
//Write the array to the first row of the worksheet
worksheet.InsertArray(oneDimensionalArray, 1, 1, false);
//Create a two-dimensional array
string[,] twoDimensionalArray = new string[,]{
{"Name", "Age", "Sex", "Dept.", "Tel."},
{"John", "25", "Male", "Development","654214"},
{"Albert", "24", "Male", "Support","624847"},
{"Amy", "26", "Female", "Sales","624758"}
};
//Write the array to the worksheet starting from the cell A3
worksheet.InsertArray(twoDimensionalArray, 3, 1);
//Auto fit column width in the located range
worksheet.AllocatedRange.AutoFitColumns();
//Apply a style to the first and the third row
CellStyle style = workbook.Styles.Add("newStyle");
style.Font.IsBold = true;
worksheet.Range[1, 1, 1, 6].Style = style;
worksheet.Range[3, 1, 3, 6].Style = style;
//Save to an Excel file
workbook.SaveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);
}
}
}

Write a DataTable to a Worksheet
To import data from a DataTable to a worksheet, use the Worksheet.InsertDataTable() method. The following are the detailed steps.
- Create a Workbook object.
- Get the first worksheet through Workbook.Worksheets[] property.
- Create a DataTable with random data.
- Write the DataTable to the worksheet at the specified location using Worksheet.InsertDataTable() method.
- Save the workbook to an Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using System;
using System.Data;
using Spire.Xls;
namespace WriteDataTableToWorksheet
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Create a DataTable object
DataTable dataTable = new DataTable();
dataTable.Columns.Add("SKU", typeof(Int32));
dataTable.Columns.Add("NAME", typeof(String));
dataTable.Columns.Add("PRICE", typeof(String));
//Create rows and add data
DataRow dr = dataTable.NewRow();
dr[0] = 512900512;
dr[1] = "Wireless Mouse M200";
dr[2] = "$85";
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr[0] = 512900637;
dr[1] = "B100 Cored Mouse";
dr[2] = "$99";
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr[0] = 512901829;
dr[1] = "Gaming Mouse";
dr[2] = "$125";
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr[0] = 512900386;
dr[1] = "ZM Optical Mouse";
dr[2] = "$89";
dataTable.Rows.Add(dr);
//Write datatable to the worksheet
worksheet.InsertDataTable(dataTable, true, 1, 1, true);
//Auto fit column width in the located range
worksheet.AllocatedRange.AutoFitColumns();
//Apply a style to the first and the third row
CellStyle style = workbook.Styles.Add("newStyle");
style.Font.IsBold = true;
worksheet.Range[1, 1, 1, 3].Style = style;
//Save to an Excel file
workbook.SaveToFile("InsertDataTable.xlsx", ExcelVersion.Version2016);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.