title: Formula to convert .NET pixels to Excel width in OpenXML format
body: <p>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'm trying to determine the width for a column that is to be Auto-sized. I am getting the number of pixels by measuring the string and then trying to convert that to the column width for OpenXML, which is measured in characters I think.</p>
  
  <p>I've read Microsoft's documentation on how to convert it and tried the formula they suggest, but it isn't even close to being accurate:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.column.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.column.aspx</a></p>
  
  <p>Here's my code using their formula:</p>
  
  <pre><code>    public double GetCharacterWidth(string Text, Font f, Graphics g)
      {
          float MaxDigitWidth = g.MeasureString("0", f).Width;
          float Pixels = g.MeasureString(Text, f).Width;
  
          return ((Pixels - 5) / MaxDigitWidth * 100 + 0.5) / 100;
      }
  </code></pre>
  
  <p>Any ideas?</p>