Chinaunix首页 | 论坛 | 博客
  • 博客访问: 501837
  • 博文数量: 99
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 783
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-12 09:11
文章分类

全部博文(99)

文章存档

2023年(2)

2022年(1)

2020年(1)

2019年(1)

2018年(4)

2017年(16)

2016年(60)

2015年(1)

2013年(3)

2006年(10)

我的朋友

分类: 信息化

2019-10-25 21:55:00

        想要看公式的直接到第二部分,第一部分内容均是官方文档,经过笔者多次查阅、验证,笔者认为官方文档在计算列款存在误导,实际使用与文档不一致,欢迎大家来指正本文在宽度计算的问题,谢谢。

一、EXCEL文档内两个重要的属性


(1)、sheetFormatPr对象类
属性 英文说明

baseColWidth

(Base Column Width)

Specifies the number of characters of the maximum digit width of the normal style's font.
This value does not include margin padding or extra padding for gridlines. It is only the number of characters.

See defaultColWidth description in this section for details on calculating this value.

See the col element description, particularly the width attribute description, for more information on what is meant by "maximum digit width".

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

指定普通样式字体的最大数字宽度的字符数。此值不包括网格线的边距填充或额外填充。它只是字符数。

defaultColWidth

(Default Column Width)

Default column width measured as the number of characters of the maximum digit width of the normal style's font.

If the user has not set this manually, then it can be calculated:
defaultColWidth = baseColumnWidth + {margin padding (2 pixels on each side, totalling 4 pixels)} + {gridline (1pixel)}

If the user has set this manually, then there is no calculation, and simply a value is specified.

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

默认列宽度,以普通样式字体的最大数字宽度的字符数度量。
如果用户尚未手动设置此内容,则可以计算:
defaultColWidth baseColumnWidth + {margin padding (2 pixels on each side, totalling 4 pixels)} + {gridline (1pixel)}
如果用户已手动设置此,则没有计算,并且只指定一个值。
此属性的可能值由 W3C XML 架构双数据类型定义。

defaultRowHeight

(Default Row Height)

Default row height measured in point size. Optimization so we don't have to write the height on all rows.
This can be written out if most rows have custom height, to achieve the optimization.

When the row height of all rows in a sheet is the default value, then that value is written here, and customHeight is not set. If a few rows have a different height, that information is written directly on each row. However, if most or all of the rows in the sheet have the same height, but that height isn't the default height, then that height value should be written here (as an optimization), and the customHeight flag should also be set. In this case, all rows having this height do not need to express the height, only rows whose height differs from this value need to be explicitly expressed.

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

默认行高度(磅值)。优化,因此我们不必在所有行上写入高度。如果大多数行具有自定义高度,则可以写出此,以实现优化。
当工作表中所有行的行高度为默认值时,则在此处写入该值,并且未设置自定义高度。如果几行具有不同的高度,则该信息将直接写入每行。但是,如果工作表中的大多数或所有行具有相同的高度,但该高度不是默认高度,则应在此处写入该高度值(作为优化),并且还应设置自定义 Height 标志。在这种情况下,具有此高度的所有行都不需要表示高度,只有高度与此值不同的行需要显式表示。
此属性的可能值由 W3C XML 架构双数据类型定义。


(2)、COl对象类

Defines column width and column formatting for one or more columns of the worksheet. 


Attributes

Description

width (Column Width)


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.

列宽度,以数字 0、1、......、9 的最大数字宽度的字符数度量,以普通样式的字体呈现。边距填充有 4 个像素(每侧两个),加上网格线的 1 像素填充。
        width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
*示例:以 Calibri 字体为例,11 (pt)字体大小的最大数字宽度为 7 像素(在 96 dpi 时)。事实上,每个数字的宽度是同一个字体。因此,如果单元格宽度为 8 个字符宽,则此属性的值必须为
        =Truncate([8*7+5]/7*256)/256 = 8.7109375
一、在运行时将文件中的宽度值转换为列宽度值(以像素表示),使用此计算:

 =Truncate(((256 * {width} + Truncate(128/{Maximum Digit Width}))/256)*{Maximum Digit Width})
 [示例:使用与上述相同的示例,计算将为Truncate(((256*8.7109375+Truncate(128/7))/256)*7) = 61 像素。

二 、要从像素转换为字符宽度,请使用此计算:

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

[示例:使用上面的示例,计算将为Truncate((61- 5)/7*100+0.5)/100  = 8 个字符。

*注意:应用宽边框时,左/右边框的一部分必须与每侧的 2 像素填充重叠。宽边框不会影响列的宽度计算。结束注释*

此属性的可能值由 W3C XML 架构双数据类型定义。


二、高度计算


        EXCEL的高度在文档记录是以磅为单位的,sheetFormatPr类的defaultRowHeight是默认行高以磅为单位大小。 
        MAC WPS默认值:17.6磅 ( 默认字体:宋体 大小12磅), Windos WPS值:13.5磅 (默认字体:宋体 大小11磅)。
        换算公式为:72磅=1英寸,1英寸=2.54厘米。
        那么1磅=1/72英寸,而1英寸所含有PPI个像素,这里就不详细展开PPI的计算了,现在一般计算机默认为96,详细的计算可以自己百度。
        因此磅到像素的转换为 :  PX=(PT/72*PPI)      (公式-1) 其中PX为像素 PT为磅
              厘米到像素的转换为:PX= (mm/2.54*PPI) (公式-2) 其中PX为像素 mm为厘米
     (1)MAC WPS计算 默认字体:宋体 大小12磅
             高度为17.6磅的像素为    px=17.6/72*96 =23.46     
             高度为0.62厘米的像素为 px=0.62/2.54*96 =23.43  (此处的厘米数为wps上展示的,下同,和上述对比使用,有兴趣的同学可以根据宏获取磅值,再算像素
     (2)WINDOWS WPS计算 默认字体:宋体 大小11磅
             高度为13.5磅的像素为   px=13.5/72*96 =18.00 
             高度为0.48厘米的像素为 px=0.48/2.54*96 =18.14
       在WPS的若某行存在与默认不一样的高度时,在某行类的属性上ht就存在磅值,其计算方式与默认磅值相同。


三、宽度计算

          

         要公式的看第二部分,这里(1)部分在说明官方文档的误导
      (1)官方文档说明
            
官方文档对defaultColWidth (Default Column Width) 默认列宽度的解释如下:

Default column width measured as the number of characters of the maximum digit width of the normal style's font.

If the user has not set this manually, then it can be calculated:

   defaultColWidth baseColumnWidth + {margin padding (2 pixels on each side, totalling 4 pixels)} + {gridline (1pixel)}        (公式-3)

            而在列的with计算说又如下:

 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      (公式-4)

              查阅官方文档我们知道EXCEL的列的默认字符个数是8,那么将默认字符8,Maximum Digit Width为X导入上述 (公式-4 )
                       (8X+5/256X=256width
                          =》 width =(8X+5)/ X    
                          =》 width =  8+5/X           (公式-5)
            在公式-5中 5/x为一个反函数,超过5以后随着X-Maximum Digit Width的变大,宽度几乎保持不变?我们从实际的应用中可以知道数字的宽度随着字体的变大,宽度也变大, 这个从EXCEL的文档中就可以轻易证实。
                     
       
(2)宽度到像素计算
  •        MAC WPS计算 默认字体:宋体 大小12磅 defaultColWidth值:9.1428
                 宽度为9.1428的像素为     px=9.1428*8 =73.14      (so easy。。。。。上面balabala一大堆也没说清楚) 
                 为1.79厘米的像素为  px=1.79/2.54*96 =67.65  
                 为1.93厘米的像素为  px=1.93/2.54*96 =72.94 (同样的设置在windows下)
             不知道WPS为何在MAC系统下计算的厘米数与WINDOWS下为何不同,难道是bug。不过73.14-67.65=5.49px(难道是2*padding+1gridlines)不计算在内,有懂WPS的人可以告诉我一下。当然对比两个系统下WPS还有其他差异,此处就不再一一描述来,总之研究WPS还是在WINDOWS下比较好(笔者被坑惨了,吐槽一下)。
  •       WINDOWS WPS计算 默认字体:宋体 大小11磅  defaultColWidth值:9
                width为9的像素为               px=9*8 =72.00                
                为1.9厘米的像素为              px=1.9/2.54*96 =71.81   
             同样设置,在col上设置列宽时:
                宽度width为22.375的像素为       px=22.375*8 =179                
                宽度为4.74厘米的像素为            px=4.74/2.54*96 =179.14  
            至于defaultColWidth怎么计算出来,欢迎知道的专业人事告诉笔者一下,多谢。
阅读(5521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~