execl代碼導出模板

 @GetMapping("/template")
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
//        String parameter = req.getParameter("arg");
//        if ("chfpi".equals(parameter)) {

            HSSFWorkbook workbook = null;
            String fileName = "chfp報考管理模板"+ ".xls"; // Excel文件名
            OutputStream os = null;
            workbook = buildExcelDocument();
            resp.reset();// 清空輸出流
            // web瀏覽通過MIME類型判斷文件是excel類型
            resp.setHeader("Content-type",
                    "application/vnd.ms-excel;charset=UTF-8");

            // 對文件名進行處理。防止文件名亂碼
            // Content-disposition屬性設置成以附件方式進行下載
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
            os = resp.getOutputStream();// 取得輸出流
            workbook.write(os);
//        }
    }

    public HSSFWorkbook buildExcelDocument() {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 12); // 設置字體的大小
        font.setFontName("宋體"); // 設置字體的樣式,如:宋體、微軟雅黑等
        font.setItalic(false); // 斜體true爲斜體
        HSSFCellStyle style = workbook.createCellStyle();
        Sheet sheetHome = workbook.createSheet("首頁");

        // 案例展示2行
        String[][] exampleStr = new String[][] {
                { "eg:", "姓名", "手機號碼", "考試批", "考試次", "金額" },
                { "", "張三", "185xxxxxxxx", "18年6月", "補考一批", "200" },
                { "", "李四", "185xxxxxxxx", "18年6月", "補考一批", "200" } };
        font.setBold(false);
        font.setColor(HSSFColor.BLACK.index);
        style.setFont(font);
        for (int i = 0; i < exampleStr.length; i++) {
            Row r = sheetHome.createRow(i);
            String[] strings = exampleStr[i];
            for (int j = 0; j < strings.length; j++) {
                Cell c = r.createCell(j, Cell.CELL_TYPE_STRING);
                c.setCellStyle(style);
                c.setCellValue(strings[j]);
            }
        }

        // 提示信息
        HSSFFont fontRemind = workbook.createFont();
        fontRemind.setFontHeightInPoints((short) 12); // 設置字體的大小
        fontRemind.setFontName("宋體"); // 設置字體的樣式,如:宋體、微軟雅黑等
        fontRemind.setItalic(false); // 斜體true爲斜體
        HSSFCellStyle styleRemind = workbook.createCellStyle();
        fontRemind.setBold(false); // 對文中進行加粗
        fontRemind.setColor(HSSFColor.RED.index); // 設置字體的顏色
        styleRemind.setFont(fontRemind);

        Row rowHead = sheetHome.createRow(3);
        Cell cellHead = rowHead.createCell(0, Cell.CELL_TYPE_STRING);
        cellHead.setCellStyle(styleRemind);
        cellHead.setCellValue("姓名、手機號、考試批、考試次、金額爲必填項,不能爲空!\r\n考試批格式爲xx年xx月");

        rowHead = sheetHome.createRow(4);
        cellHead = rowHead.createCell(0, Cell.CELL_TYPE_STRING);
        cellHead.setCellStyle(styleRemind);
        cellHead.setCellValue("本頁爲案列展示頁,請跳轉至第二頁進行操作!");

        /*********************************** Sheet第二頁 ********************************************/
        Sheet sheetInput = workbook.createSheet("客戶信息錄入");
        // HSSFDataValidation setGenderValid = ExcelUtils.setGenderValid();
        Row rowInput = sheetInput.createRow(0);
        String[] title = new String[] { "姓名", "手機號碼", "考試批", "考試次", "金額" };
        for (int i = 0; i < title.length; i++) {
            Cell c = rowInput.createCell(i, Cell.CELL_TYPE_STRING);
            c.setCellStyle(style);
            c.setCellValue(title[i]);
        }
        // sheetInput.addValidationData(setGenderValid);
        return workbook;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章