zoukankan      html  css  js  c++  java
  • java实现点击查询数据生成excel文件并下载

    须先导入关键maven包 
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
    </dependency>

    请求的controller:
    @GetMapping("/deposits/downloadexcel")
    public void downloadExcel (HttpServletRequest request, HttpServletResponse response){
    try {
    //命名列名
    List<String> cellNameList = new ArrayList<>();
    cellNameList.add("充值时间");
    cellNameList.add("充值金额");
    cellNameList.add("说明");
    //给文件命名
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    String dateformat = simpleDateFormat.format(new Date());
    String excelPath="充值记录"+dateformat+".xls";
    //给表命名
    String title= "充值记录";
    HSSFWorkbook excel = Excel.createExcel(title, cellNameList);
    List<Deposit> Deposits = depositService.findAll();
    int row = 1;
    //从数据库读数据然后循环写入
    for(Deposit deposit : Deposits){
    List<String> excelData = new ArrayList<>();
    excelData.add(deposit.getCreatedAt().toString());
    excelData.add(deposit.getPrice().toString());
    excelData.add(deposit.getComment());
    excel = Excel.createExcelData(excel, excelData, row);
    row++;
    }
    //输出数据
    //FileOutputStream fos = new FileOutputStream(excelPath);
    OutputStream out = null;
    //防止中文乱码
    String headStr = "attachment; filename="" + new String(excelPath.getBytes("utf-8"), "ISO8859-1" ) + """;
    //response.setContentType("octets/stream");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", headStr);
    out = response.getOutputStream();
    //excel写入流
    excel.write(out);
    out.flush();
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    工具类:
    public class Excel {
    public static HSSFWorkbook createExcel(String sheetName, List<String> cellNameList) {
    HSSFWorkbook excel = new HSSFWorkbook();
    HSSFSheet sheet = excel.createSheet(sheetName);
    HSSFRow row = sheet.createRow(0);
    int cellIndex = 0;
    for (String cellName : cellNameList) {
    HSSFCell cell = row.createCell(cellIndex);
    cell.setCellValue(cellName);
    cellIndex++;
    }
    return excel;
    }

    public static HSSFWorkbook createExcelData(HSSFWorkbook excel,List<String> excelData,int rowIndex){
    HSSFRow row=excel.getSheetAt(0).createRow(rowIndex);
    for(int i = 0; i < excelData.size(); i++){
    row.createCell(i).setCellValue(excelData.get(i));
    }
    return excel;
    }
    }
    excel表格样式可以通过代码设置,具体设置百度,这里就不写了
    实现效果

    点击导出数据便能实现数据下载

    不知为何浏览器不支持粘贴图片 贼难受 就写到这儿了



    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
    </dependency>
    盛世岂埋凌云气,年少无为就努力
  • 相关阅读:
    吴恩达机器学习课程笔记章节二单变量线性回归
    cs224n第六讲依存分析
    吴恩达机器学习课程笔记章节一绪论
    cs224n第二讲词向量表示:word2vec
    cs224n第一讲深度自然语言处理
    DIY的.net正则表达式工具
    不写一行代码,利用常用工具和软件批量下载URL资源
    WEB页面采集器编写经验之一:静态页面采集器
    大规模IP地址黑名单高性能查询实现
    安卓开发入门与面试题01(潭州安卓开发入门教程)
  • 原文地址:https://www.cnblogs.com/guangchuantang/p/10904488.html
Copyright © 2011-2022 走看看