java – Android POI:使用autoSizeColumn()时崩溃

前端之家收集整理的这篇文章主要介绍了java – Android POI:使用autoSizeColumn()时崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

有这个错误

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

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

@H_502_5@ 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;kFailed to save file",e); } finally { try { if (null != os) os.close(); } catch (Exception ignored) { } } Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show(); return success; }

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

最佳答案
你已经标记了这个android.在Android上,大多数(所有?)AWT类都不可用.但POI需要FontRenderContext来计算列大小.

解决此问题,请使用setColumnWidth(2,width)替换对autoSizeColumn(2)的调用.可以通过计算该行中显示的最大字符数来计算宽度的近似值.首先尝试使用约0.55 * fontSizeInPoints的值作为比例字体.

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

猜你在找的Android相关文章