java百萬級別excel導出(easyExcel,阿里出品,超讚)

* @Author: yaomaoyang

* @CreateDate: 2018/9/29 18:05

* @UpdateUser: yaomaoyang

* @UpdateDate: 2018/9/29 18:05

* @UpdateRemark: 修改內容

* @Version: 1.0

*/

public class ExcelUtils {

/**

* 導出

* @param list

* @param response

* @param clazz

* @return

*/

public static BoUtil export(List extends BaseRowModel> list, HttpServletResponse response, Class extends BaseRowModel> clazz) {

BoUtil boUtil = BoUtil.getDefaultFalseBo();

ServletOutputStream out = null;

try {

out = response.getOutputStream();

} catch (IOException e) {

e.printStackTrace();

}

ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);

try {

boUtil = BoUtil.getDefaultTrueBo();

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();

boUtil.setMsg("導出成功");

} catch (Exception e) {

e.printStackTrace();

boUtil.setMsg("導出失敗");

return boUtil;

} finally {

writer.finish();

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

return boUtil;

}

}

}

AdvertisingIndentBo.java

package com.dctp.cloud.boss.bo;

import java.math.BigDecimal;

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 String id;

// 交易類型:1 買入 2賣出

private Integer tradeType;

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

//交易類型(導出時使用)

private String tradeTypeStr;

// 用戶ID begin

private Long userID;

private Integer login;

private String alaisName;

@ExcelProperty(value = {"會員"},index = 2)

private String realname;

private String phone;

private String email;

// 用戶信息 end

// 資產ID

private Long coinID;

@ExcelProperty(value = {"資產名稱"},index = 3)

private String coinName;

// 交易狀態

private Integer tradeState;

//交易狀態(導出時使用)

@ExcelProperty(value = {"狀態"},index = 4)

private String tradeStateStr;

// 取價類型

private Integer priceType;

// 取價類型(導出時使用)

@ExcelProperty(value = {"取價類型"},index = 5)

private String priceTypeStr;

// 平臺實時價格的百分比

@ExcelProperty(value = {"溢價"},index = 6)

private BigDecimal premium;

// 交易單價

@ExcelProperty(value = {"價格"},index = 7)

private BigDecimal price;

// 交易數量

private BigDecimal cionNum;

// 剩餘數量

private BigDecimal residueNum;

// 交易最小金額

@ExcelProperty(value = {"最小金額"},index = 8)

private BigDecimal minPrice;

// 交易最大金額

@ExcelProperty(value = {"最大金額"},index = 9)

private BigDecimal maxPrice;

// 付款方式類型

@ExcelProperty(value = {"付款類型"},index = 10)

private String payType;

// 付款期限30-120分鐘之內

@ExcelProperty(value = {"付款期限"},index = 11)

private Integer payTime;

// 創建時間

@ExcelProperty(value = {"創建時間"},index = 12)

private String createTime;

// 交易密碼

private String password;

/**

* 創建人

*/

private Long createBy;

/**

* 修改人

*/

private Long updateBy;

private String activeFlag;

}

controller:

boUtil = ExcelUtils.export(list, response, AdvertisingIndentBo.class);

list:LIst:你需要到處的數據集合。

BaseRowModel:為什麼要繼承BaseRowModel,因為他是基類。

@ExcelProperty:對應excel中的表頭。

value:表頭的名稱

index:排序

總結

以上就是esatExcel的excel導出,如果有什麼疑問或者不足,歡迎大家的留言。


分享到:


相關文章: