java Excel 導出 封裝

接 上一篇導出博客 : http://www.javashuo.com/article/p-eyhmwhqc-ma.htmljava

 

/**
 * @Title:數據導出
 * @Description:
 * @Created by yangjie on 2019/4/12/10:13.
 */
@Service
public class ExcelService {

    public void export(String fileName, String[] title, String[] params, List list, HttpServletResponse response) throws Exception {
        List<Object[]> dataList = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            Object[] objs = new Object[params.length];
            for (int j = 0; j < params.length; j++) {
                objs[j] = getGetMethod(list.get(i), params[j]).toString();
                if (list.get(i).getClass().getDeclaredField(params[j]).getGenericType().toString().equals("class java.util.Date")) {
                    if (ValidationUtil.isNotBlank(objs[j])) {
                        Date time = new Date(objs[j].toString());
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        objs[j] = sdf.format(time);
                    }
                }
                dataList.add(i, objs);
            }
        }
        ExportToExcelUtil.exportExcel(fileName, title, dataList, response);
    }

    /**
     * @Title:
     * @TODO:
     * @Created By MrYang on 2019/4/12/16:54
     */
    public static Object getGetMethod(Object ob, String name) throws Exception {
        Method[] m = ob.getClass().getMethods();
        for (int i = 0; i < m.length; i++) {
            if (("get" + name).toLowerCase().equals(m[i].getName().toLowerCase())) {
                return m[i].invoke(ob) == null ? "" : m[i].invoke(ob);
            }
        }
        return "";
    }

}

 

@RequestMapping("/export")
public void export(OrderVO, HttpServletResponse response) {
    Log.logger.info("導出數據 入參信息" + JSONObject.toJSONString(vo));
    String fileName = "訂單列表";
   
    String[] title =
            {"編號","訂單編號","借款人姓名","借款人手機號","放款金額","分期數",
            "資金渠道","歸屬渠道","代理人","進件時間","放款時間"};
    String[] params =
            {"orderNo","orderNo","borrowerName","borrowerMobileNo","loanAmount","stageNum",
             "channelName","mcName","agentName","addTime","loanTime"};

    List list = orderService.queryList(vo);
    
    try {
        excelService.export(fileName,title,params, list ,response);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
相關文章
相關標籤/搜索