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 !

【EPplus】Column width discrepancy的更多相关文章

  1. 【动态规划】Column Addition @ICPC2017Tehran/upcexam5434

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 A multi-digit column addition is a formula on adding two integers writ ...

  2. 【理解】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 functio ...

  3. 【Flask】Column常用参数

    ### Column常用参数:1. primary_key:设置某个字段为主键.2. autoincrement:设置这个字段为自动增长的.3. default:设置某个字段的默认值.在发表时间这些字 ...

  4. 【CSS】思考和再学习——关于CSS中浮动和定位对元素宽度/外边距/其他元素所占空间的影响

      一.width:auto和width:100%的区别   1.width:100%的作用是占满它的参考元素的宽度.(一般情况下参考元素 == 父级元素,这里写成参考元素而不是父级元素,在下面我会再 ...

  5. 【CSS】元素样式

    1.使用CSS的三种方式: 方式一.通过元素的style属性来设置元素的样式 方式二.在HTML头部标签<head>中通过<link>标签引入一个外部的CSS资源,通常是一个C ...

  6. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

  7. 【Discuz】-QQ互联登陆提示错误信息:Unknown column 'conuintoken' in 'field list'

    提示信息 discuz! Database Error(1054) Unknown column 'conuintoken' in 'field list'REPLACE INTO common_co ...

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

    [168-Excel Sheet Column Title(Excell列标题)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a positive in ...

  9. 【转】Hive 修改 table、column

    表 1.重命名表重命名表的语句如下: ALTER TABLE table_name RENAME TO new_table_name 2.修改表属性: ALTER TABLE table_name S ...

随机推荐

  1. dubbo模块组织方式

    dubbo源码版本:2.5.4 阿里通过maven将dubbo的36个模块组织成了一个项目,各个模块结构如下: -------------------------------------------- ...

  2. 滤镜模糊效果,利用了文字阴影和透明。其实是抄的iSlider官网的

    <!DOCTYPE html> <html> <meta charset="UTF-8"> <meta name="viewpo ...

  3. UE4高级功能--初探超大无缝地图的实现LevelStream

    转自:http://blog.csdn.net/u011707076/article/details/44903223 LevelStream 实现超大无缝地图--官方文档学习 The Level S ...

  4. DataTables warning : Requested unknown parameter '5' from the data source for row 0

    在该项目中我使用了jquery.dataTables.js来作为我的前端数据表格. 表格的官网地址:https://www.datatables.net/ 一.jsp部分代码片段如下: <tab ...

  5. NPO与X7R、X5R、Y5V、Z5U神马的有啥区别

    主要是介质材料不同.不同介质种类由于它的主要极化类型不一样,其对电场变化的响应速度和极化率亦不一样. 在相同的体积下的容量就不同,随之带来的电容器的介质损耗.容量稳定性等也就不同.介质材料划按容量的温 ...

  6. scp指令的学习

    http://www.cnblogs.com/hitwtx/archive/2011/11/16/2251254.html scp是linux中功能最强大的文件传输命令,可以实现从本地到远程以及远程到 ...

  7. [JAVA设计模式]第一部分:接口、抽象类、设计原则

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. Reactivecocoa初级使用

    一直听闻ReactiveCocoa(以下简称RAC)的大名,但始终没有使用.最近时间比较空闲就决定研究一下. 在配置RAC时候遇到了一个小麻烦需要说明本人用cocoapods管理第三方框架,于是按照正 ...

  9. bootstrap学习笔记<二>(标题,段落样式)

    标题.样式:class="h1"~class="h6" bootstrap中重新定义了h1~h6标签,具体差别如下: 在bootstrap中其他任何标签使用cl ...

  10. python利用xmlrpc方式对odoo数据表进行增删改查操作

    # -*- encoding: utf-8 -*- import xmlrpclib #导入xmlrpc库,这个库是python的标准库. username ='admin' #用户登录名 pwd = ...