我可以在 Open XML SDK 生成的电子表格上设置自动宽度而不计算各个宽度吗?

发布于 2024-08-30 20:08:54 字数 187 浏览 4 评论 0原文

我正在使用 Open XML SDK 从大量数据创建 Excel 文件。我终于成功获得了一个功能性的列节点,它指定了文件中实际使用的所有列。有一个“BestFit”属性可以设置为 true,但这显然没有任何作用。有没有办法自动将这些列设置为“最适合”,以便当有人打开此文件时,它们的大小已经调整到正确的数量?或者我是否被迫提前计算每列的宽度,并在代码中设置它?

I'm working on creating an Excel file from a large set of data by using the Open XML SDK. I've finally managed to get a functional Columns node, which specifies all of the columns which will actually be used in the file. There is a "BestFit" property that can be set to true, but this apparently does not do anything. Is there a way to automatically set these columns to "best fit", so that when someone opens this file, they're already sized to the correct amount? Or am I forced to calculate how wide each column should be in advance, and set this in the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野の 2024-09-06 20:08:54

我理解规范和 这个MSDN讨论BestFit告诉你Excel中自动计算了宽度,但它没有告诉Excel应该在下次打开时再次计算它。

正如“goodol”在该讨论中指出的那样,我认为宽度只能在显示列时计算,因为它取决于内容、使用的字体、其他样式参数......所以即使你想预先计算宽度你自己,请注意,这只是一个估计,如果内容包含大量“宽”字符,则可能是错误的。或者 Open XML SDK 可以为您做这件事吗?

The way I understand the spec and this MSDN discussion, BestFit tells you the width was auto-calculated in Excel, but it does not tell Excel that it should calculate it again next time it is opened.

As "goodol" indicates in that discussion, I think the width can only be calculated when you display the column, since it depends on the contents, the font used, other style parameters... So even if you want to pre-calculate the width yourself, be aware that this is only an estimation, and it can be wrong if the contents contain lots of "wide" characters. Or does the Open XML SDK do this for you?

海未深 2024-09-06 20:08:54

我正在使用我强烈推荐的EPPlus。我花了一段时间才弄清楚如何使用它来做到这一点,这就是我想出的:

// Get your worksheet in "sheet" variable

// Set columns to auto-fit
for (int i = 1; i <= sheet.Dimension.Columns; i++)
{
    sheet.Column(i).AutoFit();
}

I'm using EPPlus which I highly recommend. Took me a while to figure out how to do it using that, here's what I came up with:

// Get your worksheet in "sheet" variable

// Set columns to auto-fit
for (int i = 1; i <= sheet.Dimension.Columns; i++)
{
    sheet.Column(i).AutoFit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文