EXCEL的高度和寬度計算-文檔值到像素

經過筆者多次查閱、驗證,筆者認爲官方文檔在計算列款存在誤導,實際使用與文檔不一致,詳見第三部分,歡迎大家來指正本文在寬度計算的問題,謝謝。

一、高度計算

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. WINDOWS WPS計算 默認字體:宋體 大小11磅

        高度爲13.5磅的像素爲   px=13.5/72*96 =18.00 
        高度爲0.48釐米的像素爲 px=0.48/2.54*96 =18.14
    

    在WPS的若某行存在與默認不一樣的高度時,在某行類的屬性上ht就存在磅值,其計算方式與默認磅值相同。
    此處的釐米數爲wps上展示的,下同,和上述計算對比使用,有興趣的同學可以根據宏獲取磅值,再算像素

  2. MAC WPS計算 默認字體:宋體 大小12磅

          高度爲17.6磅的像素爲    px=17.6/72*96 =23.46     
          高度爲0.62釐米的像素爲 px=0.62/2.54*96 =23.43 
    

二、寬度計算

寬度值到像素的轉換爲 : PX=(width*8) (公式-3) 其中PX爲像素 width爲 defaultColWidth或列的值。

  1. 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         
    
  2. MAC WPS計算 默認字體:宋體 大小12磅 defaultColWidth值:9.1428

             寬度爲9.1428的像素爲     px=9.1428*8 =73.14       
             寬度爲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下比較好(筆者被坑慘了,吐槽一下)。

三、寬度理論部分

  1. sheetFormatPr對象類的defaultColWidth屬性

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)}
如果用戶已手動設置此,則沒有計算,並且只指定一個值。

  1. COl對象類的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 beTruncate([8
7+5]/7256)/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(((2568.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 (公式-4)
示例:以 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}) (公式-5)
[示例:使用與上述相同的示例,計算將爲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 個字符。

以上理論內容均是官方文檔,在文檔筆者發現在計算公式-4與常識有悖論的地方,假設默認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的文檔中就可以輕易證實。
這也是筆者沒有使用官方的運算的方法的原因,根據這個公式怎麼也計算不出想要的,這裏也歡迎WPS/OFFICE的專家指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章