JxlsUtils.java 4.45 KB
Newer Older
frank.xa.zhang's avatar
frank.xa.zhang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
package pwc.taxtech.atms.common.util;

import org.jxls.area.Area;
import org.jxls.builder.AreaBuilder;
import org.jxls.builder.xls.XlsCommentAreaBuilder;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.transform.poi.WritableCellValue;
import org.jxls.transform.poi.WritableHyperlink;
import org.jxls.util.TransformerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import pwc.taxtech.atms.dto.export.ExportDto;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Jxls导出Excel工具
 * author kevin
 */
public class JxlsUtils {
    private static final Logger log = LoggerFactory.getLogger(JxlsUtils.class);
    public  static String sheetName = "汇总表";

    public static void toPackageOs(HttpServletResponse response , String fileName) {
        ResponseUtil.response(response, fileName, null);
    }


    /**
     * 生成模板输入流
     * @param temPath
     * @return
     * @throws Exception
     */
    public static InputStream toPackageIn(String temPath){
        try {
            return new ClassPathResource((temPath)).getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }


    public static <T> void export(ExportDto exportDto){
        toPackageOs(exportDto.getResponse(), exportDto.getFileName());
        try {
            exportExcel(exportDto.getList(), exportDto.getResponse().getOutputStream(), toPackageIn(exportDto.getTemplateUrl()), exportDto.getRelation(),exportDto.getSheetNameTemp());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static <T> void   exportExcel(List<T> list, ServletOutputStream os ,
                                         InputStream in, Object relation ){
        exportExcel( list,  os ,in,  relation , null);
    }
    public static <T> void   exportExcel(List<T> list, ServletOutputStream os ,
                                       InputStream in, Object relation, String sheetName1 ){
        String sn = sheetName;
        if(sheetName1 != null)
            sn = sheetName1;

        long exportExcelBegin = System.currentTimeMillis();
        log.warn("exportExcel begin: " + exportExcelBegin);
        Context context = new Context();
        context.putVar("list", list);
        if(relation != null) context.putVar("relation", relation);

        Map<String , Object> myFunction = new HashMap<String , Object>();

        myFunction.put("util", new JxlsUtils());

        Transformer trans = TransformerFactory.createTransformer(in, os);

        JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) trans.getTransformationConfig().getExpressionEvaluator();

        evaluator.getJexlEngine().setFunctions(myFunction);

        //载入模板、处理导出

        AreaBuilder areaBuilder = new XlsCommentAreaBuilder(trans);

        List<Area> areaList = areaBuilder.build();
        //"汇总表!A1"

        areaList.get(0).applyAt(new CellRef( sn + "!A1"), context);

        try {
            trans.write();
            //释放资源

            os.flush();

            os.close();

            in.close();
            long endExcel = System.currentTimeMillis();
            log.warn("exportExcel begin: " + endExcel);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //返回大的数

    public Integer max(Integer x , Integer y){

        return x > y ? x : y;

    }



    //给金额前面显示个货币符号

    public Object formatMoney(Object a){

        Object result = null;

        if(a != null){

            return "¥" + a;

        }

        return result;

    }



    //超链接

    public WritableCellValue myHyperlink(String address, String title) {

        return new WritableHyperlink(address, title);

    }




    private Map<String,Integer> countMap = new HashMap<>(); //用于jxls的foreach循环计数
    //为Excel添加序号
    public Integer count(String var){
        if (var == null) return null;
        if(countMap.containsKey(var)){
            Integer t = countMap.get(var);
            t += 1;
            countMap.replace(var,t);
            return t;
        }else{
            countMap.put(var,1);
        }
        return 1;
    }





}