首页 > 解决方案 > 使用 OpenXml.Spreadsheet 时如何将列宽设置为 BestFit

问题描述

我正在创建一个 xlsx 文件并尝试将列宽设置为最佳宽度(取决于字体,例如“Calibri”和字体大小,例如 10),在我的情况下,这只是标题标题的长度列。我正在使用OpenXml和使用net-Core.

我知道它不适用于MeasureString并且AutoFit因为我需要包含System.Drawing它会导致 eg 的重载new Font

我还找到了我可能需要以某种方式使用的方法BestFithere)和Widthhere )。

Columns columns = new Columns();

// headerTitles is just some list of strings which should define the length of the column
foreach (string columnName in headerTitles)
{

    // somehow I need to include this
    FontName ftn = new FontName();
    ftn.Val = "Calibri";
    FontSize ftsz = new FontSize();
    ftsz.Val = 10;

    // here the Width should be the width_of_column or something like AutoFit
    // ! width_of_columnName is what I am looking for !
    columns.Append(new Column() { Min = 2, Max = 2, Width = width_of_columnName, CustomWidth = true });
}

worksheetPart.Worksheet.Append(columns);

我真的很挣扎。我知道它在使用时很容易,但是System.Drawing我找不到任何可行的解决方案。OpenXmlnet-core

我希望有人能帮帮忙。

我对 C# 很陌生。

标签: c#excelopenxmlopenxml-sdkcolumn-width

解决方案


推荐阅读