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 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 widthInZeroCharacterWidth256thPart = cell.Column.Width;
        double heightInTwip = cell.Row.Height;

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

        foreach (LengthUnit unit in Enum.GetValues(typeof(LengthUnit)))
        {
            double convertedWidth = LengthUnitConverter.Convert(widthInZeroCharacterWidth256thPart, LengthUnit.ZeroCharacterWidth256thPart, unit);
            double convertedHeight = LengthUnitConverter.Convert(heightInTwip, LengthUnit.Twip, unit);
            Console.WriteLine($"{convertedWidth:0.###} x {convertedHeight:0.###} {unit}");
        }
    }
}
Imports System
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using 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 widthInZeroCharacterWidth256thPart As Double = cell.Column.Width
        Dim heightInTwip As Double = cell.Row.Height

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

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

            Dim convertedWidth As Double = LengthUnitConverter.Convert(widthInZeroCharacterWidth256thPart, LengthUnit.ZeroCharacterWidth256thPart, unit)
            Dim convertedHeight As Double = LengthUnitConverter.Convert(heightInTwip, LengthUnit.Twip, 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