Poi precisely sets the row height and column width of Excel

Source: Internet
Author: User

Excel does not have the same row height and column width units.

The 1,excel column height is in points, and the Apache POI line height Unit is twips (TWIP):

1 inch = 72 lbs = 25.4 mm = 1440 twips
1 lbs = 0.353 mm = 20 twips

Row height of =excel in poi *20

Line height in Excel =poi/20

Here's the way to say the pixel line-wrapping:

DPI = The number of pixels that can be displayed within 1 inches. Usually the computer screen is 96DPI, iphone4s screen is 326DPI, ordinary laser black and white printer is 400DPI

To calculate the POI row height or Excel line height, first convert it to inches, and then multiply the small dpi to get the pixels.

Pixel = (lb/72) *dpi

Pixel = (line height of Excel/72) *dpi

Pixel = (Row height/20/72 in poi) *dpi

Line height of Excel = Pixel/dpi*72

Row height in poi = pixel/dpi*72*20

For example, on a computer screen, Excel has a default row height of 13.5, (13.5/72) *96=18 pixels, and 18 pixels is really displayed in Excel.

2,excel column width is the number of characters

(2014-08-18 Note: A detailed description of the width calculation of excel in this article how to calculate the width of Excel?) )

In versions above excel2003, you can create a blank XLS document and pull the first column width to 10. Then enter 1234567890 in cell A1 to see that the cell fits exactly the 10 characters. The width of a character is obtained by measuring the average width of 1234567890 of these 10 characters in the default font (usually the Arial 11th, depending on the version).

Just remember that the width of one character is 8 pixels (2013-10-29 Note: The width of a character depends on the first font size in the Excel file, not necessarily 8 pixels).

The actual pixel width of a cell is also preceded by a blank edge of 2 pixels on the basis of (the width of the number of characters * default character). In addition, a pixel is superimposed between the characters, which is also subtracted when calculating:

(2013-10-29 Note: The margin of a character is not necessarily just 2 pixels, it is related to the font)

pixels = 2 pixels blank + (the number of characters * The width of the default character) + 2 pixels Blank-(number of characters-1)

To tidy up, the formula becomes:

Pixel = 5 + (number of characters * (width of default character-1))

Substituting default character width 8:

Pixel = 5 + (number of characters * 7)

The character width algorithm in POI is:

Double width = (Number of characters * (character width-1) + 5)/(character width-1) * 256;

It is then rounded to an integer.

Poi precisely sets the row height and column width of Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.