easyexcel下载工具

优化excel下载功能,采用新的工具类降低内存:

https://github.com/alibaba/easyexcel

pom:

 <!-- easyexcel依赖包 -->

    <dependency>

  <groupId>com.alibaba</groupId>

  <artifactId>easyexcel</artifactId>

  <version>1.0.4</version>

    </dependency>

        <!-- lombok依赖包 -->

    <dependency>

  <groupId>org.projectlombok</groupId>

  <artifactId>lombok</artifactId>

  <version>1.16.12</version>

  <scope>provided</scope>

    </dependency>

  <!-- 注意开始 如果使用easyexcel下载工具,那么就poi版本换为高版本,比如:3.17【easyexcel中使用了高版本的poi进行改造】-->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.17</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.17</version>

</dependency>

package net.chrone.merchant.service.util.easyexcel;

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.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @Description:    excel操作工具类
* @Version:        1.0
*/
public class ExcelUtils {
    /**
     * 导出
     * @param list
     * @param response
     * @param clazz
     * @return
     */
    public static  void export(List<? extends BaseRowModel> list, HttpServletResponse response, Class<? extends BaseRowModel> clazz) {
 
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
        try {
            String fileName = new String(
                    (new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8");
            Sheet sheet2 = new Sheet(2, 3,clazz, "sheet", null);
            writer.write(list, sheet2);
            //response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8"));
            //response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            out.flush();
        } catch (Exception e) {
            //导出失败
        } finally {
            writer.finish();
            try {
                out.close();
            } catch (IOException e) {
                //流关闭失败
            }
        }
    }
}

 

package net.chrone.merchant.service.util.easyexcel.vo;

 

import com.alibaba.excel.annotation.ExcelProperty;

import com.alibaba.excel.metadata.BaseRowModel;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.experimental.Builder;

 

@SuppressWarnings("deprecation")

@Data

@Builder

@NoArgsConstructor

@AllArgsConstructor

public class AdvertisingIndentBo extends BaseRowModel {

   // 订单编号

   @ExcelProperty(value = {"广告编号"},index = 0)

   private Integer id;

   @ExcelProperty(value = {"交易"},index = 1)

   //交易类型(导出时使用)

   private String tradeTypeStr;

   @ExcelProperty(value = {"资产名称"},index = 2)

   private String coinName;

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值