【EPplus】Column width discrepancy

description

Hi Jan,

I have noticed that when I set a column width there is a 
discrepancy between the width that I set in code and what 
Excel reports.

From the code below, Excel reports the width to be 11.27.

Excel reports the height to be exactly what I set.

oSheet.Column(1).Width = 12.0d;
oSheet.Row(1).Height = 27.75d;

Duardo

Closed Nov 10, 2010 at 3:00 PM by

This is the OOXML value. You can have a look at it if you rename your document *.zip, extract it and look in the \xl\worksheets\sheetX.xml file. I think Excel recalculates it in some way on startup. 
Anyway, I can‘t change it since a lot of people use it.

comments

TomDierickx wrote Apr 9, 2012 at 8:32 PM

For anybody else who runs into this, by some lengthy trial-and-error experimentation, I believe whatever Column width you think you are setting, it narrows it by approx 0.667 and then rounds to the nearest 1/7th and if you are trying to set a value lower than 1.667, it starts rounding down about 50% to the nearest 12th.

So, for X >= 1.667, you will probably see a true column width of [ROUND(7 * (X - 1/256)) - 5] / 7 rounding this to two decimal places. For example, let‘s say you try setting the column width to 10 (i.e. X=10), then you will likely really see a value of "ROUND(7 * (10 - 1/256))" = 70 - 5 = 65 / 7 = 9.29

One workaround that seems to work for a variety of sample widths is to create a custom function to "adjust up" the value you think you‘re setting by an amount that will result in a net value to get you as close as possible (to the nearest 1/7th if > 1.6667 or to the nearest 1/2th if < 1.6667):

oSheet.Column(1).Width = 10 ‘really ends up being 9.29
oSheet.Column(2).Width = SetTrueColumnWidth(10) ‘ really ends up being 10.00

where the following function takes your desired column width and modifies it by enough such that the net result of setting the column width will actually end up being what you want it to be:

Private Function SetTrueColumnWidth(ByVal dblWidth As Double) As Double

‘DEDUCE WHAT THE COLUMN WIDTH WOULD REALLY GET SET TO
Dim z As Double = 1
If (dblWidth >= (1 + 2 / 3)) Then

  z = Math.Round((Math.Round(7 * (dblWidth - 1 / 256), 0) - 5) / 7, 2)

Else

  z = Math.Round((Math.Round(12 * (dblWidth - 1 / 256), 0) - Math.Round(5 * dblWidth, 0)) / 12, 2)

End If

‘HOW FAR OFF? (WILL BE LESS THAN 1)
Dim errorAmt As Double = 0
errorAmt = dblWidth - z

‘CALCULATE WHAT AMOUNT TO TACK ONTO THE ORIGINAL AMOUNT TO RESULT IN THE CLOSEST POSSIBLE SETTING 
Dim adjAmt As Double = 0
If (dblWidth >= (1 + 2 / 3)) Then

  adjAmt = (Math.Round(7 * errorAmt - 7 / 256, 0)) / 7

Else

  adjAmt = ((Math.Round(12 * errorAmt - 12 / 256, 0)) / 12) + (2 / 12)

End If

‘RETURN A SCALED-VALUE THAT SHOULD RESULT IN THE NEAREST POSSIBLE VALUE TO THE TRUE DESIRED SETTING
If (z > 0) Then

  Return dblWidth + adjAmt

Else

  Return 0

End If

End Function

grimmdp wrote Apr 2, 2013 at 1:42 PM

Hi Tom,
Thanks a bunch for posting this, it helped me a ton. Here it is in C# for anyone who need it:

Dean

        public static double GetTrueColumnWidth(double dblWidth)
        {
            //DEDUCE WHAT THE COLUMN WIDTH WOULD REALLY GET SET TO
            double z = 1d;
            if (dblWidth >= (1 + 2 / 3))
                z = Math.Round((Math.Round(7 * (dblWidth - 1 / 256), 0) - 5) / 7, 2);
            else
                z = Math.Round((Math.Round(12 * (dblWidth - 1 / 256), 0) - Math.Round(5 * dblWidth, 0)) / 12, 2);

            //HOW FAR OFF? (WILL BE LESS THAN 1)
            double errorAmt = dblWidth - z;

            //CALCULATE WHAT AMOUNT TO TACK ONTO THE ORIGINAL AMOUNT TO RESULT IN THE CLOSEST POSSIBLE SETTING
            double adjAmt = 0d;
            if (dblWidth >= (1 + 2 / 3))
                adjAmt = (Math.Round(7 * errorAmt - 7 / 256, 0)) / 7;
            else
                adjAmt = ((Math.Round(12 * errorAmt - 12 / 256, 0)) / 12) + (2 / 12);

            //RETURN A SCALED-VALUE THAT SHOULD RESULT IN THE NEAREST POSSIBLE VALUE TO THE TRUE DESIRED SETTING
            if (z > 0)
                return dblWidth + adjAmt;
            return 0d;

        }

  

redaxe wrote Jul 28, 2013 at 8:38 AM

Saved my day thanks a lot,
only one issue 3.5 is being calculated as 3.57 but its fine for me. :)

Christophe_ wrote Jan 7, 2015 at 9:07 AM

Also saved some time on our project, thanks !

时间: 2024-12-31 20:24:28

【EPplus】Column width discrepancy的相关文章

【Flask】Column常用参数

### Column常用参数:1. primary_key:设置某个字段为主键.2. autoincrement:设置这个字段为自动增长的.3. default:设置某个字段的默认值.在发表时间这些字段上面经常用.4. nullable:指定某个字段是否为空.默认值是True,就是可以为空.5. unique:指定某个字段的值是否唯一.默认是False.6. onupdate:在数据更新的时候会调用这个参数指定的值或者函数.在第一次插入这条数据的时候,不会用onupdate的值,只会使用defa

【理解】column must appear in the GROUP BY clause or be used in an aggregate function

column "ms.xxx_time" must appear in the GROUP BY clause or be used in an aggregate function ------------------------------------------------------------------------------------------ 有min(), max(), sum(), avg()这些函数可以和group by 语句连在一起用. The SQL GR

【LeetCode】树(共94题)

[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *

【转】用CSS+DIV时width的问题

CSS盒子占据面积的大小一共是边距(margin)+边框(border)+填白(padding)+宽度(width).所以当你设置width:200px;border:1px xx xx;的时候,实际的这个DIV的width最大值还是200.只是DIV的实际占据页面的宽度是:1(左边框)+200(width)+1(右边框)=202所以这个DIV里面还有200的宽度可以容纳包含的DIV.当然如果里面的DIV宽度大于200,外面DIV定义的宽度就会失去效果. 注意啦!注意啦!重点地方要回答你了,如果

【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】

[168-Excel Sheet Column Title(Excell列标题)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB

【异常处理】Incorrect string value: &#39;\xF0\x90\x8D\x83...&#39; for column... Emoji表情字符过滤的Java实现

Emoji表情字符现在在APP已经广泛支持了.但是Mysql的UTF8编码对Emoji字符的支持却不是那么好.所以我们经常会遇到这样的异常: Incorrect string value: '\xF0\x90\x8D\x83...' for column 原因是Mysql里UTF8编码最多只能支持3个字节,而Emoji表情字符使用的UTF8编码,很多都是4个字节,有些甚至是6个字节. 解决的方案有两种: 1.使用utf8mb4的mysql编码来容纳这些字符. 2.过滤掉这些特殊的表情字符. 关于

【MySQL】Unknown column &#39;column_name&#39; in &#39;field list&#39;

使用 INSERT INTO - SELECT FROM - ON DUPLICATE KEY UPDATE 时遇到了这个问题,百思不得其解?? 后来总算找到了解决方法,使用子查询.如下: 参考链接:https://stackoverflow.com/questions/2472229/insert-into-select-from-on-duplicate-key-update [MySQL]Unknown column 'column_name' in 'field list' 原文地址:h

【BBED】bbed常用命令

[BBED]bbed常用命令         一.1  相关知识点扫盲 BBED(Oracle Block Browerand EDitor Tool),用来直接查看和修改数据文件数据的一个工具,是Oracle一款内部工具,可以直接修改Oracle数据文件块的内容,在一些极端恢复场景下比较有用.该工具不受Oracle支持,所以默认是没有生成可执行文件的,在使用前需要重新连接.   一.1.1  我的编译代码 ls -l  $ORACLE_HOME/rdbms/lib/*sbbd* ls -l 

基于&lt;MediaElement&gt;的WPF视频播放器(带部分特效)【2】

一.前言       上回说到需要做放视频的使用向导,这两天公司里的老司机一直帮我答疑解惑,让这个任务变得挺顺的,真心感谢他们! 这次与[1]中的不同之处在于: (1)播放和暂停按钮集成在<MediaElement>的点击事件之中,点一下是播放,再点一下是暂停 (2)加入了微软官方改写的粒子特效 (3)加上了自己琢磨的按钮旋转效果,以及按钮淡出popup效果 (4)进度条改善美观 二.代码       前台: 1 <Window 2 xmlns="http://schemas.