Apache POI 3.10 autoSizeColumn fails

apache-poi, awt, java

Solution

From the Apache POI QuickGuide entry on "How to adjust column width to fit the contents":

Warning

To calculate column width Sheet.autoSizeColumn uses Java2D classes that throw exception if graphical environment is not available. In case if graphical environment is not available, you must tell Java that you are running in headless mode and set the following system property: java.awt.headless=true

You've said you're running neither in a graphical environment, nor with headless. As the quick guide makes clear, if you want to do graphical things (such as sizing fonts), you must run with either a graphical environment or headless enabled

Problem

``` Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.awt.Font at java.awt.font.TextLayout.singleFont(TextLayout.java:468) at java.awt.font.TextLayout.<init>(TextLayout.java:527) at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:208) at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:386) ``` My env is the following: ``` java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode) ``` nogui and java.awt.headless=true flags are not set.

Original source