Excel Column Width Unit Converter[pixels - excel column width units]

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Excel Column Width Unit Converter[pixels - excel column width units]


   for the style problem, i send it again.

  For one yearbook editing system, which buid the excel file for printing, i have used the poi to generate excel file. The
sheet built should fit the size for paper accurately( +- 2pixels ), like A4 paper or others...
  it s a rough road in finding the converter for Pixels-Excel Column Width Units changing.
  at first(2008.04), i found a solution from the mail list  Excel column widths - an almost complete solution/explanation , and it made me so happy that it make me ride out the storm.
  soon(2008.05), i found it can't meet the requirements in my system. so i set the column width by hand in the excel sheet, and observer it, find the law.and then write a new algorithm to do it ...
  i m so lucky, that, at the end of  the system developing, yesterday, i found the algorithm more accurately and more simply. the column width is muti-256, which 7pixels equals 256 excel column width units, but in the 7 pixels, it has a map(not average increasing):36,73,109,146,182,219,256.
  i don't know if it can do well in the other environment, my computer is xp, and 96dpi. maybe it s useful to someone who do the same job as mine.
  the code:
package name.xio.util.poi;
 * the units converter for excel
 * @author xio[[hidden email]]
public class MSExcelUtil {
 public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
 public static final int UNIT_OFFSET_LENGTH = 7;
 public static final int[] UNIT_OFFSET_MAP = new int[] { 0, 36, 73, 109, 146, 182, 219 };
  * pixel units to excel width units(units of 1/256th of a character width)
  * @param pxs
  * @return
 public static short pixel2WidthUnits(int pxs) {
   short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR * (pxs / UNIT_OFFSET_LENGTH));

   widthUnits += UNIT_OFFSET_MAP[(pxs % UNIT_OFFSET_LENGTH)];

   return widthUnits;
  * excel width units(units of 1/256th of a character width) to pixel units
  * @param widthUnits
  * @return
 public static int widthUnits2Pixel(short widthUnits) {
   int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) * UNIT_OFFSET_LENGTH;
   int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
   pixels += Math.round((float) offsetWidthUnits / ((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));
   return pixels;

Explore the seven wonders of the world

To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]