zoukankan      html  css  js  c++  java
  • 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();
    }
    }
    }
    }

  • 相关阅读:
    CoreText实现图文混排之点击事件
    iOS仿喜马拉雅FM做的毕业设计及总结(含新手福利源码)
    iOS---多线程实现方案一 (pthread、NSThread)
    iOS中navigationItem的titleView如何居中
    从 setNeedsLayout 说起
    精准化测试专业平台Paw:苹果APP应用代码质量的守护者
    Runtime实战之定制TabBarItem大小
    YYModel 源码历险记 代码结构
    10分钟搞定支付宝和微信支付 的 各种填坑
    如何写好一个UITableView
  • 原文地址:https://www.cnblogs.com/liuhaotian548/p/13552256.html
Copyright © 2011-2022 走看看