easypoi相对于poi多了一个easy,顾名思义就是更简单了,具体复杂的操作,比如模板导出什么的可以看官网,这里分享一个常规操作
1、加入依赖
<!--easypoi-->
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-web</artifactId>
<version>2.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jeecg/easypoi-annotation -->
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>2.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jeecg/easypoi-base -->
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-base</artifactId>
<version>2.4.0</version>
</dependency>
2、加入自己的一个文件下载类
package com.dpy.platform.common.util;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
* @author zhuenbang
* @description
* @date 2018/7/26 14:42
*/
public class FileDownload {
public static void fileDownload(final HttpServletResponse response, byte[] file, String fileName) throws Exception {
byte[] data = file;
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
response.flushBuffer();
}
public static byte[] getBytes(Workbook workbook) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
return os.toByteArray();
}
}
3、加入导出excel的实体类
package com.dpy.platform.common.vo.nature;
import com.dpy.platform.common.entity.nature.NatureInvoice;
import com.dpy.platform.common.enums.nature.InvoiceEnums;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelTarget;
/**
* @author zhuenbang
* @description
* @date 2018/7/26 16:36
*/
@ExcelTarget(value = "natureInvoiceExcel")
public class NatureInvoiceExcel {
@Excel(name = "订单号",width = 30)
private String orderId;//订单id
@Excel(name = "发票介质",width = 30)
private String invoiceType;//0纸质发票1电子发票
@Excel(name = "发票内容",width = 30)
private String invoiceContent;//发票内容
@Excel(name = "发票抬头",width = 30)
private String invoiceBelong;//0企业1个人
//private String invoiceLookUp;//发票抬头名称
@Excel(name = "收票人",width = 30)
private String invoiceRecipient;//收件人
@Excel(name = "收票地址/邮箱",width = 30)
private String invoiceAddres;//邮件地址
//private String invoiceTaxNo;// 纳税号
//private String invoicePhoneNumber;//联系号码
// private String invoiceEmail;//邮箱
@Excel(name = "发票状态",width = 30)
private String invoiceFlag;//是否开发票
@Excel(name = "商家",width = 30)
private String merchants = "得便宜";//商家
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getInvoiceType() {
return invoiceType;
}
public void setInvoiceType(String invoiceType) {
this.invoiceType = invoiceType;
}
public String getInvoiceContent() {
return invoiceContent;
}
public void setInvoiceContent(String invoiceContent) {
this.invoiceContent = invoiceContent;
}
public String getInvoiceBelong() {
return invoiceBelong;
}
public void setInvoiceBelong(String invoiceBelong) {
this.invoiceBelong = invoiceBelong;
}
public String getInvoiceRecipient() {
return invoiceRecipient;
}
public void setInvoiceRecipient(String invoiceRecipient) {
this.invoiceRecipient = invoiceRecipient;
}
public String getInvoiceAddres() {
return invoiceAddres;
}
public void setInvoiceAddres(String invoiceAddres) {
this.invoiceAddres = invoiceAddres;
}
public String getInvoiceFlag() {
return invoiceFlag;
}
public void setInvoiceFlag(String invoiceFlag) {
this.invoiceFlag = invoiceFlag;
}
public String getMerchants() {
return merchants;
}
public void setMerchants(String merchants) {
this.merchants = merchants;
}
public NatureInvoiceExcel() {
}
public NatureInvoiceExcel(NatureInvoice natureInvoice) {
if (natureInvoice != null) {
this.orderId = natureInvoice.getOrderId();
this.invoiceType = InvoiceEnums.InvoiceType.getByCode(natureInvoice.getInvoiceType()).getMsg();
this.invoiceBelong = InvoiceEnums.InvoiceBelong.getByCode(natureInvoice.getInvoiceBelong()).getMsg();
this.invoiceContent = natureInvoice.getInvoiceContent();
//this.invoiceTaxNo = natureInvoice.getInvoiceTaxNo();
this.invoiceRecipient = natureInvoice.getInvoiceRecipient() + "" + natureInvoice.getInvoicePhoneNumber();
//this.invoiceEmail = natureInvoice.getInvoiceEmail();
this.invoiceAddres = natureInvoice.getInvoiceAddres() != null ? natureInvoice.getInvoiceAddres() : natureInvoice.getInvoiceEmail();
this.invoiceFlag = InvoiceEnums.InvoiceFlag.getByCode(natureInvoice.getInvoiceFlag() == true ? 1 : 0).getMsg();
this.merchants = "得便宜";
}
}
}
4、具体service导出操作的代码
@Override
public void exportexcelInvoice(NatureInvoiceParam natureInvoiceParam, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<NatureInvoice> invoiceList = natureInvoiceaAdminDao.exportexcelInvoice(natureInvoiceParam);
List<NatureInvoiceExcel> excelList = invoiceList.stream().map(NatureInvoiceExcel::new)
.collect(Collectors.toList());
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("发票列表",
"发票列表"), NatureInvoiceExcel.class, excelList);
FileDownload.fileDownload(response, FileDownload.getBytes(workbook), DateUtil.currentDate() + "发票列表.xls");
}
5、最后导出的exce截图