C# Miscellaneous Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To convert a column number to an Excel column name in C#:
public static string GetExcelColumnName(int columnNumber) { int dividend = columnNumber; string columnName = String.Empty; int modulo; while (dividend > 0) { modulo = (dividend - 1) % 26; columnName = Convert.ToChar(65 + modulo).ToString() + columnName; dividend = (int)((dividend - modulo) / 26); } return columnName; }
To convert an Excel column name to a number in C#:
public static int GetExcelColumnNumber(string columnName) { int sum = 0; for (int i = 0; i < columnName.Length; i++) { sum *= 26; sum += (columnName[i] - 'A' + 1); } return sum; }
Note that these functions assume that the column number or name is valid for Excel. For example, they do not handle negative or zero column numbers, or column names with invalid characters.
How to get Excel column names in C#: Retrieving Excel column names in C#.
int columnIndex = 1; string columnName = ExcelHelper.GetColumnName(columnIndex); Console.WriteLine($"Column Index: {columnIndex}, Column Name: {columnName}");
Setting cell values in a specific Excel column using C#: Setting cell values in a specific Excel column using C#.
int columnIndex = 1; int rowIndex = 1; object cellValue = "Hello, Excel!"; ExcelHelper.SetCellValue(worksheet, columnIndex, rowIndex, cellValue);
Working with Excel columns and rows in C#: Basic operations involving Excel columns and rows in C#.
// Iterate through columns foreach (var column in ExcelHelper.GetColumns(worksheet)) { // Do something with each column } // Iterate through rows foreach (var row in ExcelHelper.GetRows(worksheet)) { // Do something with each row }
Inserting and deleting Excel columns in C#: Performing operations to insert and delete Excel columns in C#.
int columnIndexToInsert = 2; ExcelHelper.InsertColumn(worksheet, columnIndexToInsert); int columnIndexToDelete = 3; ExcelHelper.DeleteColumn(worksheet, columnIndexToDelete);
Formatting Excel columns in C#: Applying formatting to Excel columns in C#.
int columnIndex = 1; ExcelHelper.ApplyColumnFormat(worksheet, columnIndex, ExcelFormat.Bold | ExcelFormat.Italic);
Using C# to retrieve data from a specific Excel column: Retrieving data from a specific Excel column using C#.
int columnIndex = 1; List<object> columnData = ExcelHelper.GetColumnData(worksheet, columnIndex);
Sorting Excel columns programmatically in C#: Programmatically sorting Excel columns in C#.
int columnIndex = 1; ExcelHelper.SortColumn(worksheet, columnIndex, SortOrder.Ascending);
Resizing Excel columns with C#: Adjusting the width of Excel columns using C#.
int columnIndex = 1; double columnWidth = 15.0; ExcelHelper.ResizeColumn(worksheet, columnIndex, columnWidth);
Excel column validation in C#: Implementing column validation in Excel using C#.
int columnIndex = 1; ExcelHelper.ValidateColumn(worksheet, columnIndex, ExcelValidationType.Numeric, "Value must be numeric");