Springboot利用poi导出excel下载

2020-12-13 04:44

阅读:310

标签:index   catch   cells   puts   文档   public   framework   xlsx   exports   

Springboot利用poi导出excel下载

因为项目中之前的做法是用反射获取属性,所以demo中也是用的反射,我看网上很多文章都是存入一个List中,不知道这两种哪种更何合适一点,或者有什么更好的方法也请大佬们赐教。

pom

org.apache.poi
    poi
    3.13org.apache.poi
    poi-ooxml
    3.13org.projectlombok
    lombok
    true

Service

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class StudentService {

    public List getList(Student student, int index, int size) {
        Student student1 = new Student("张三", 90, 18);
        Student student2 = new Student("李四", 85, 17);
        Student student3 = new Student("王五", 70, 19);
        List list = new ArrayList();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        return list;
    }
}

Controller

import lombok.Cleanup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class StudentController {

    @Autowired
    public StudentService studentService;

    @RequestMapping("/exportStudentExcel")
    public ResponseEntity exportExcel(Student student) {

        List list = studentService.getList(student, 0, 10); // 每次只需要改这几行

        String fileName = "学生成绩统计表"; // 每次只需要改这几行
        String[] getters = {"getName", "getScore", "getAge"}; // 每次只需要改这几行
        String[] headers = {"姓名", "分数", "年龄"}; // 每次只需要改这几行

        Workbook wb = ExcelUtils.createWorkBook(list, getters, headers,student.getClass()); // 每次只需要改这几行

        @Cleanup ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] content = os.toByteArray();
        HttpHeaders httpHeaders = new HttpHeaders();
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpHeaders.setContentDispositionFormData("attachment", fileName + ".xlsx");
        return new ResponseEntity(content, httpHeaders, HttpStatus.OK);
    }
}

ExcelUtils

public class ExcelUtils {
    /**
     * 创建excel文档
     *
     * @param getters list中map的key数组集合
     * @param headers excel的列名
     */
    public static Workbook createWorkBook(List list, String[] getters, String[] headers, Class clazz) {

        List methods = getMethodsByStrs(getters, clazz);

        // 创建.xlsx工作簿
        Workbook wb = new XSSFWorkbook();
        // 创建第一个sheet(页),并命名
        Sheet sheet = wb.createSheet("sheet1");
        // 手动设置列宽.第一个参数表示要为第几列设,第二个参数表示列的宽度,n为列高的像素数.

        for (int i = 0; i  getMethodsByStrs(String[] getters, Class clazz) {
        List list = new ArrayList();
        for (String getter : getters) {
            try {
                list.add(clazz.getDeclaredMethod(getter));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}

Springboot利用poi导出excel下载

标签:index   catch   cells   puts   文档   public   framework   xlsx   exports   

原文地址:https://www.cnblogs.com/n031/p/11119764.html


评论


亲,登录后才可以留言!