How to read cell values from specific columns in Excel using C# and VB.NET?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It can also convert Excel documents to PDF files.
This article explains how to read cell values from specific columns in Excel using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Use the following code snippet to read cell values from specific columns in Excel.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Define the columns to read (0-based indices: 0 = A, 3 = D, 5 = F)
List<int> columns = new List<int> { 0, 3, 5 };
//Get the used range of the worksheet
IRange usedRange = worksheet.UsedRange;
if (usedRange == null || usedRange.LastRow < usedRange.Row)
return; //Exit if the worksheet has no data
int startRow = usedRange.Row;
int endRow = usedRange.LastRow;
//Loop through the specified columns only
foreach (int col in columns)
{
for (int row = startRow; row <= endRow; row++)
{
//Read the cell values from the specified columns
string value = worksheet.GetCellValue(row, col + 1, false);
}
}
}
VB.NET
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Define the columns to read (0-based indices: 0 = A, 3 = D, 5 = F)
Dim columns As New List(Of Integer) From {0, 3, 5}
'Get the used range of the worksheet
Dim usedRange As IRange = worksheet.UsedRange
If usedRange Is Nothing OrElse usedRange.LastRow < usedRange.Row Then
Return 'Exit if the worksheet has no data
End If
Dim startRow As Integer = usedRange.Row
Dim endRow As Integer = usedRange.LastRow
'Loop through the specified columns only
For Each col As Integer In columns
For row As Integer = startRow To endRow
'Read the cell values from the specified columns
Dim value As String = worksheet.GetCellValue(row, col + 1, False)
Next
Next
End Using
You can download the complete sample for reading cell values from specific columns in Excel from
I hope you enjoyed learning how to read cell values from specific columns in Excel using
You can refer to our XIsIO feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with features such as import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!