首页 > 解决方案 > Apache POI 获取字体指标

问题描述

我想在 excel 中自动调整列的大小,但又不会花费太多的性能。Apache POI 的内置自动调整大小非常慢(几小时后没有完成 100 万行)。为了节省性能,我只想近似单元格宽度,但为此我需要字体度量。

Apache POI 有一个名为 的类FontDetails,但它不能单独工作。该类StaticFontMetrics似乎是实际加载指标的类,但它不是公开的。但即使将受保护的代码复制到我的工作区并使其可访问,它也无法加载字体指标。

我怎样才能得到这些指标?总是会java.awt.FontMetrics返回一个准确的结果?

编辑:我在尝试获取字体度量时得到的堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: The supplied FontMetrics doesn't know about the font 'Calibri', so we can't use it. Please add it to your font metrics file (see StaticFontMetrics.getFontDetails
at ourpackagestructure.apachepoi.FontDetails.create(FontDetails.java:106)
at ourpackagestructure.apachepoi.StaticFontMetrics.getFontDetails(StaticFontMetrics.java:94)

标签: javaexcelapache-poi

解决方案


Apache poi使用AttributedStringTextLayout以特殊字体获取文本的边界。

因此,只要整个列使用相同的字体,最好的方法是首先获取应存储在该列中的最长字符串。然后使用 . 获取该字体中该字符串的宽度java.awt.font.TextLayout。然后将其设置为列宽。

请注意,in 的列宽以Excel默认字符宽度的 1/256 为单位设置。因此,除了该字体中字符串的宽度(以像素为单位)之外,您还需要默认字符的宽度来计算Excel列宽。

例子:

import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
import java.awt.geom.Rectangle2D;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.SheetUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ApachePoiGetStringWidth {

 public static void main(String args[]) throws Exception {

  String testString = "Lorem ipsum semit dolor";
  String fontName = "Calibri";
  short fontSize = 24;
  boolean italic = true;
  boolean bold = false;

  Workbook workbook = new XSSFWorkbook();
  Font font = workbook.createFont();
  font.setFontHeightInPoints(fontSize);
  font.setFontName(fontName);
  font.setItalic(italic);
  font.setBold(bold);
  CellStyle style = workbook.createCellStyle();
  style.setFont(font);

  AttributedString attributedString = new AttributedString(testString);
  attributedString.addAttribute(TextAttribute.FAMILY, font.getFontName(), 0, testString.length());
  attributedString.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints());
  if (font.getBold()) attributedString.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD,  0, testString.length());
  if (font.getItalic()) attributedString.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE,  0, testString.length());

  FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);

  TextLayout layout = new TextLayout(attributedString.getIterator(), fontRenderContext);
  Rectangle2D bounds = layout.getBounds();
  double frameWidth = bounds.getX() + bounds.getWidth();

System.out.println(frameWidth);

  Sheet sheet = workbook.createSheet();
  Row row = sheet.createRow(2);
  Cell cell = row.createCell(2);
  cell.setCellValue(testString);
  cell.setCellStyle(style);

  int defaultCharWidth = SheetUtil.getDefaultCharWidth(workbook);
  sheet.setColumnWidth(2, (int)Math.round(frameWidth / defaultCharWidth * 256));

  try (java.io.FileOutputStream out = new java.io.FileOutputStream("Excel.xlsx")) {
   workbook.write(out);
  }

  workbook.close();

 }
}

推荐阅读