easy-execl java export data

Function Description: The database query to export data to generate corresponding execl file

Involving technology (easy-execl) alibaba open source components, compared with poi take up less memory and better performance

1: easyexcel dependent introducing (direct reference to)
sad

## 2: introducing easyexcel tool library (direct reference to)

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ExcelExportUtil {

    /**
     * 导出execl
     * @param dataList 模型数据集合
     * @param fileName 文件名称
     * @param sheetName sheet名称
     * @param object 模型对象
     * @param response   响应参数
     * @param request  请求参数
     * @throws IOException
     */
    public static void export(List<? extends BaseRowModel> dataList, String fileName, String sheetName,
                              BaseRowModel object, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //设置响应参数返回输出对象
        ServletOutputStream out =getOut(fileName,response,request);
        //使用easy-excel
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,true);
        //创建Sheet
        Sheet sheet = new Sheet(1,0, object.getClass());
        //sheet命名
        sheet.setSheetName(sheetName);
        writer.write(dataList,sheet);
        writer.finish();
        out.close();
    }

    /**
     *  设置响应参数返回输出对象
     * @param fileName 文件名称
     * @param response  响应对象
     * @param request  请求对象
     * @return
     */
    private static ServletOutputStream getOut(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        String agent = request.getHeader("USER-AGENT").toLowerCase();
        response.setContentType("application/vnd.ms-excel");
        String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        if (agent.contains("firefox")) {
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xlsx" );
        } else {
            response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xlsx");
        }
        return response.getOutputStream();
    }
}

## 3 generated model derived class (according to their need to be modified)

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;

/**
 * <p>Description: 实体类 </p>
 * <p>Company: http://www.bjpowernode.com
 */
@Data
public class StudentModel extends BaseRowModel {
	//value 生成对应的第一行表头名,index为列的下标
    @ExcelProperty(value = "序号",index = 0)
    private Integer id;

    @ExcelProperty(value = "姓名",index = 1)
    private String name;

    @ExcelProperty(value = "年龄",index = 2)
    private Integer age;
}

Call ## 4controller tool class

@GetMapping("/execl/export")
    public void getExport(HttpServletResponse response, HttpServletRequest request) throws IOException {
        List<Student> students = studentService.allList();
        List<StudentModel> data=new ArrayList<>();
        //转化为模型数据集合
        List<StudentModel> studentModels = JSONObject.parseArray(JSON.toJSONString(students), StudentModel.class);
        StudentModel studentModel = new StudentModel();
        ExcelExportUtil.export(studentModels,"学生表","student",studentModel,response,request);
    }

## 5 normal access to the browser
Here Insert Picture Description

Published 27 original articles · won praise 1 · views 865

Guess you like

Origin blog.csdn.net/weixin_44971379/article/details/103759195