1.poi导出execel

1.pom.xml中添加相应的依赖

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>4.0.1</version>
    </dependency>

2.Controller添加如下方法

package com.li.controller;

import com.li.entity.User;
import com.li.entity.VO;
import com.li.service.UserService;
import org.apache.poi.ss.usermodel.Cell;
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.PathVariable;
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.net.URLEncoder;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class ExepertExecelController {

@Autowired
private UserService userService;
/*
* 通过
* */
@RequestMapping(value = "/export/{month}", method = RequestMethod.GET)
public void export(@PathVariable(name = "month") String month, HttpServletResponse response) throws Exception {
    //1.构造数据,User为实体类对象、数据库中查询数据封装到list集合中
    List<User> list =
            userService.queryPermission();
    //2.创建工作簿
    XSSFWorkbook workbook = new XSSFWorkbook();
    //3.构造sheet
    String[] titles = {"用户id","用户名","密码","盐"};
    Sheet sheet = workbook.createSheet();
    Row row = sheet.createRow(0);
    AtomicInteger headersAi = new AtomicInteger();
    for (String title : titles) {
        //创建单元格
        Cell cell = row.createCell(headersAi.getAndIncrement());
        cell.setCellValue(title);
    }
    AtomicInteger datasAi = new AtomicInteger(1);
    Cell cell = null;
    //获取的对象里的数据、以下方法循环一次,输出一行
    for (User user : list) {
        Row dataRow = sheet.createRow(datasAi.getAndIncrement());
        //编号
        cell = dataRow.createCell(0);
        cell.setCellValue(user.getUserId());
        //用户名
        cell = dataRow.createCell(1);
        cell.setCellValue(user.getUserName());
        //密码
        cell = dataRow.createCell(2);
        cell.setCellValue(user.getPassWord());

        cell = dataRow.createCell(3);
        cell.setCellValue(user.getPasswordSalt());


    }
    String fileName = URLEncoder.encode(month+"人员信息.xlsx", "UTF-8");

    response.setContentType("application/octet-stream");
    response.setHeader("content-disposition", "attachment;filename=" + new
            String(fileName.getBytes("ISO8859-1")));
    response.setHeader("filename", fileName);
    workbook.write(response.getOutputStream());
}

}

可参考
https://www.oschina.net/p/poi?hmsr=aladdin1e1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值