Maps and Jxls - process different excel sheets sep

2019-08-01 11:13发布

问题:

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.

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.

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

回答1:

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.

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).

I'm open to any suggestion.