Unit Conversion in Excel files

The following example shows how you can display the cell size in different measurement units using GemBox.Spreadsheet's LengthUnitConverter utility class.

Upload your file (Drag file here)
using System;
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = ExcelFile.Load("%InputFileName%");
        var worksheet = workbook.Worksheets[0];
        var cell = worksheet.Cells["A1"];

        double widthInPoints = cell.Column.GetWidth(LengthUnit.Point);
        double heightInPoints = cell.Row.GetHeight(LengthUnit.Point);

        Console.WriteLine("A1 cell's size in different units:");

        foreach (LengthUnit unit in Enum.GetValues(typeof(LengthUnit)))
        {
            // The CharacterWidth should not be used with LengthUnitConverter, see:
            // https://www.gemboxsoftware.com/spreadsheet/docs/GemBox.Spreadsheet.LengthUnit.html
            if (unit == LengthUnit.CharacterWidth)
                continue;

            double convertedWidth = LengthUnitConverter.Convert(widthInPoints, LengthUnit.Point, unit);
            double convertedHeight = LengthUnitConverter.Convert(heightInPoints, LengthUnit.Point, unit);
            Console.WriteLine($"{convertedWidth:0.###} x {convertedHeight:0.###} {unit}");
        }
    }
}
Imports System
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = ExcelFile.Load("%InputFileName%")
        Dim worksheet = workbook.Worksheets(0)
        Dim cell = worksheet.Cells("A1")

        Dim widthInPoints As Double = cell.Column.GetWidth(LengthUnit.Point)
        Dim heightInPoints As Double = cell.Row.GetHeight(LengthUnit.Point)

        Console.WriteLine("A1 cell's size in different units:")

        For Each unit As LengthUnit In [Enum].GetValues(GetType(LengthUnit))

            ' The CharacterWidth should not be used with LengthUnitConverter, see:
            ' https://www.gemboxsoftware.com/spreadsheet/docs/GemBox.Spreadsheet.LengthUnit.html
            If unit = LengthUnit.CharacterWidth Then Continue For

            Dim convertedWidth As Double = LengthUnitConverter.Convert(widthInPoints, LengthUnit.Point, unit)
            Dim convertedHeight As Double = LengthUnitConverter.Convert(heightInPoints, LengthUnit.Point, unit)
            Console.WriteLine($"{convertedWidth:0.###} x {convertedHeight:0.###} {unit}")

        Next

    End Sub
End Module
Workbook's cell size in all measurement units
Screenshot of workbook's cell size in different units

LengthUnitConverter supports the most common measurement units, such as pixel, pica, inch, and centimeter.

Additionally, GemBox.Spreadsheet provides methods for specifying or retrieving various dimension values in different measurement units.

For instance, with ExcelRow.GetHeight(LengthUnit) or ExcelRow.SetHeight(Double, LengthUnit) you can get or set the row's height in the desired length unit, and with ExcelColumn.GetWidth(LengthUnit) or ExcelColumn.SetWidth(Double, LengthUnit) you can get or set the column's width in the desired length unit.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy