官方文檔:EasyPoi教程_V1.0
最近在做導(dǎo)出Excel,針對數(shù)據(jù)是一對多形式的對比后還是Easypoi比較好用。然后給大家分享哈
1.添加maven依賴
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
2、導(dǎo)出對應(yīng)的實(shí)體類(一對多中的“一”)?
package com.zjt.shop.modules.personnel.entity.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import com.zjt.shop.common.excel.ExcelField;
import com.zjt.shop.modules.order.entity.vo.CodeVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* 作者:zhuLin
* 日期:2020-07-30 17:36
* 備注:產(chǎn)品信息 - 實(shí)體類
*/
@Data
@ApiModel(value = "產(chǎn)品信息統(tǒng)計(jì)導(dǎo)出-實(shí)體類")
public class ProductSuitTjVo {
//需要合并得列添加:needMerge = true 即可
@Excel(name = "產(chǎn)品名稱",needMerge = true,width = 20)
@ApiModelProperty(value = "產(chǎn)品名稱")
private String msgName;
@Excel(name = "產(chǎn)品狀態(tài)",needMerge = true,width = 20)
@ApiModelProperty(value = "產(chǎn)品狀態(tài)")
private String prodState;
@Excel(name = "產(chǎn)品經(jīng)理",needMerge = true,width = 20)
@ApiModelProperty(value = "產(chǎn)品經(jīng)理")
private String productManager;
//子集
@ExcelCollection(name = "")
List<SuitTjVo> suitList;
}
?3、子集對象實(shí)體類(一對多中的“多”)
package com.zjt.shop.modules.personnel.entity.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import com.zjt.shop.common.excel.ExcelField;
import com.zjt.shop.modules.order.entity.vo.CodeVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* 作者:zhuLin
* 日期:2020-07-30 17:36
* 備注:產(chǎn)品信息 - 實(shí)體類
*/
@Data
@ApiModel(value = "產(chǎn)品信息統(tǒng)計(jì)導(dǎo)出-實(shí)體類")
public class SuitTjVo {
@Excel(name = "小達(dá)人傭金",width = 10)
@ApiModelProperty(value = "小達(dá)人傭金")
private double talentAmtMin;
@Excel(name = "中達(dá)人傭金",width = 10)
@ApiModelProperty(value = "中達(dá)人傭金")
private double talentAmtCin;
@Excel(name = "銷售價",width = 30)
@ApiModelProperty(value = "銷售價")
private String salePrice;
@Excel(name = "結(jié)算價",width = 30)
@ApiModelProperty(value = "結(jié)算價")
private String settlePrice;
}
?4、導(dǎo)出Excel的實(shí)現(xiàn)方法
/**
* 導(dǎo)出Excel數(shù)據(jù)工具類
* @param response
* @param workbook
* @param fileName
* @throws Exception
*/
public static void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
response.setHeader("Pargam", "no-cache");
response.setHeader("Cache-Control", "no-cache");
ServletOutputStream outStream = null;
try {
outStream = response.getOutputStream();
workbook.write(outStream);
} catch (Exception e) {
e.printStackTrace();
log.error("導(dǎo)出Excel異常:" + e.getMessage());
} finally {
outStream.flush();
outStream.close();
}
}
4、實(shí)例
@ApiOperation("產(chǎn)品信息統(tǒng)計(jì)-導(dǎo)出")
@PostMapping("/productTjExport")
public void productTjExport (HttpServletResponse response,@RequestBody ProductStatisticsVo productVo) throws Exception {
String title = "產(chǎn)品信息統(tǒng)計(jì)";
log.info(title + "導(dǎo)出參數(shù):{}",productVo);
List<ProductSuitTjVo> list = empInfoService.productTjExport(productVo); //mybatis 一對多數(shù)據(jù)格式
ExportParams params = new ExportParams();
params.setTitle(title); //設(shè)置表頭
params.setSheetName(title); //設(shè)置sheet名
Workbook workbook = ExcelExportUtil.exportExcel(params, ProductSuitTjVo.class, list);
ExportExcel.setExportExcelFormat(response, workbook, title);
}
文章來源:http://www.zghlxwxcb.cn/news/detail-719949.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-719949.html
到了這里,關(guān)于Easypoi實(shí)現(xiàn)表格導(dǎo)出(實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)一對多,合并單元格)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!