Formula to convert .NET pixels to Excel width in OpenXML format

前端 未结 3 543
故里飘歌
故里飘歌 2021-02-13 04:16

I have spent many hours trying to determine a formula to convert .NET pixels to an Excel column width using the OpenXML format. I am using EPPlus to generate xmls documents. I\'

3条回答
  •  无人共我
    2021-02-13 04:30

    An upgrade to the answer MikeTeeVee gave, as I was seeing discrepancies at the 3rd dp.

    In the calculation of points width from the OpenXml file width the documentation states

    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

    This lead me to a mostly stab in the dark experiment and come up with a conversion from pixels to width:

    double width = Math.Truncate(px / 7 * 256) / 256
    

    This returns precisely the same value as found in the OpenXml document.

提交回复
热议问题