How to get cell type using C#/VB.NET?
This article explains how to get cell type using C#/VB.NET in WinForms XIsIO.
What is cell type?
Cell type specifies value type of the cell in the worksheet. The cell type in XlsIO can be Blank, Error, Boolean, Number, Formula and String.
Cell Type | Description |
Blank | Blank indicates that the cell does not contain any value. |
Error | Error type shows the cell value is an error value. |
Boolean | If the cell contains TRUE/FALSE, then the cell type will be Boolean. |
Number | Number indicates that the cell contains a number value. |
Formula | Formula refers that the cell contains a formula. |
String | String denotes that the cell contains a text value. |
To get the cell type, you need to follow the below steps.
Steps to get cell type
- Get the cell range to get the cell type.
- Use the WorksheetImpl.GetCellType(int row, int column, bool bNeedFormulaSubType) to get the cell type.
foreach (IRange range in worksheet.UsedRange) { // Get cell type WorksheetImpl.TRangeValueType cellType = (worksheet as WorksheetImpl).GetCellType(range.Row, range.Column, false); // Add the cell type as text into worksheet worksheet[range.Row + 1, range.Column].Text = cellType.ToString(); worksheet[range.Row + 1, range.Column].CellStyle.Font.Bold = true; }
To know more about cell manipulation in XlsIO, please refer the documentation.
The following C#/VB.NET complete code snippet shows how to get cell type in XlsIO.
using Syncfusion.XlsIO; using Syncfusion.XlsIO.Implementation; using System.IO; using System.Reflection; namespace XlsIO_Sample { class Program { public static void Main(string[] args) { // Instantiate the spreadsheet creation engine using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; // Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream workbookStream = assembly.GetManifestResourceStream("XlsIOSample.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(workbookStream); IWorksheet worksheet = workbook.Worksheets[0]; foreach (IRange range in worksheet.UsedRange) { // Get cell type WorksheetImpl.TRangeValueType cellType = (worksheet as WorksheetImpl).GetCellType(range.Row, range.Column, false); // Add the cell type as text into worksheet worksheet[range.Row + 1, range.Column].Text = cellType.ToString(); worksheet[range.Row + 1, range.Column].CellStyle.Font.Bold = true; } // Save and close the workbook Stream stream = File.Create("Output.xlsx"); workbook.SaveAs(stream); } } } }
Imports Syncfusion.XlsIO Imports Syncfusion.XlsIO.Implementation Imports System.IO Imports System.Reflection Namespace XlsIO_Sample Class Program Public Shared Sub Main(ByVal args As String()) 'Instantiate the spreadsheet creation engine Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Program).GetTypeInfo().Assembly Dim workbookStream As Stream = assembly.GetManifestResourceStream("XlsIOSample.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(workbookStream) Dim worksheet As IWorksheet = workbook.Worksheets(0) For Each range As IRange In worksheet.UsedRange 'Get cell type Dim cellType As WorksheetImpl.TRangeValueType = (TryCast(worksheet, WorksheetImpl)).GetCellType(range.Row, range.Column, False) 'Add cell type as text into worksheet worksheet(range.Row + 1, range.Column).Text = cellType.ToString() worksheet(range.Row + 1, range.Column).CellStyle.Font.Bold = True Next 'Save and close the workbook Dim stream As Stream = File.Create("Output.xlsx") workbook.SaveAs(stream) End Using End Sub End Class End Namespace
The following screenshot shows the output generated by XlsIO after getting the cell type.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like 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.
Conclusion
I hope you enjoyed learning about how to get cell type using C#/VB.NET.
You can refer to our WinForms XIsIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms XIsIO documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms XIsIO and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!