• java导出excel


    /**
    * 导出excel
    */
    @SuppressWarnings("deprecation")
    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response){

    //查询要导出的list
    //List<Bean> bean= ...service.queryList();
    XSSFWorkbook xsf = new XSSFWorkbook();
    XSSFSheet sheet = xsf.createSheet();
    XSSFCellStyle style = xsf.createCellStyle();
    // 填充
    //style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    // 添加标题颜色
    //style.setFillForegroundColor(HSSFColor.BLUE.index);
    // 获取字体
    XSSFFont font = xsf.createFont();
    // 加粗
    font.setBold(true);
    // 把设置好的字体效果放进样式
    style.setFont(font);
    XSSFRow row = sheet.createRow(0);

    //设置excel标题

    String[] head = {"ID","名称","性别","创建时间"};
    for (int i = 0; i < head.length; i++) {
    row.createCell(i).setCellValue(head[i]);
    row.getCell(i).setCellStyle(style);
    }
    SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    for (int i = 0; i < productList.size(); i++) {
    XSSFRow rowTo = sheet.createRow(i+1);
    rowTo.createCell(0).setCellValue(bean.get(i).getId());
    rowTo.createCell(1).setCellValue(bean.get(i).getName());
    rowTo.createCell(2).setCellValue(bean.get(i).getSex());
    rowTo.createCell(3).setCellValue(bean.get(i).getCreateDate());
    }
    this.excelDownload(xsf, response);
    }

    /**
    * 导出excel lht
    */
    public static void excelDownload(XSSFWorkbook wirthExcelWB,HttpServletResponse response) {
    OutputStream out = null;
    try {
    out = response.getOutputStream();
    //让浏览器识别是什么类型的文件
    response.reset(); // 重点突出
    response.setContentType("application/x-msdownload;charset=UTF-8");
    String file = new String("导出数据.xls".getBytes("utf-8"), "iso-8859-1");//导出数据为要导出的文件的名称
    response.setContentType("application/octet-stream; charset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename="" + file + """);

    wirthExcelWB.write(out);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if( null != out){
    try {
    out.close();
    out = null;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

  • 相关阅读:
    CSS------制作一个带+-的input框
    rest简介
    flask之flask-login登陆验证(一)
    Python之异常设计(一)
    flask之flask-sqlalchemy(一)
    flask之wtforms 表单验证(一)
    三 Django模型层之Meta
    二 Djano模型层之模型字段选项
    一 Django模型层简介
    Django之路由、模板和模型系统 (转载)
  • 原文地址:https://www.cnblogs.com/liuhaotian548/p/13552256.html
Copyright © 2020-2023  润新知