c# - 使用 OpenXml.Spreadsheet 时如何将列宽设置为 BestFit
问题描述
我正在创建一个 xlsx 文件并尝试将列宽设置为最佳宽度(取决于字体,例如“Calibri”和字体大小,例如 10),在我的情况下,这只是标题标题的长度列。我正在使用OpenXml
和使用net-Core
.
我知道它不适用于MeasureString
并且AutoFit
因为我需要包含System.Drawing
它会导致 eg 的重载new Font
。
我还找到了我可能需要以某种方式使用的方法BestFit
(here)和Width
(here )。
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
我找不到任何可行的解决方案。OpenXml
net-core
我希望有人能帮帮忙。
我对 C# 很陌生。
解决方案
推荐阅读
- react-native - 有没有办法在 React Native 中连接到一个简单的 TCP 套接字?
- sql - 有谁知道如何在 .ASP 经典页面中显示存储在 SQL 表中的 TIFF 图像?
- django - 如何在 Django 的 Web 应用程序中按日期对帖子进行排序?
- python - 比较来自 CSV 的日期的问题
- flutter - Flutter Provider:正确调度动作的最佳方式是什么?
- web - 尝试使用 GitHub Pages 设置 GoDaddy 域时出现问题
- reactjs - 使用 Travis、AWS Elasticbeanstalk 和 Docker 构建失败
- c++ - 针对模板的过时显式实例化进行编译
- python - 需要Python解决方案来提取多个模式之间的数据保存到csv
- jupyter-notebook - Jupyter 笔记本没有指向浏览器