Android POI:使用autoSizeColumn()时崩溃

Pau*_*ger 8 java android apache-poi

autoSizeColumn POI的方法抛出了我无法解决的异常:

 "java.lang.ClassNotFoundException: Didn't find class "java.awt.font.FontRenderContext" on path:..." 
Run Code Online (Sandbox Code Playgroud)

有这个错误

"java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/font/FontRenderContext;"
Run Code Online (Sandbox Code Playgroud)

这是我的代码,在将数据放入列后调用该方法:

  private boolean saveExcelFile(Context context, String fileName) {

    if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
        Log.e("ExcelLog", "Storage not available or read only");
        return false;
    }

    boolean success = false;

    Cell c;

    Workbook wb = new HSSFWorkbook();

    CellStyle cs = wb.createCellStyle();
    cs.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    Sheet sheet1;
    sheet1 = wb.createSheet("Historique du "+date);

    MultiFormatWriter writer = new MultiFormatWriter();
    Bitmap ImageBitmap;

    CreationHelper helper = wb.getCreationHelper();

    Drawing drawing = sheet1.createDrawingPatriarch();

    Row row = sheet1.createRow(0);

    c = row.createCell(0);
    c.setCellValue("Quantité");
    c.setCellStyle(cs);

    c = row.createCell(1);
    c.setCellValue("Code barre");
    c.setCellStyle(cs);

    c = row.createCell(2);
    c.setCellValue("Association");
    c.setCellStyle(cs);

    int m = 0;
    for(int k=0;k<ExpListDistribs.size();++k) {
        int l = 0;
        for (int n = 0; n < ExpListDistribs.get(k).getDistribs().size()*2; n++) {
            while(l<ExpListDistribs.get(k).getDistribs().size()) {
                if (isOdd(m)) {
                    row = sheet1.createRow(m + 1);
                    row.setHeight((short)800);
                    m++;
                    c = row.createCell(0);
                    c.setCellValue("");
                } else {
                    ClientAnchor anchor = helper.createClientAnchor();
                    row = sheet1.createRow(m + 1);
                    row.setHeight((short)1200);
                    c = row.createCell(0);
                    c.setCellStyle(style);
                    c.setCellValue(ExpListDistribs.get(k).getDistribs().get(l).getQuantite()+" kg");
                    c = row.createCell(2);
                    c.setCellStyle(style);
                    c.setCellValue(ExpListDistribs.get(k).getAssociation());

                    l++;
                    int t = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                    anchor.setCol1(1);
                    anchor.setCol2(2);
                    anchor.setRow1(m + 1);
                    anchor.setRow2(m + 2);
                    m++;
                    drawing.createPicture(anchor, t);
                }
            }
        }
    }

    sheet1.setColumnWidth(0, (15 * 200));
    sheet1.setColumnWidth(1, (15 * 800));
    sheet1.autoSizeColumn(2);

    File file = new File(context.getExternalFilesDir(null), fileName);
    FileOutputStream os = null;

    try {
        os = new FileOutputStream(file);
        wb.write(os);
        Log.w("FileUtils", "Writing file" + file);
        success = true;
    } catch (IOException e) {
        Log.w("FileUtils", "Error writing " + file, e);
    } catch (Exception e) {
        Log.w("FileUtils", "Failed to save file", e);
    } finally {
        try {
            if (null != os)
                os.close();
        } catch (Exception ignored) {
        }
    }
    Toast.makeText(getApplicationContext(), "Success",
            Toast.LENGTH_LONG).show();
    return success;
}
Run Code Online (Sandbox Code Playgroud)

有人有任何线索可以帮助我吗?
提前致谢.

Axe*_*xel 6

您已标记此android。在android上,大多数(全部?)AWT类不可用。但是POI需要a FontRenderContext来计算列大小。

作为一种变通方法,代替您的来电autoSizeColumn(2)通过setColumnWidth(2, width)width可以通过将该行中显示的最大字符数乘以一个因子来计算的近似值。首先尝试0.55 * fontSizeInPoints为比例字体设置大约的值。

PS:下次,请提供完整的堆栈跟踪,并提及您使用的JDK和POI版本。