springboot之报表的导出

时间:2020-06-19 16:05:56   收藏:0   阅读:263

1、导入jar包

 <!-- poi实现excel导入导出 用于报表的导出-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

2、controller层

package com.ccc.demoboot.controller;

import com.ccc.demoboot.domain.RiskUser;
import com.ccc.demoboot.service.RiskUserService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class UserReportController {

    @Autowired
    private RiskUserService riskUserService;
    @RequestMapping(value = "/UserInfoExcel", method = RequestMethod.GET)
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void exportEnergyProExcel(HttpServletResponse response) {
        List<RiskUser> list =riskUserService.getUserByCompanyId(17) ;//查出数据库数据
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("users");//创建一张表
        Row titleRow = sheet.createRow(0);//创建第一行,起始为0
        titleRow.createCell(0).setCellValue("序号");
        titleRow.createCell(1).setCellValue("姓名");//第一列
        titleRow.createCell(2).setCellValue("公司");
        titleRow.createCell(3).setCellValue("添加人");
        titleRow.createCell(4).setCellValue("邮箱");
        titleRow.createCell(5).setCellValue("电话");
        int cell = 1;
        for (RiskUser riskUser : list) {
            Row row = sheet.createRow(cell);//从第二行开始保存数据
            row.createCell(0).setCellValue(cell);
            row.createCell(1).setCellValue(riskUser.getUserName());//将数据库的数据遍历出来
            row.createCell(2).setCellValue(riskUser.getCompanyId());
            row.createCell(3).setCellValue(riskUser.getDealUser());
            row.createCell(4).setCellValue(riskUser.getEmail());
            row.createCell(5).setCellValue(riskUser.getContactPhone());
            cell++;
        }

        String fileName = "user用户信息报表.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  

原文:https://www.cnblogs.com/xuemeng11/p/13163089.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!