编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Java使用EasyExcel做导出功能(java导出excel表)

wxchong 2024-06-30 10:38:56 开源技术 12 ℃ 0 评论

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。

官方网站

https://easyexcel.opensource.alibaba.com/

废话不多说 直接开整:

先进行pom.xml要导入包进行添加:

<!-- excel工具包 https://easyexcel.opensource.alibaba.com/qa/-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.10</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </exclusion>
        <exclusion>
            <artifactId>poi-ooxml-schemas</artifactId>
            <groupId>org.apache.poi</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.10.0</version>
</dependency>

写个通用EasyExcel工具类:


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * EasyExcel工具类
 */
@Slf4j
public class EasyExcelUtil {
    
    /**
     * 导出Excel(07版.xlsx)到web
     *
     * @param response  响应
     * @param excelName Excel名称
     * @param sheetName sheet页名称
     * @param clazz     Excel要转换的类型
     * @param data      要导出的数据
     * @throws Exception
     */
    public static void export2Web(HttpServletResponse response, String excelName, String sheetName, Class clazz, List data) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        excelName = URLEncoder.encode(excelName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
        EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
    }
    
}

返回参数:


import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;

@Data
@ApiModel(value = "VO", description = "导出列表Vo")
public class VO implements Serializable {
    
    /**
     * 主键
     * @ExcelIgnore:导出列表不显示
     */
    @ApiModelProperty(value = "主键")
    @ExcelIgnore
    private Integer id;

     /**
     * 名称
     * @ColumnWidth(30):要显示导出的列表列:30代表列表宽度
     */
    @ApiModelProperty(value = "名称")
    @ExcelProperty(value = "名称")
    @ColumnWidth(30)
    private String name;
    
    /**
     * 地址
     */
    @ApiModelProperty(value = "地址")
    @ExcelProperty(value = "地址")
    @ColumnWidth(35)
    private String address;
    
}

controller:

/**
 * 导出数据列表
 *
 * @return
 */
@ApiOperation(value = "导出数据列表", notes = "导出数据列表")
@GetMapping(value = "/expor")
public void expor(@ApiIgnore Dto dto, HttpServletResponse response) {
    try {
        String fileName = "数据列表" + DateUtils.getDate("yyyyMMddHHmmss");
       //Service调用相应查询数据方法
        List<VO> list = Service.expor(dto);
        EasyExcelUtil.export2Web(response, fileName, "数据列表", VO.class, list);
    } catch (Exception e) {
        logger.info("导出数据列表失败!失败信息:{}", e.getMessage());
    }
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表