欢迎访问我的个人网站==》 jiashubing.cn

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()


  原创文章,欢迎转载,转载请注明出处

posted @   贾树丙  阅读(16937)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2013-07-27 动态规划专题
点击右上角即可分享
微信分享提示