天天看點

導出多個工作表的電子表檔案

寫電子表内容:

public byte[] writeMapData(Map<String,List<Map<String,Object>>> data) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        WritableWorkbook wwb = Workbook.createWorkbook(os);
        int sheetIndex=0;
        for (Map.Entry<String, List<Map<String, Object>>> s :
                data.entrySet()) {
            WritableSheet writableSheet = wwb.createSheet(s.getKey(),sheetIndex);
            List<Map<String,Object>> sheetData = s.getValue();
            if(sheetData.size()==0){
                continue;
            }

            //region  取列
            List<String> colList = new ArrayList<>();
            Map<String, Object> mapCol = sheetData.get(0);
            Iterator iterator = mapCol.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                colList.add(entry.getKey().toString());
            }
            //endregion

            //region    寫資料
            int rowIndex = 0;
            int colIndex = 0;

            for (String str :
                    colList) {
                WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 10, WritableFont.BOLD, false);
                WritableCellFormat wcf = new WritableCellFormat(wf);
                Label label = new Label(colIndex, rowIndex, str, wcf);
                writableSheet.addCell(label);
                colIndex++;
            }

            rowIndex++;

            for (Map<String, Object> obj :
                    sheetData) {
                colIndex = 0;
                for (String str :
                        colList) {
                    String value = "";
                    if (obj.get(str) != null) {
                        value = obj.get(str).toString();
                    }
                    Label label = new Label(colIndex, rowIndex, value);
                    writableSheet.addCell(label);
                    colIndex++;
                }
                rowIndex++;
            }
            //endregion

            sheetIndex++;
        }
        wwb.write();
        wwb.close();
        return os.toByteArray();
    }      
@RequestMapping("/downloadTeacherMb")
    @LoginLimit(limit = false)
    public void downloadTeacherMb(HttpServletResponse response) throws Exception {
        List list1 = codeMapper.listAll(1);
        List list2 = codeMapper.listAll(2);
        List list3 = codeMapper.listAll(3);
        List<Map<String, Object>> listT = new ArrayList<>();
        Map<String, Object> mapT = new LinkedHashMap<>();
        mapT.put("姓名", "");
        mapT.put("barcode", "");
        mapT.put("性别", "");
        mapT.put("身份證号碼", "");
        mapT.put("聯系電話", "");
        mapT.put("民族代碼", "");
        mapT.put("政治面貌代碼", "");
        mapT.put("學曆代碼", "");
        mapT.put("畢業學校", "");
        mapT.put("出生年月", "");
        mapT.put("聯系位址", "");
        listT.add(mapT);

        Map<String, List<Map<String, Object>>> map = new LinkedHashMap<>();
        map.put("教師資料表", listT);
        map.put("學曆代碼對照表", list1);
        map.put("民族代碼對照表", list2);
        map.put("政治面貌代碼對照表", list3);

        Excel excel = new Excel();
        byte[] data = excel.writeMapData(map);

        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("教師導入模版", "UTF-8") + ".xls");

        OutputStream stream = response.getOutputStream();
        stream.write(data);
        stream.flush();
        stream.close();
    }