POI獲取單元格的寬和高


獲取單元格的寬,即獲取所在列的寬。先獲取單元格所在的sheet:cell.getSheet()

sheet.getColumnWidth( cell.getColumnIndex() )  單位不是像素,是1/256個字符寬度
sheet.getColumnWidthInPixels( cell.getColumnIndex() )  單位是像素

獲取單元格的高,即獲取所在行的高。先獲取單元格所在的row:cell.getRow()

row.getHeight()
row.getHeightInPoints()
這兩個屬性的區別在於HeightInPoints的單位是點,而Height的單位是1/20個點,所以Height的值永遠是HeightInPoints的20倍

如果說,我非要獲取到單元格行高的像素值,該怎么辦呢?俺有個不太精確的方法。

網上有大神總結:
  EXCEL列高度的單位是磅,Apache POI的行高度單位是緹(twip)
  DPI = 1英寸內可顯示的像素點個數。通常電腦屏幕是96DPI, IPhone4s的屏幕是326DPI, 普通激光黑白打印機是400DPI
  要計算POI行高或者Excel的行高,就先把它行轉換到英寸,再乘小DPI就可以得到像素
  像素= (Excel的行高度/72)*DPI

所以獲取行高的像素值的方法就是: (row.getHeightInPoints() / 72) * 96

 

四個方法對應的參考文檔如下:

前兩個,參考文檔鏈接:https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Sheet.html

getColumnWidth int getColumnWidth(int columnIndex)
    get the width (in units of 1/256th of a character width )
    Character width is defined as the maximum digit width of the numbers 0, 1, 2, ... 9 as rendered using the default font (first font in the workbook)
Parameters:
    columnIndex - - the column to get (0-based)
Returns:
    width - the width in units of 1/256th of a character width

 

getColumnWidthInPixels float getColumnWidthInPixels(int columnIndex)
    get the width in pixel
    Please note, that this method works correctly only for workbooks with the default font size (Arial 10pt for .xls and Calibri 11pt for .xlsx). If the default font is changed the column width can be streched
Parameters:
    columnIndex - - the column to set (0-based)
Returns:
    width in pixels

  

后兩個,參考文檔鏈接:https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.html

getHeight short getHeight()
    Get the row's height measured in twips (1/20th of a point). If the height is not set, the default worksheet value is returned, See Sheet.getDefaultRowHeightInPoints()
Returns:
    row height measured in twips (1/20th of a point)

 

getHeightInPoints float getHeightInPoints()
    Returns row height measured in point size. If the height is not set, the default worksheet value is returned, See Sheet.getDefaultRowHeightInPoints()
Returns:
    row height measured in point size
See Also:
    Sheet.getDefaultRowHeightInPoints()


  原創文章,歡迎轉載,轉載請注明出處


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM