pom文件
<!-- easypoi导入导出excel -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version> 4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.2.0</version>
</dependency>
这里是工具方法
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class AbstractExcelOut implements IExcelOut {
private String fileName;
private HttpServletResponse response;
private AbstractExcelOut() {
}
public AbstractExcelOut(String fileName, HttpServletResponse response) {
this.fileName = fileName;
this.response = response;
this.responseInit();
}
private void responseInit() {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
try {
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void excelOut(ExportParams exportParams, List<?> dataList, Class<?> tClass) throws Exception {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, tClass, dataList);
setExportExcelFormat(workbook);
}
private void setExportExcelFormat(Workbook workbook) throws Exception {
ServletOutputStream outStream = null;
try {
outStream = response.getOutputStream();
workbook.write(outStream);
} finally {
outStream.close();
}
}
}
接口层
import cn.afterturn.easypoi.excel.entity.ExportParams;
import java.util.List;
public interface IExcelOut {
void excelOut(ExportParams exportParams, List<?> dataList, Class<?> tClass) throws Exception;
}
调用
String ORDER_File_NAME = "order-excel";
String ORDER_SHEET_NAME = "order-excel";
String ORDER_TITLE = "采购统计表";
AbstractExcelOut abstractExcelOut = new AbstractExcelOut(ORDER_File_NAME, response);
abstractExcelOut.excelOut(new ExportParams(ORDER_TITLE, ORDER_SHEET_NAME), resultList, YmzcOrderStatisticalVo.class);
关于合并都在实体类里面(主要是结构和注解)
关于合并只有两点:
1、类的数据结构。
2、注解
@ExcelCollection(name = "") 集合使用
@Excel(name = "采购数量合计(支)", width = 30, orderNum = "5",needMerge = true, isStatistics = true)
name:列的标题名称,width :表格的宽度, orderNum :排序(注意每个类中的排序为单独的 不同类排序不通用)
needMerge : 是否合并单元格, isStatistics:是否进行统计(若为true,则在表格最后自动生成最终统计)
@Data
@ApiModel("采购统计以及导出")
public class YmzcOrderStatisticalVo implements Serializable {
@ExcelCollection(name = "")
@ApiModelProperty(value = "")
private List<ExcelSheetHead> number;
@ApiModelProperty(value = "采购单位名称")
@Excel(name = "采购单位", width = 30, orderNum = "3",needMerge = true)
private String creator;
@ApiModelProperty(value = "采购疫苗详情")
@ExcelCollection(name = "", orderNum = "4")
private List<YmzcOrderVaccineDetailsVo> detailsList;
@ApiModelProperty(value = "采购单位ID")
private Long creatorId;
@ApiModelProperty(value = "采购数量合计(支)")
@Excel(name = "采购数量合计(支)", width = 30, orderNum = "5",needMerge = true, isStatistics = true)
private Integer statisticalNumber;
@ApiModelProperty(value = "采购数量合计(支)")
@Excel(name = "采购金额合计(元)", width = 30, orderNum = "6",needMerge = true, isStatistics = true)
private BigDecimal statisticalPrice;
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a2f8266e234efc17a3d6e182603e201f.png)