Get Cell Type in Excel in C#

Get Cell Type in Excel in C#

2025-04-23 06:35:00 Written by  Koohji
Rate this item
(0 votes)

When working with Excel files programmatically in C#, determining the data type of a cell is critical for accurate data processing. Whether you're validating user inputs, parsing spreadsheets, or migrating data, knowing how to get the cell data types ensures your application handles information correctly.

In this guide, you'll learn how to use Spire.XLS for .NET library to check the cell data types in Excel in C#, covering the following aspects:

Install the .NET Excel Library

Before proceeding, you need to have the Spire.XLS for .NET library installed in your project. A recommended way to install it is via NuGet, and there are two available options:

  • 1.Right-click the project in “Solution Explorer -> Manage NuGet Packages”. Search for “Spire.XLS” and install the latest version.
  • 2.Go to “Tools -> NuGet Package Manager -> Package Manager Console”, and then run the following command:
    PM> Install-Package Spire.XLS

Alternatively, you can download the library from this link and manually add the DLL files as references.

Learn the Cell Data Types in Spire.XLS

Spire.XLS provides the XlsWorksheet.TRangeValueType enumeration to represents the cell value types. It includes following members:

Cell Type Description
String Represents the cells that store alphanumeric characters, including numbers stored as text.
Number Represents the cells that contain numeric value, including integers, decimals, scientific notation, and date-time values.
Formula Represents the cells that contain a formula.
Blank Represents the cells contain no data.
Boolean Represents the cells that contain TRUE or FALSE.
Error Represents the cells may display errors as #VALUE! or #N/A due to invalid operations.

How to Get Cell Data Type in Excel in C#

To check the value type of a cell, you need to follow the below steps.

  • Create a Workbook object.
  • Load an XLS or XLSX Excel file, and then get a specified worksheet within it.
  • Get a specified cell range in the sheet though the Worksheet.Range[] property.
  • Iterate through each cell in the cell range.
  • Get the row number and column number of the current cell.
  • Call the Worksheet.GetCellType(int row, int column, bool bNeedFormulaSubType) method to get the value type of the current cell, and return a specific enumeration member of the XlsWorksheet.TRangeValueType enumeration type.
  • Converts the value of the enumeration member to its corresponding text string.
  • Write the text string to another cell and set its font style.
  • Save the result file using the Workbook.SaveToFile() method.
  • Sample C# Code
using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet;

namespace GetCellType
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Workbook object
            Workbook workbook = new Workbook();
            // Load an Excel file
            workbook.LoadFromFile("Test1.xlsx");

            // Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            // Get a specified cell range
            CellRange range = sheet.Range["A2:A8"];

            // Iterate through all cells in the range
            foreach (CellRange cell in range)
            {
                // Get the row number and column number of the current cell
                int row = cell.Row;
                int column = cell.Column;

                // Get the value type of the current cell
                XlsWorksheet.TRangeValueType cellType = sheet.GetCellType(row, column, false);

                // Convert the cell type value to a text string and write to another cell
                sheet[row, column + 1].Text = cellType.ToString();

                // Set the font style of the output cell
                sheet[row, column + 1].Style.Font.Color = Color.Red;
                sheet[row, column + 1].Style.Font.IsBold = true;
            }

            // Save the result file
            workbook.SaveToFile("GetCellType.xlsx", ExcelVersion.Version2016);
        }
    }
}

A screenshot of the result file:

Get or retrieve the cell data types in an Excel worksheet

Conclusion

Spire.XLS simplifies checking Excel cell data types in C# with its intuitive API. By leveraging the GetCellType() method, you can accurately determine whether a cell contains a number, string, boolean, etc. This approach ensures robust data processing for applications that interact with Excel files.

Get a Free License

To fully experience the capabilities of Spire.XLS for .NET without any evaluation limitations, you can request a free 30-day trial license.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 23 April 2025 09:20