Unit Conversion

Utility class LengthUnitConverter can be used to convert values between various measurement units and the following example shows how you can use it.

Various Excel lengths extracted with GemBox.Spreadsheet
Screenshot of various Excel lengths extracted with GemBox.Spreadsheet
Upload your file (Drag file here)
using System;
using System.Text;
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 sb = new StringBuilder();

        var worksheet = workbook.Worksheets[0];

        sb.AppendFormat("Sheet left margin is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(worksheet.PrintOptions.LeftMargin, LengthUnit.Inch, LengthUnit.Pixel)));
        sb.AppendLine();

        sb.AppendFormat("Width of column A is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(worksheet.Columns[0].Width, LengthUnit.ZeroCharacterWidth256thPart, LengthUnit.Pixel)));
        sb.AppendLine();

        sb.AppendFormat("Height of row 1 is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(worksheet.Rows[0].Height, LengthUnit.Twip, LengthUnit.Pixel)));
        sb.AppendLine();

        var picture = worksheet.Pictures[1];
        sb.AppendFormat("Image width x height is: {0} centimeters x {1} centimeters.",
            Math.Round(picture.Position.GetWidth(LengthUnit.Centimeter), 2),
            Math.Round(picture.Position.GetHeight(LengthUnit.Centimeter), 2));

        Console.WriteLine(sb.ToString());
    }
}
Imports System
Imports System.Text
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 sb = New StringBuilder()

        Dim worksheet = workbook.Worksheets(0)

        sb.AppendFormat("Sheet left margin is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(worksheet.PrintOptions.LeftMargin, LengthUnit.Inch, LengthUnit.Pixel)))
        sb.AppendLine()

        sb.AppendFormat("Width of column A is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(worksheet.Columns(0).Width, LengthUnit.ZeroCharacterWidth256thPart, LengthUnit.Pixel)))
        sb.AppendLine()

        sb.AppendFormat("Height of row 1 is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(worksheet.Rows(0).Height, LengthUnit.Twip, LengthUnit.Pixel)))
        sb.AppendLine()

        Dim picture = worksheet.Pictures(1)
        sb.AppendFormat("Image width x height is: {0} centimeters x {1} centimeters.",
            Math.Round(picture.Position.GetWidth(LengthUnit.Centimeter), 2),
            Math.Round(picture.Position.GetHeight(LengthUnit.Centimeter), 2))

        Console.WriteLine(sb.ToString())
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.