Add Column in Excel Workbook in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to Add Column in Excel workbook via Spire.XLS.

using System.Drawing;
using System.Data;
using System.Data.OleDb;
using Spire.Xls;

namespace AddColumn
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\..\..\Data\parts-column.xls");
            Worksheet sheet = workbook.Worksheets[0];

            //add one column
            sheet.InsertColumn(3);

            //get the data of the new column
            DataColumn dataColumn = GetColumnData(4);

            //insert the data into the new column
            sheet.InsertDataColumn(dataColumn, true, 1, 3);

            //add two columns
            sheet.InsertColumn(5, 2);

            //get the data of the two new columns
            DataColumn dataColumn4 = GetColumnData(5);
            DataColumn dataColumn5 = GetColumnData(6);

            //insert the data into the new columns
            sheet.InsertDataColumn(dataColumn4, true, 1, 5);
            sheet.InsertDataColumn(dataColumn5, true, 1, 6);

            int rowCount = 0;
            rowCount = sheet.Rows.Length;
            //set the added columns's style
            SetFormat(sheet, 3,rowCount);
            SetFormat(sheet, 5,rowCount);
            SetFormat(sheet, 6,rowCount);

            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
        static private DataColumn GetColumnData(int columnIndex)
        {
            string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\..\\..\\..\\Data\\database.accdb;Persist Security Info=False;";
            DataTable parts = new DataTable();
            using (OleDbConnection conn = new OleDbConnection(connString))
            {
                string sqlParts = "select * from parts";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlParts, conn);
                adapter.Fill(parts);
            }
            DataColumn column = parts.Columns[columnIndex];
            return column;
        }

        static private void SetFormat(Worksheet sheet, int columnIndex,int rowCount)
        {
            //set the header style
            sheet.Range[1, columnIndex].Style.Font.IsBold = true;
            sheet.Range[1, columnIndex].Style.Color = Color.DeepSkyBlue;
            sheet.Range[1, columnIndex].HorizontalAlignment = HorizontalAlignType.Center;
            sheet.Range[1, columnIndex].AutoFitColumns();

            //draw the lines
            sheet.Range[1, columnIndex, rowCount-1, columnIndex].Style.Borders.LineStyle = LineStyleType.Thin;
            sheet.Range[1, columnIndex, rowCount-1, columnIndex].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range[1, columnIndex, rowCount-1, columnIndex].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;

            //set the data rows's color
            sheet.Range[2, columnIndex, rowCount-1, columnIndex].Style.Color = Color.SkyBlue;
        }
    }
}

Imports System.Drawing
Imports System.Data
Imports System.Data.OleDb
Imports Spire.Xls

Namespace AddColumn
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\..\..\..\Data\parts-column.xls")
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'add one column
			sheet.InsertColumn(3)

			'get the data of the new column
			Dim dataColumn As DataColumn = GetColumnData(4)

			'insert the data into the new column
			sheet.InsertDataColumn(dataColumn, True, 1, 3)

			'add two columns
			sheet.InsertColumn(5, 2)

			'get the data of the two new columns
			Dim dataColumn4 As DataColumn = GetColumnData(5)
			Dim dataColumn5 As DataColumn = GetColumnData(6)

			'insert the data into the new columns
			sheet.InsertDataColumn(dataColumn4, True, 1, 5)
			sheet.InsertDataColumn(dataColumn5, True, 1, 6)

			Dim rowCount As Integer = 0
			rowCount = sheet.Rows.Length
			'set the added columns's style
			SetFormat(sheet, 3,rowCount)
			SetFormat(sheet, 5,rowCount)
			SetFormat(sheet, 6,rowCount)

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
		Private Shared Function GetColumnData(ByVal columnIndex As Integer) As DataColumn
			Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\..\..\Data\database.accdb;Persist Security Info=False;"
			Dim parts As New DataTable()
			Using conn As New OleDbConnection(connString)
				Dim sqlParts As String = "select * from parts"
				Dim adapter As New OleDbDataAdapter(sqlParts, conn)
				adapter.Fill(parts)
			End Using
			Dim column As DataColumn = parts.Columns(columnIndex)
			Return column
		End Function

		Private Shared Sub SetFormat(ByVal sheet As Worksheet, ByVal columnIndex As Integer, ByVal rowCount As Integer)
			'set the header style
			sheet.Range(1, columnIndex).Style.Font.IsBold = True
			sheet.Range(1, columnIndex).Style.Color = Color.DeepSkyBlue
			sheet.Range(1, columnIndex).HorizontalAlignment = HorizontalAlignType.Center
			sheet.Range(1, columnIndex).AutoFitColumns()

			'draw the lines
			sheet.Range(1, columnIndex, rowCount-1, columnIndex).Style.Borders.LineStyle = LineStyleType.Thin
			sheet.Range(1, columnIndex, rowCount-1, columnIndex).Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range(1, columnIndex, rowCount-1, columnIndex).Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None

			'set the data rows's color
			sheet.Range(2, columnIndex, rowCount-1, columnIndex).Style.Color = Color.SkyBlue
		End Sub
	End Class
End Namespace