EasyPoi在项目中的使用
<ul>
<li>EasyPoi使用:建议使用xxx.xls</li>
</ul>
<pre><code><!--easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
<!--easypoi-->
<!---解决验证报错,根据情况决定是否需要添加-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency></code></pre>
<ul>
<li>导出工具类:</li>
</ul>
<pre><code>package com.cf.utils.excel;
import com.cf.vo.ContentProviderDataVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
/**
* Excel工具类
*
* @author jxd
* @version 1.0 *
* @date 2021/3/8 17:18
*/
public class EasyPoiUtil {
/*
*
*响应体指定
* @param response
* @param title 导出文件头标题
* @param fileName 文件名称
* @author jxd
* @date 2021/3/8 17:19
* @return
*/
public static void processCommonExportResponse(HttpServletResponse response, String title, String fileName,Class<?> classzz, Collection<?> dataSet) throws Exception{
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, "sheet1"),
classzz,dataSet);
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
LocalDateTime localDateTime=LocalDateTime.now();
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
String today = dateTimeFormatter.format(localDateTime);
fileName=String.format("%s%s.xls",fileName,today);
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
}
}
</code></pre>
<ul>
<li>模型对象</li>
</ul>
<pre><code>package com.cf.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jeecgframework.poi.excel.annotation.Excel;
import java.io.Serializable;
/**
* 数据统计列表导出对象模型
*
* @author jxd
* @version 1.0 *
* @date 2021/3/8 16:00
*/
@Builder
@AllArgsConstructor()
@NoArgsConstructor
@Data
public class ContentProviderDataVo implements Serializable {
private static final long serialVersionUID = 2919150855001907654L;
/**
* 供应商名称
*/
@Excel(name = "内容供应商", orderNum = "1", width = 25)
private String contentProviderName;
/**
* 视频名称
*/
@Excel(name = "视频名称", orderNum = "2", width = 30)
private String videoName;
/**
* 优惠包金额
*/
@Excel(name = "优惠包购买金额", orderNum = "3", width = 10)
private String statisticalPackageMoney;
/**
* 优惠包人数
*/
@Excel(name = "优惠包购买人数", orderNum = "4", width = 10)
private String statisticalPackageCount;
/**
* VIP金额
*/
@Excel(name = "VIP金额", orderNum = "5", width = 10)
private String statisticalVipMoney;
/**
* VIP人数
*/
@Excel(name = "购买VIP人数", orderNum = "6", width = 10)
private String statisticalVipCount;
/**
* 付费转换率
*/
@Excel(name = "付费转化率", orderNum = "7", width = 10)
private String conversionRate;
/**
* 上线时间
*/
@Excel(name = "上线时间", orderNum = "8", width = 25)
private String upTime;
/**
* 播放量
*/
@Excel(name = "播放量", orderNum = "9", width = 10)
private String statisticalPlayCount;
/**
* 点击量
*/
@Excel(name = "点击量", orderNum = "10", width = 10)
private String statisticalClickCount;
/**
* 播放时长
*/
@Excel(name = "播放时长", orderNum = "11", width = 20)
private String statisticalDurationTime;
/**
* PV
*/
@Excel(name = "PV", orderNum = "12", width = 10)
private String statisticalPv;
/**
* UV
*/
@Excel(name = "UV", orderNum = "13", width = 10)
private String statisticalUv;
}</code></pre>
<ul>
<li>使用</li>
</ul>
<pre><code>//导出使用
EasyPoiUtil.processCommonExportResponse(response,"数据统计列表","数据统计列表",ContentProviderDataVo.class,contentProviderDataVos);</code></pre>
<ul>
<li>导入:</li>
</ul>
<pre><code>//导入
@PostMapping("/import")
public Object importVideoSimpleSetList(@RequestParam(value = "file", required = false) MultipartFile multipartFile, HttpServletRequest httpServletRequest) throws Exception {
InputStream inputStream = multipartFile.getInputStream();
String parentId = httpServletRequest.getParameter("parentId");
Pair<Boolean, String> pair = videoSimpleSetService.importVideoSimpleSetListByParentId(inputStream, parentId);
if (!pair.getFirst()) {
return HttpResponse.error(pair.getSecond());
}
return HttpResponse.success();
}</code></pre>