Код: Выделить всё
// see: https://stackoverflow.com/questions/18268620/openxml-auto-size-column-width-in-excel
private static double GetTextWidth(string text)
{
var bmp = new Bitmap(1, 1);
using var graphics = Graphics.FromImage(bmp);
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.PageUnit = GraphicsUnit.Pixel;
using var font = new System.Drawing.Font("Calibri", 10F, FontStyle.Regular, GraphicsUnit.Point);
var pixelWidth = graphics.MeasureString(text, font).Width;
//see: https://stackoverflow.com/questions/7716078/formula-to-convert-net-pixels-to-excel-width-in-openxml-format/7902415
var openXmlWidth = (pixelWidth - 12 + 5) / 7d + 1;
return openXmlWidth;
}
Я бы хотел переехать мой код Excel, так что это не «WPF, клиентская сторона», а скорее серверная, и здесь я хочу, чтобы сервер был либо машиной Windows, либо машиной Linux, т. е. мне нужно, чтобы код был кросс-платформенным.
Увы, я не уверен, что можно использовать для измерения ширины текста в пикселях, который не будет зависеть от Windows...
Проблема в том, что многие классы из приведенного выше фрагмента кода кажутся зависимыми от Windows. Т.е. компилятор выдает мне множество предупреждений, например:
Код: Выделить всё
warning CA1416: This call site is reachable on all platforms. 'Graphics.MeasureString(string?, Font)' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'Graphics.TextRenderingHint' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'FontStyle.Regular' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'GraphicsUnit.Pixel' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'GraphicsUnit.Point' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'Bitmap' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'Graphics.PageUnit' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'TextRenderingHint.AntiAlias' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'Font' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
warning CA1416: This call site is reachable on all platforms. 'Graphics.FromImage(Image)' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
Есть ли какие-либо библиотеки или классы, которые я могу использовать для этого? Или предупреждение компилятора «неправильное», и на Linux-сервере это будет прекрасно работать?
PS. Кстати, код используется для расчета, «вписывается» ли данный текст в ячейку Excel определенной ширины; если это не так, то используется другой текст. Т.е. Я не хочу использовать «родную» логику Excel «усечь текст так, чтобы он соответствовал».
Подробнее здесь: https://stackoverflow.com/questions/786 ... text-width
Мобильная версия