我可以在 Open XML SDK 生成的电子表格上设置自动宽度而不计算各个宽度吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我理解规范和 这个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?
我正在使用我强烈推荐的EPPlus。我花了一段时间才弄清楚如何使用它来做到这一点,这就是我想出的:
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: