Java實現動態的匯出Excel表功能--用form表單提交

月明aniu發表於2020-11-15

前端程式碼:
1,在這裡插入圖片描述
2,在這裡插入圖片描述
3,這是用form表單提交!在這裡插入圖片描述

在這裡插入圖片描述
4,在這裡插入圖片描述
後端程式碼實現:
控制器
@PostMapping(value = “/ExcelExport”)
public void ExcelExport(HttpServletRequest request, String id, HttpServletResponse response) {

    String token = request.getParameter("token");
    String account = JwtUtil.getClaim(token, SecurityConsts.ACCOUNT);
    String username = JsonUtil.stringKey(account, "username");
    Date date = JwtUtil.getClaim(token).getExpiresAt();
    Long realExpire = date.getTime() - System.currentTimeMillis();
    if (realExpire < 1200000) {

    } else {
        String id_str = request.getParameter("id");
        memberService.ExcelExport(request, id, response, excel_path);
    }
    FileOutput fileOutput = new FileOutput();
    fileOutput.setUrlStr("d:/");
    fileOutput.setFileName("黨員基礎資訊表.xls");

}

業務層
void ExcelExport(HttpServletRequest request, String id, HttpServletResponse response, String excel_path);

實現類
@Override
public void ExcelExport(HttpServletRequest request, String id, HttpServletResponse response, String excel_path) {

    String excel_title[] = request.getParameterValues("excel_title");
    try {
        List<M_member_Vo_1> list_member = baseMapper.listMember(id);
        List<ExcelExportEntity> colList = new ArrayList<>();
        ExcelExportEntity colEntity = null;
        for (int i = 0; i < excel_title.length; i++) {
            String[] dy = excel_title[i].split(",");
            colEntity = new ExcelExportEntity(dy[1], dy[0]);
            colList.add(colEntity);
        }
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (M_member_Vo_1 m_member_Vo : list_member) {
            Map<String, Object> valMap = new HashMap<String, Object>();
            valMap.put("member_name", m_member_Vo.getMember_name());
            valMap.put("member_sex", m_member_Vo.getMember_sex());
            valMap.put("member_birthday", m_member_Vo.getMember_birthday());
            valMap.put("member_mobile", m_member_Vo.getMember_mobile());
            valMap.put("member_card", m_member_Vo.getMember_card());
            valMap.put("member_dw", m_member_Vo.getMember_dw());
            valMap.put("member_derp", m_member_Vo.getMember_derp());
            valMap.put("member_xzzw", m_member_Vo.getMember_xzzw());
           
            list.add(valMap);
        }
        String fileName = "";
        fileName = "黨員基本資訊表"+".xls";
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("黨員基礎資訊表", "黨員資訊"), colList,
                list);
        FileOutputStream fos = new FileOutputStream(excel_path + fileName);
        workbook.write(fos);
        fos.close();

        FileUtil.downloadFile(excel_path, fileName, response);


    } catch (Exception e) {
        e.printStackTrace();
    }


}

Mapper層
@Select("…")
List<M_member_Vo> listMember(String member_org);

Vo類:
在這裡插入圖片描述

本地配置Excel:
file.Excel.path=D:/
Linux配置:
#黨員基本檔案Excel下載位置----上傳到伺服器這個路徑必須是linux上所擁有的路徑!!!要不然下載不了Excel檔案!!!
file.Excel.path=/data

pom.xml-----忽略!!!加粗樣式

相關文章