Jxls - Transformer类是org.jxls.transform.poi.PoiTransformer

时间:2016-07-03 13:54:17

标签: java jxls

我正在努力将Java程序的结果导出到excel表。我正在使用Jxls。 它在我使用XLSTransforme r时有效,但不执行excel模板的计算。为了执行这些计算,我尝试使用Transformer class,但我收到了一条调试消息:DEBUG org.jxls.util.TransformerFactory - Transformer class is org.jxls.transform.poi.PoiTransformer,而我的导入与Jxls sourceforge页面上的示例相同。

private void exportData(File file) throws ParseException, IOException {

    AreaBuilder areaBuilder = new XlsCommentAreaBuilder();

    int o = 0;
    List<ListData2> rs2 = new ArrayList<ListData2>();
    ListData2 s2 = new ListData2(item[o], rep[o], justefaux[o], tempsrep[o]);
    for(int i=0; i<tableau.getRowCount(); i++){
        item[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 2).toString();
        rep[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 3).toString();
        justefaux[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 4).toString();
        tempsrep[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 5).toString();
        ListData2 temp = new ListData2(item[i], rep[i], justefaux[i], tempsrep[i]);
        s2.add(temp);
        rs2.add(s2);
    }



    InputStream in = IdGenre.class.getResourceAsStream("/xlsTemplates/IdGenre/IdGenreTemplate.xlsx");
 try
    {OutputStream out = new FileOutputStream("d:/IdGenre.xlsx");

     Context context = new Context();
     context.putVar("rs2", rs2);

     Transformer transformer = TransformerFactory.createTransformer(in, out);
     JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) transformer.getTransformationConfig().getExpressionEvaluator();
     Map<String, Object> functionMap = new HashMap<>();
     functionMap.put("joiner", new ListUtil());
     evaluator.getJexlEngine().setFunctions(functionMap);

     areaBuilder.setTransformer(transformer);
     List xlsAreaList = areaBuilder.build();
     Iterator iterator = xlsAreaList.iterator();

     while (iterator.hasNext()) {
         Area xlsArea = (Area) iterator.next();
         xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), context);
     }
     transformer.write();

    }
    finally
    {
        in.close();
    }
}


public static class ListUtil {

    public String join(List list) {
        StringBuilder builder = new StringBuilder();
        for (Object o : list) {
            if (builder.length() != 0) {
                builder.append(" / ");
            }
            builder.append(o);
        }
        return builder.toString();
    }
}

堆栈追踪:

15:44:44.975 [AWT-EventQueue-0] DEBUG org.jxls.util.TransformerFactory - Transformer class is org.jxls.transform.poi.PoiTransformer
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.font.FontDesignMetrics$MetricsKey.init(Unknown Source)
at sun.font.FontDesignMetrics.getMetrics(Unknown Source)
...

当我尝试使用JxlsHelper类时,我得到了相同的堆栈跟踪:

private void exportData(File file) throws ParseException, IOException {

    int o = 0;
    List<ListData2> rs2 = new ArrayList<ListData2>();
    ListData2 s2 = new ListData2(item[o], rep[o], justefaux[o], tempsrep[o]);
    for(int i=0; i<tableau.getRowCount(); i++){
        item[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 2).toString();
        rep[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 3).toString();
        justefaux[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 4).toString();
        tempsrep[i]=((DataIdGenre) tableau.getModel()).getValueAt(i, 5).toString();
        ListData2 temp = new ListData2(item[i], rep[i], justefaux[i], tempsrep[i]);
        s2.add(temp);
        rs2.add(s2);
    }



    InputStream in = IdGenre.class.getResourceAsStream("/xlsTemplates/IdGenre/IdGenreTemplate.xlsx");
 try
    {OutputStream out = new FileOutputStream("d:/IdGenreYOLO.xlsx");

     Context context = new Context();
     context.putVar("rs2", rs2);
     JxlsHelper.getInstance().processTemplate(in, out, context);

    }
    finally
    {
        in.close();
    }
}

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

你得到的调试信息

15:44:44.975 [AWT-EventQueue-0] DEBUG org.jxls.util.TransformerFactory - Transformer class is org.jxls.transform.poi.PoiTransformer

只是一条信息消息,表明将使用Jxls PoiTransformer生成Excel输出。

真正的错误与Jxls无关,但是与你接下来的NullPointerException有关

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.font.FontDesignMetrics$MetricsKey.init(Unknown Source)
at sun.font.FontDesignMetrics.getMetrics(Unknown Source)
...

所以你应该进一步了解堆栈跟踪,看看你为什么在sun.font.FontDesignMetrics.getMetrics()获得NullPointerException

<强>更新

如果您在使用Excel时无法计算公式,则可以使用POI Formula Evaluation中说明的技巧。例如,您可以在Jxls处理完成后强制Excel使用workbook.setForceFormulaRecalculation(true)重新计算公式。 您可以通过调用PoiTransformer getWorkbook()方法获取工作簿实例。