Android POI:使用 autoSizeColumn() 时崩溃 [英] Android POI : crash when using autoSizeColumn()

查看:26
本文介绍了Android POI:使用 autoSizeColumn() 时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

autoSizeColumn 方法抛出一个我无法解决的异常:

autoSizeColumn method of POI throws an exception that I didn't manage to resolve :

 "java.lang.ClassNotFoundException: Didn't find class "java.awt.font.FontRenderContext" on path:..." 

出现这个错误

"java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/font/FontRenderContext;"

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

Here my code, the method is called after placing data in the column :

  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;
}

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

Someone have any clue to help me from this ?
Thanks in advance.

推荐答案

您已标记此机器人.在 android 上,大多数(全部?)AWT 类都不可用.但是 POI 需要一个 FontRenderContext 来计算列的大小.

You have tagged this android. On android, most (all?) AWT classes are not available. But POI needs a FontRenderContext to calculate column sizes.

作为一种解决方法,将您对 autoSizeColumn(2) 的调用替换为 setColumnWidth(2, width).width 的近似值可以通过计算该列中显示的最大字符数以一个因子来计算.首先为比例字体尝试一个大约 0.55 * fontSizeInPoints 的值.

As a workaround, replace your call to autoSizeColumn(2) by setColumnWidth(2, width). A approximate value for width can be calculated by counting the maximum number of characters displayed in that column by a factor. Start by trying a value of about 0.55 * fontSizeInPoints for proportional fonts.

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

PS: Next time please provide a complete stack trace and mention the JDK and POI versions that you use.

这篇关于Android POI:使用 autoSizeColumn() 时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆