Search code examples
javareportjxls

How to implement JXLS in Java file?


I want to convert my report into Excel sheet, But I don't know How to implement JXLS in java file. Please suggest me the steps for implementation.

Thanks in advance !


Solution

  • first you need to create the data objects that you want to display in your excel file

    List<Staff> staffs = new ArrayList<Staff>();
    staffs.add(new Staff("Zhang San", 6000D, 3000D));
    staffs.add(new Staff("Li Si", 5000D, 2000D));
    staffs.add(new Staff("Wang Wu", 4000D, 1000D));
    

    then add the data into java.util.Map

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

    then create XLSTransformer object and set the source file, destination file and the map which is holding your data

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

    you need to use <jx:forEach> tag to iterate over the list which is in your map and then you can set the values into your result excel file. Change your excel template file with the following

    enter image description here