Springboot导出Excel并下载

引入相关依赖




    org.apache.poi
    poi
    3.17



    org.apache.poi
    poi-ooxml
    3.17



    org.apache.poi
    poi-ooxml-schemas
    3.17

Controller

package com.huang.controller;

import com.huang.mapper.UsersMapper;
import com.huang.util.excelExport.ExcelExport2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:huang
 * @Date:2019-09-21 13:13
 * @Description:<描述>
 */
@Controller
public class TestController {

    @Resource
    private UsersMapper usersMapper;

    @RequestMapping("/test")
    public void testExprotExcel(HttpServletResponse response){

        String[] arr = new String[]{"ID","用户名","账号","密码","备注"};

        ExcelExport2.export(response,usersMapper.selectAll(),arr);

    }

}

工具类

文件导出excel工具类

package com.huang.util.excelExport;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:haung
 * @Date:2019-09-21 11:21
 * @Description:Excel导出工具类,依赖于ClassUtil工具类
 */
public final class ExcelExport2 {

    /**
     * 将传入的数据导出excel表并下载
     * @param response 返回的HttpServletResponse
     * @param importlist 要导出的对象的集合
     * @param attributeNames 含有每个对象属性在excel表中对应的标题字符串的数组(请按对象中属性排序调整字符串在数组中的位置)
     */
    public static void export(HttpServletResponse response, List importlist, String[] attributeNames) {
        //获取数据集
        List datalist = importlist;

        //声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一个表格
        HSSFSheet sheet = workbook.createSheet();
        //设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short) 18);

        //获取字段名数组
        String[] tableAttributeName = attributeNames;
        //获取对象属性
        Field[] fields = ClassUtil.getClassAttribute(importlist.get(0));
        //获取对象get方法
        List methodList = ClassUtil.getMethodGet(importlist.get(0));

        //循环字段名数组,创建标题行
        Row row = sheet.createRow(0);
        for (int j = 0; j< tableAttributeName.length; j++){
            //创建列
            Cell cell = row.createCell(j);
            //设置单元类型为String
            cell.setCellType(CellType.STRING);
            cell.setCellValue(transCellType(tableAttributeName[j]));
        }
        //创建普通行
        for (int i = 0;i

类操作工具类

package com.huang.util.excelExport;

import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:huang
 * @Date:2019-09-21 13:41
 * @Description:关于类的操作的工具类
 */
public final class ClassUtil {

    private ClassUtil() {
        throw new Error("工具类不允许实例化!");
    }

    /**
     * 获取类属性
     * @param targetObj 要获取属性的类
     * @return 含有类属性的集合
     */
    public static Field[] getClassAttribute(Object targetObj){

        Class objectClass = targetObj.getClass();
        return objectClass.getDeclaredFields();

    }

    /**
     * 获取对象的所有get或set方法
     * @param targetObj 要获取属性的类
     * @param methodKeyword get或者set关键字
     * @return 含有类get或set方法的集合
     */
    public static List getMethod(Object targetObj,String methodKeyword){
        List methodList = new ArrayList<>();

        Class objectClass = targetObj.getClass();

        Field[] field = objectClass.getDeclaredFields();
        for (int i = 0;i getMethodGet(Object targetObj){
        return getMethod(targetObj,"get");
    }

    /**
     * 获取对象的所有set方法
     * @param targetObj 要获取属性的类
     * @return 含有类方法的集合
     */
    public static List getMethodSet(Object targetObj){
        return getMethod(targetObj,"set");
    }
}

你可能感兴趣的:(Springboot导出Excel并下载)