gpt4 book ai didi

c# - 计算/将 Excel 列宽转换为 Word 表列宽的公式/算法

转载 作者:行者123 更新时间:2023-12-04 22:27:30 28 4
gpt4 key购买 nike

我正在寻找可以将 Excel 列宽转换为 Word 表宽的公式。
示例:在 excel 中创建一个简单的表格,更改列的宽度。最后将其复制/粘贴到 MS Word 文档中。

经过检查它显示的两个文件。

微软Excel:

<x:cols>
<x:col min="1" max="1" width="3.77734375" customWidth="1" />
<x:col min="2" max="2" width="10.21875" customWidth="1" />
<x:col min="3" max="3" width="12.77734375" customWidth="1" />
</x:cols>

微软字:
<w:tblGrid>
<w:gridCol w:w="454" />
<w:gridCol w:w="1100" />
<w:gridCol w:w="1380" />
</w:tblGrid>

有人可以帮助我或指导我正确的方向吗?

最佳答案

Word 的列宽存储为点的二十分之一。您可能会发现这篇文章很有启发性 http://officeopenxml.com/WPtableGrid.php .

Excel 不使用度量​​单位,因此最多只能进行 1:1 转换。 Excel 的宽度取决于使用的字体大小; Word 不会(除非表格属性设置为展开以适合内容)。

来自 width (Column Width)https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.column?view=openxml-2.8.1

Column width measured as the number of characters of the maximum digit width of the numbers 0, 1, 2, …, 9 as rendered in the normal style's font. There are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines.

width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256

[Example: Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi). In fact, each digit is the same width for this font. Therefore, if the cell width is 8 characters wide, the value of this attribute must be Truncate([8*7+5]/7*256)/256 = 8.7109375. end example]

To translate the value of width in the file into the column width value at runtime (expressed in terms of pixels), use this calculation:

=Truncate(((256 * {width} + Truncate(128/{Maximum Digit Width}))/256)*{Maximum Digit Width})

[Example: Using the same example as above, the calculation would be Truncate(((256*8.7109375+Truncate(128/7))/256)*7) = 61 pixels. end example]

To translate from pixels to character width, use this calculation:

=Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100

[Example: Using the example above, the calculation would be Truncate((61-5)/7*100+0.5)/100 = 8 characters. end example]

[Note: when wide borders are applied, part of the left/right border must overlap with the 2 pixel padding on each side. Wide borders do not affect the width calculation of the column. end note]

[Note: When the sheet is in the mode to view formulas instead of values, the pixel width of the column is doubled. end note]

The possible values for this attribute are defined by the W3C XML Schema double datatype.



以下内容可能也有帮助 http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/ ,它提供了在上面的第一个等式中进行计算的代码。

关于c# - 计算/将 Excel 列宽转换为 Word 表列宽的公式/算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56444172/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com