Unit Conversion

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

Screenshot

Unit Conversion Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.Collections.Generic;
3using System.Text;
4using GemBox.Spreadsheet;
5using GemBox.Spreadsheet.ConditionalFormatting;
6using GemBox.Spreadsheet.PivotTables;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
15
16        ExcelFile ef = ExcelFile.Load("IllustrationsAndShapes.xlsx");
17
18        StringBuilder sb = new StringBuilder();
19
20        var ws = ef.Worksheets[0];
21
22        sb.AppendFormat("Sheet left margin is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(ws.PrintOptions.LeftMargin, LengthUnit.Inch, LengthUnit.Pixel)));
23        sb.AppendLine();
24
25        sb.AppendFormat("Width of column A is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(ws.Columns[0].Width, LengthUnit.ZeroCharacterWidth256thPart, LengthUnit.Pixel)));
26        sb.AppendLine();
27
28        sb.AppendFormat("Height of row 1 is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(ws.Rows[0].Height, LengthUnit.Twip, LengthUnit.Pixel)));
29        sb.AppendLine();
30
31        var picture = ws.Pictures[1];
32        sb.AppendFormat("Image width x height is: {0} centimeters x {1} centimeters.",
33            Math.Round(picture.Position.GetWidth(LengthUnit.Centimeter), 2),
34            Math.Round(picture.Position.GetHeight(LengthUnit.Centimeter), 2));
35
36        Console.WriteLine(sb.ToString());
37    }
38}
1Imports System
2Imports System.Collections.Generic
3Imports System.Text
4Imports GemBox.Spreadsheet
5Imports GemBox.Spreadsheet.ConditionalFormatting
6Imports GemBox.Spreadsheet.PivotTables
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Dim ef As ExcelFile = ExcelFile.Load("IllustrationsAndShapes.xlsx")
16
17        Dim sb = New StringBuilder()
18
19        Dim ws = ef.Worksheets(0)
20
21        sb.AppendFormat("Sheet left margin is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(ws.PrintOptions.LeftMargin, LengthUnit.Inch, LengthUnit.Pixel)))
22        sb.AppendLine()
23
24        sb.AppendFormat("Width of column A is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(ws.Columns(0).Width, LengthUnit.ZeroCharacterWidth256thPart, LengthUnit.Pixel)))
25        sb.AppendLine()
26
27        sb.AppendFormat("Height of row 1 is: {0} pixels.", Math.Round(LengthUnitConverter.Convert(ws.Rows(0).Height, LengthUnit.Twip, LengthUnit.Pixel)))
28        sb.AppendLine()
29
30        Dim picture = ws.Pictures(1)
31        sb.AppendFormat("Image width x height is: {0} centimeters x {1} centimeters.",
32         Math.Round(picture.Position.GetWidth(LengthUnit.Centimeter), 2),
33         Math.Round(picture.Position.GetHeight(LengthUnit.Centimeter), 2))
34
35        Console.WriteLine(sb.ToString())
36
37    End Sub
38
39End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.