<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Calculating column widths in Excel Open XML</title>
	<atom:link href="http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/</link>
	<description>Mathematics. Programming. Entrepreneurship.</description>
	<lastBuildDate>Fri, 27 Jan 2012 12:15:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Vincent</title>
		<link>http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/comment-page-1/#comment-34321</link>
		<dc:creator>Vincent</dc:creator>
		<pubDate>Thu, 06 May 2010 05:53:56 +0000</pubDate>
		<guid isPermaLink="false">http://polymathprogrammer.com/?p=1737#comment-34321</guid>
		<description>Well, Chris, that looks awesome. A person with incentive to do something usually tries harder. I&#039;d say you did a lot of research.

The initial reason for my foray into this was because I had to create an Excel file with predictable column widths. Having automatic resizing column widths wasn&#039;t necessary.

Thanks for sharing your results.</description>
		<content:encoded><![CDATA[<p>Well, Chris, that looks awesome. A person with incentive to do something usually tries harder. I&#8217;d say you did a lot of research.</p>
<p>The initial reason for my foray into this was because I had to create an Excel file with predictable column widths. Having automatic resizing column widths wasn&#8217;t necessary.</p>
<p>Thanks for sharing your results.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/comment-page-1/#comment-34320</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 05 May 2010 15:00:06 +0000</pubDate>
		<guid isPermaLink="false">http://polymathprogrammer.com/?p=1737#comment-34320</guid>
		<description>Hi Vincent

I had to do this in the end because the xlsx files I am interested in are auto generated and should look nice as soon as they are opened so I looked into this a little further and found there are a couple of issues to accurately sizing columns in Excel.

1. Need to use accurate character sizing, which means that instead of using MeasureString you need to use MeasureCharacterRanges, see http://groups.google.com/group/microsoft.public.office.developer.com.add_ins/browse_thread/thread/2fc33557feb72ab4/adaddc50480b8cff?lnk=raot

2. Despite the spec saying to add 5 pixels (1 for border and 2 for each side margin) Excel seems to use 9 - 1 for the border, 5 for the leading space and 3 for the trailing space - I only found this by using the accessibility app. Magnifier and counting the pixels after using Excel to auto fit the columns

Actually I was basing my calculations on underlying font metrics so I don&#039;t actually use either MeasureCharacterRanges or MeasureString.  If anyone is interested in doing this from font metrics then:

Width=Truncate({DesiredWidth}+9/{MaxDigitWidth})/256
{MaxDigitWidth} is an integer rounded to the nearest pixel of any of the 0..9 digits at 96 dpi
{DesiredWidth} is the sum of adding all character widths together where each character width is the width of the character at 96 dpi rounded to the nearest integer.  Note that each character is rounded not the overall sum</description>
		<content:encoded><![CDATA[<p>Hi Vincent</p>
<p>I had to do this in the end because the xlsx files I am interested in are auto generated and should look nice as soon as they are opened so I looked into this a little further and found there are a couple of issues to accurately sizing columns in Excel.</p>
<p>1. Need to use accurate character sizing, which means that instead of using MeasureString you need to use MeasureCharacterRanges, see <a href="http://groups.google.com/group/microsoft.public.office.developer.com.add_ins/browse_thread/thread/2fc33557feb72ab4/adaddc50480b8cff?lnk=raot" rel="nofollow">http://groups.google.com/group/microsoft.public.office.developer.com.add_ins/browse_thread/thread/2fc33557feb72ab4/adaddc50480b8cff?lnk=raot</a></p>
<p>2. Despite the spec saying to add 5 pixels (1 for border and 2 for each side margin) Excel seems to use 9 &#8211; 1 for the border, 5 for the leading space and 3 for the trailing space &#8211; I only found this by using the accessibility app. Magnifier and counting the pixels after using Excel to auto fit the columns</p>
<p>Actually I was basing my calculations on underlying font metrics so I don&#8217;t actually use either MeasureCharacterRanges or MeasureString.  If anyone is interested in doing this from font metrics then:</p>
<p>Width=Truncate({DesiredWidth}+9/{MaxDigitWidth})/256<br />
{MaxDigitWidth} is an integer rounded to the nearest pixel of any of the 0..9 digits at 96 dpi<br />
{DesiredWidth} is the sum of adding all character widths together where each character width is the width of the character at 96 dpi rounded to the nearest integer.  Note that each character is rounded not the overall sum</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent</title>
		<link>http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/comment-page-1/#comment-34319</link>
		<dc:creator>Vincent</dc:creator>
		<pubDate>Wed, 05 May 2010 06:06:48 +0000</pubDate>
		<guid isPermaLink="false">http://polymathprogrammer.com/?p=1737#comment-34319</guid>
		<description>Hi Chris, if you used the simple width method, you&#039;ll definitely hit problems with lower case l&#039;s for example. That&#039;s why it&#039;s called the simple width method. It assumes that there&#039;s a range of characters in the text such that the average width can be used.

I modified my program and tried with 5 l&#039;s, that is &quot;lllll&quot;. The simple width method failed to cover its column. But the method from the spec did its job. It calculated a width wide enough to cover &quot;lllll&quot; with a bit of buffer.

Maybe there&#039;s a way to calculate the exact width when the data string is rendered in the column. But I believe the method from the spec is used to cover the data string comfortably, not exactly.

Remember, this is Excel. A user can easily adjust the column width.</description>
		<content:encoded><![CDATA[<p>Hi Chris, if you used the simple width method, you&#8217;ll definitely hit problems with lower case l&#8217;s for example. That&#8217;s why it&#8217;s called the simple width method. It assumes that there&#8217;s a range of characters in the text such that the average width can be used.</p>
<p>I modified my program and tried with 5 l&#8217;s, that is &#8220;lllll&#8221;. The simple width method failed to cover its column. But the method from the spec did its job. It calculated a width wide enough to cover &#8220;lllll&#8221; with a bit of buffer.</p>
<p>Maybe there&#8217;s a way to calculate the exact width when the data string is rendered in the column. But I believe the method from the spec is used to cover the data string comfortably, not exactly.</p>
<p>Remember, this is Excel. A user can easily adjust the column width.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/comment-page-1/#comment-34317</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Tue, 04 May 2010 16:00:34 +0000</pubDate>
		<guid isPermaLink="false">http://polymathprogrammer.com/?p=1737#comment-34317</guid>
		<description>Hi, Looking at this I didn&#039;t think it should work because it is sizing treating all characters as the same size which they are not (Number of characters * Max Digit Width).  Strangely it seems to work perfectly with your selected string, but if you try it with a string consisting entirely of lower case &quot;l&quot; characters you will see it does not auto size the column correctly at all.

I think the Number of characters value is supposed to be the relative number of zero characters (which you already have as fSimpleWidth), if the average size of the characters you are using is the same as the size of the numeric digit then just the number of characters would equate to the same thing but if the average size is different it is wrong.

I tried the calculation using fSimpleWidth instead of the number of characters but that was also wrong and I&#039;m afraid I don&#039;t know why, one other oddity is that the spec says &quot;Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi)&quot; so it is odd that your data shows width of zero in exactly that font to be 12.544...

Apologies if I&#039;m missing something here</description>
		<content:encoded><![CDATA[<p>Hi, Looking at this I didn&#8217;t think it should work because it is sizing treating all characters as the same size which they are not (Number of characters * Max Digit Width).  Strangely it seems to work perfectly with your selected string, but if you try it with a string consisting entirely of lower case &#8220;l&#8221; characters you will see it does not auto size the column correctly at all.</p>
<p>I think the Number of characters value is supposed to be the relative number of zero characters (which you already have as fSimpleWidth), if the average size of the characters you are using is the same as the size of the numeric digit then just the number of characters would equate to the same thing but if the average size is different it is wrong.</p>
<p>I tried the calculation using fSimpleWidth instead of the number of characters but that was also wrong and I&#8217;m afraid I don&#8217;t know why, one other oddity is that the spec says &#8220;Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi)&#8221; so it is odd that your data shows width of zero in exactly that font to be 12.544&#8230;</p>
<p>Apologies if I&#8217;m missing something here</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tweets that mention Calculating column widths in Excel Open XML &#124; Polymath Programmer -- Topsy.com</title>
		<link>http://polymathprogrammer.com/2010/01/18/calculating-column-widths-in-excel-open-xml/comment-page-1/#comment-6515</link>
		<dc:creator>Tweets that mention Calculating column widths in Excel Open XML &#124; Polymath Programmer -- Topsy.com</dc:creator>
		<pubDate>Wed, 20 Jan 2010 19:02:30 +0000</pubDate>
		<guid isPermaLink="false">http://polymathprogrammer.com/?p=1737#comment-6515</guid>
		<description>[...] This post was mentioned on Twitter by Michael Kiselman, Vincent Tan. Vincent Tan said: I found out that, calculating &quot;best fit&quot; column widths isn&#039;t that hard after all... http://bit.ly/8d9vxU [...]</description>
		<content:encoded><![CDATA[<p>[...] This post was mentioned on Twitter by Michael Kiselman, Vincent Tan. Vincent Tan said: I found out that, calculating &quot;best fit&quot; column widths isn&#39;t that hard after all&#8230; <a href="http://bit.ly/8d9vxU" rel="nofollow">http://bit.ly/8d9vxU</a> [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

