91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using OfficeOpenXml;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace friction
|
|
{
|
|
public class ExcelHelper
|
|
{
|
|
//The correct method to convert width to pixel is:
|
|
//Pixel =Truncate(((256 * {width} + Truncate(128/{Maximum DigitWidth}))/256)*{Maximum Digit Width})
|
|
|
|
//The correct method to convert pixel to width is:
|
|
//1. use the formula =Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100
|
|
// to convert pixel to character number.
|
|
//2. use the formula width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
|
|
// to convert the character number to width.
|
|
|
|
public const int MTU_PER_PIXEL = 9525;
|
|
|
|
public static int ColumnWidth2Pixel(ExcelWorksheet ws, double excelColumnWidth)
|
|
{
|
|
//The correct method to convert width to pixel is:
|
|
//Pixel =Truncate(((256 * {width} + Truncate(128/{Maximum DigitWidth}))/256)*{Maximum Digit Width})
|
|
|
|
//get the maximum digit width
|
|
decimal mdw = ws.Workbook.MaxFontWidth;
|
|
|
|
//convert width to pixel
|
|
decimal pixels = decimal.Truncate(((256 * (decimal)excelColumnWidth + decimal.Truncate(128 / mdw)) / 256) * mdw);
|
|
//double columnWidthInTwips = (double)(pixels * (1440f / 96f));
|
|
|
|
return Convert.ToInt32(pixels);
|
|
|
|
}
|
|
|
|
public static double Pixel2ColumnWidth(ExcelWorksheet ws, int pixels)
|
|
{
|
|
//The correct method to convert pixel to width is:
|
|
//1. use the formula =Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100
|
|
// to convert pixel to character number.
|
|
//2. use the formula width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
|
|
// to convert the character number to width.
|
|
|
|
//get the maximum digit width
|
|
decimal mdw = ws.Workbook.MaxFontWidth;
|
|
|
|
//convert pixel to character number
|
|
decimal numChars = decimal.Truncate(decimal.Add((pixels - 5) / mdw * 100, (decimal)0.5)) / 100;
|
|
//convert the character number to width
|
|
decimal excelColumnWidth = decimal.Truncate((decimal.Add(numChars * mdw, 5)) / mdw * 256) / 256;
|
|
|
|
return Convert.ToDouble(excelColumnWidth);
|
|
}
|
|
|
|
public static int RowHeight2Pixel(double excelRowHeight)
|
|
{
|
|
//convert height to pixel
|
|
decimal pixels = decimal.Truncate((decimal)(excelRowHeight / 0.75));
|
|
|
|
return Convert.ToInt32(pixels);
|
|
}
|
|
|
|
public static double Pixel2RowHeight(int pixels)
|
|
{
|
|
//convert height to pixel
|
|
double excelRowHeight = pixels * 0.75;
|
|
|
|
return excelRowHeight;
|
|
}
|
|
|
|
public static int MTU2Pixel(int mtus)
|
|
{
|
|
//convert MTU to pixel
|
|
decimal pixels = decimal.Truncate(mtus / MTU_PER_PIXEL);
|
|
|
|
return Convert.ToInt32(pixels);
|
|
}
|
|
|
|
public static int Pixel2MTU(int pixels)
|
|
{
|
|
//convert pixel to MTU
|
|
int mtus = pixels * MTU_PER_PIXEL;
|
|
|
|
return mtus;
|
|
}
|
|
}
|
|
}
|