Unit Conversion in Excel worksheets

You can use the GemBox.Spreadsheet provides a LengthUnitConverter utility class to convert values between different measurement units. With this class, you can use the units you are most comfortable with 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.

The following example shows how you can display the cell size in different measurement units.

Workbook's cell size in all measurement units
Screenshot of workbook's cell size in different units
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

EMU is the highest precision unit. The LengthUnitConverter.Convert method converts:

  • 1 Point to 12700 Emus.
  • 1 Pixel to 9525 Emus.
  • 1 Inch to 914400 Emus.
  • 1 Millimeter to 36000 Emus.
  • 1 Centimeter to 360000 Emus.
  • 1 Twip to 635 Emus.
  • 1 ZeroCharacterWidth to 66675 Emus.
  • 1 ZeroCharacterWidth256thPart to 260.44921875 Emus.

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