地图和Jxls - 分别使用XLSTransformer处理不同的Excel表单 [英] Maps and Jxls - process different excel sheets separately with XLSTransformer

查看:203
本文介绍了地图和Jxls - 分别使用XLSTransformer处理不同的Excel表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张有两张表格的excel模板,我想通过 XLSTransformer 来填充。这两张纸的数据是不同的(不同长度的列表,其中一张是表格的结果 - 见下面的代码),这意味着我不能通过一张地图传递它们。
我试过两张地图:

I have an excel template with two sheets that I want to populate through XLSTransformer. The data are different for the two sheets (list of different lenghts, with one taking results of a table - see code below), meaning that I cannot pass them through one map. I've tried with two maps :

     //for the first excel sheet     
     Map<String, List<ListData>> beanParams = new HashMap<String, List<ListData>>();
     beanParams.put("rs", rs); 

     //for the second one  
     Map<String, List<DetailResult>> beanParams2 = new HashMap<String, List<DetailResult>>();
     beanParams2.put("detRes", detRes); 

     XLSTransformer former = new XLSTransformer();
     former.transformXLS(srcFilePath, beanParams, destFilePath);
     former.transformXLS(srcFilePath, beanParams2, destFilePath);

列表如下所示:

The lists look like this :

    List<Results> rs = new ArrayList<Results>();
    Results s1 = new Results(compteurFemme, compteurHomme, compteurTot, averageTempsFemme, averageTempsHomme, averageTempsTot);
    rs.add(s1);

    List<ResultsDetails> detRes = new ArrayList<ResultsDetails>();
    for(int i=0; i<tableau.getRowCount(); i++){
        ResultsDetails newRes = new ResultsDetails(item[i], rep[i], justefaux[i], tempsrep[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();
        detRes.add(newRes);
    }

个别地,这两个出口正在各自的工作表上,但是在一起,第二次擦除第一个。

Individually, the two exports are working on the respective sheet, but together, the second erases the first one.

然后尝试使用某种类型的multimap,其中一个键(我放入excel中的那个键)用于两个值

I then try to use some kind of multimap, with one key (the one I put in excel) for two values

     Map<String, List<Object>> hm = new HashMap<String, List<Object>>();
     List<Object> values = new ArrayList<Object>();
     values.add(rs);
     values.add(detRes);
     hm.put("det", values);

     XLSTransformer former = new XLSTransformer();
     former.transformXLS(srcFilePath, hm, destFilePath);

但是我收到一个错误,告诉我数据无法访问。

But I got an error telling me that the datas were inaccessible.

所以我的问题是,有没有办法直接处理使用 XLSTransformer

So my question is, is there a way to deal directly with different sheets when using XLSTransformer ?

推荐答案

好吧,我已经通过一个临时文件提出了一些建议:

Ok, I've come up with something, through a temporary file :

   private void exportDataDet(File file) throws ParseException, IOException, ParsePropertyException, InvalidFormatException {

    List<ResultsDetails> detRes = generateResultsDetails();
    try(InputStream is = IdGenre.class.getResourceAsStream("/xlsTemplates/IdGenre/IdGenre_20-29_+12.xlsx")) {
        try (OutputStream os = new FileOutputStream("d:/IdGenreYOLO.xlsx")) {
            Context context = new Context();
            context.putVar("detRes", detRes);
            JxlsHelper.getInstance().processTemplate(is, os, context);
        }
    }
}


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

    List<Results> rs = generateResults();
 try{
     String srcFilePath = "d:/IdGenreYOLO.xlsx";
     String destFilePath = "d:/IdGenreRes.xlsx";

     Map<String, List<Results>> beanParams = new HashMap<String, List<Results>>();
     beanParams.put("rs", rs);

     XLSTransformer former = new XLSTransformer();
     former.transformXLS(srcFilePath, beanParams, destFilePath);
    }
    finally{
        Path path = Paths.get("d:/IdGenreYOLO.xlsx");
        try{
            Files.delete(path);
        }
        finally{}
    }
}

这可能不是最好的解决方案,甚至更多,因为我必须添加其他不适合现有列表的数据,并且 - 至少现在 - 将通过第二个临时文件完成。

It's probably not the best solution, even more because I have to add other data that could not fit in the two existing lists, and - at least for now - it will be done through a second temp file.

我没有使用相同的方法两次,因为 XLSTransformer 是我能够在excel模板中操作的唯​​一一个我是(我无法修改)。

I haven't use the same method twice, because XLSTransformer was the only one I was able to make operative within the excel template I was given (which I can't modify).

我接受任何建议。

这篇关于地图和Jxls - 分别使用XLSTransformer处理不同的Excel表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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