zoukankan      html  css  js  c++  java
  • 点击table导出excel(后端实现)

    依赖:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
    </dependency>

    工具类添加静态方法(每行代码作用都已注明)

    public static XSSFWorkbook writeXSSFWorkbook(String sheetName, String []titles, String [][]values, XSSFWorkbook wb) {
            // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
            if (wb == null) {
                wb = new XSSFWorkbook();
            }
            // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
            XSSFSheet sheet = wb.createSheet(sheetName);
            // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
            XSSFRow row = sheet.createRow(0);
            // 第四步,创建单元格,并设置值表头 设置表头居中
            XSSFCellStyle style = wb.createCellStyle();
            //声明列对象
            XSSFCell cell = null;
            // 创建标题
            for(int i = 0; i < titles.length; i++){
                cell = row.createCell(i);
                cell.setCellValue(titles[i]);
                cell.setCellStyle(style);
            }
            // 创建内容
            for (int i = 0; i < values.length; i++) {
                row = sheet.createRow(i + 1);
                for (int j = 0; j < values[i].length; j++) {
                    //将内容按顺序赋给对应的列对象
                    row.createCell(j).setCellValue(values[i][j]);
                }
            }
            // 设置列宽
            for (int col = 0; col < values[0].length; col++) {
                if (col == 0) {
                    sheet.setColumnWidth(col,40 * 256);
                } else if (col == 1) {
                    sheet.setColumnWidth(col,40 * 256);
                } else if (col == 2) {
                    sheet.setColumnWidth(col,15 * 256);
                } else if (col == 3) {
                    sheet.setColumnWidth(col,15 * 256);
                } else {
                    sheet.setColumnWidth(col,20 * 256);
                }
            }
            return wb;
        }

    controller实现点击下载,需要生成一个二维数组作为excel数据:

       @GetMapping("/exportDsExcelFile)
       public void exportDsExcelFile(HttpServletResponse response){  
         // 查询需要导出的数据 List
    <DigitalResourceStatistical> lists = digitalResourcesService.getDigitalResourceStatistical(); String[][] content = new String[lists.size()][];
         // excel表的表头 String[] titles
    =new String[]{"文献类型", "数量(GB)"}; DateFormat dateFormat = new SimpleDateFormat("_yyyyMMdd");
         String date
    = dateFormat.format(new Date()); //sheet名 String sheetName = "不同文献数量统计表"; //文件内容 for(int i = 0;i < lists.size(); i++){ content[i] = new String[titles.length]; content[i][0] = lists.get(i).getTypeName(); content[i][1] = lists.get(i).getFileSize(); } } //excel文件名 String fileName = sheetName + date +".xlsx"; XSSFWorkbook wb = ExcelUtil.writeXSSFWorkbook(sheetName, titles, content,null); //响应到客户端 response.setContentType("application/vnd.ms-excel;charset=utf-8");
         response.setHeader("Content-Disposition", "attachment;filename="+fileName); OutputStream os
    = null; try { os = response.getOutputStream(); wb.write(os); os.close(); wb.close(); } catch (IOException e) { e.printStackTrace(); } }

    以上是最基本的代码结构,还需要根据自己的业务需求添加功能。

    效果展示:

    点击导出后的效果:

  • 相关阅读:
    Matching「CEOI2011」
    中位数之中位数「NOIP多校联考 2019」
    积木「CSP-S全国排位赛第一场」
    序列「CSP-S全国排位赛第一场」
    GT考试「HNOI2008」
    动物园「NOI2014」
    Radio Transmission「BOI2009」
    小Z的袜子「国家集训队」
    BZOJ3624: [Apio2008]免费道路
    BZOJ1190: [HNOI2007]梦幻岛宝珠
  • 原文地址:https://www.cnblogs.com/wscw/p/14431047.html
Copyright © 2011-2022 走看看