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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值