<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
<easyexcel.version>1.1.2-beat1</easyexcel.version>
不需要配置yml
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
/**
* 上传带数据的模板
*
* @param headerList 模板表头
* @param list 模板数据
* @param process 进程
* @param tenantId 企业id
* @param grantId 操作员id
* @return url
*/
public String uploadWithData(List<FieldBaseVO> headerList, List<Map<String, DataBaseVO>> list, Process process,
Long tenantId, Long grantId) {
try {
OutputStream out = new FileOutputStream(fileName);
ExcelWriter writer = EasyExcelFactory.getWriter(out);
//设置表格编号
Sheet sheet1 = getSheet(headerList);
writer.write1(generateFormData(headerList, list, process, tenantId, grantId), sheet1);
mergeNotList(writer, headerList, list);
writer.finish();
out.close();
} catch (FileNotFoundException e) {
process.setState("异常");
process.setDescription("FileNotFoundException");
processDao.update(process);
e.printStackTrace();
} catch (IOException e) {
process.setState("异常");
process.setDescription("IOException");
processDao.update(process);
e.printStackTrace();
}
return uploadTemplateByFile();
}
/**
* 根据表头生成sheet模板
*
* @param headerList 表头数据
* @return sheet
*/
private Sheet getSheet(List<FieldBaseVO> headerList) {
//设置表格编号
Sheet sheet1 = new Sheet(1);
//设置表格名称
sheet1.setSheetName("第一个form模板");
//设置表格header
//计算,并生成header的行数
int rowNumber = anysisRowNumber(headerList, 1);
//生成header
List<List<String>> head = transHeader(headerList, rowNumber);
sheet1.setHead(head);
return sheet1;
}
/**
* 根据表头和表数据,生成对应的表数据;
*
* @param headerList 表头
* @param formDatas 表数据
* @param process 当前进程
* @param tenantId 企业id
* @param grantId 操作员
* @return 返回填充后的表
*/
private List<List<Object>> generateFormData(List<FieldBaseVO> headerList, List<Map<String, DataBaseVO>> formDatas,
Process process, Long tenantId, Long grantId) {
//设置数据内容,生成表格
List<List<Object>> object = new ArrayList<>();
int count = 0;
for (Map<String, DataBaseVO> map : formDatas) {
int maxRowNumber = getFormDataRowNumber(map, headerList);
for (int i = 0; i < maxRowNumber; i++) {
//一个da代表一行,添加不同值代表占多列;(在同列的不同的行中添加相同值,代表合并)
List<Object> da = new ArrayList<>();
for (FieldBaseVO f : headerList) {
//普通类型
if (!"list".equals(f.getType())) {
setDataList(toObjMap(map), f, da, tenantId, grantId);
} else {
//获取list中的mp;
List<FieldBaseVO> fields = ((TableVO) f).getFields();
//获取字段名称相同的数据的list,
if (map.containsKey(f.getName())) {
JSONObject jsVO = JSONObject.parseObject(JSONObject.toJSONString(map.get(f.getName())), JSONObject.class);
JSONArray jsArr = jsVO.getJSONArray("value");
List<Map<String, Object>> mpss = new ArrayList<>();
for (int j = 0; j < jsArr.size(); j++) {
//RowVo
JSONObject o = (JSONObject) jsArr.get(j);
JSONObject value = (JSONObject) o.get("value");
mpss.add(value.getInnerMap());
}
//将tlist中的数据按照名称,逐一添加到data中
for (FieldBaseVO fbv : fields) {
setDataList(mpss.get(i), fbv, da, tenantId, grantId);
}
}
}
}
object.add(da);
}
count++;
if (count != formDatas.size()) {
process.setState("处理中");
process.setDescription("处理进度");
process.setRate(count + "/" + formDatas.size());
redisTemplate.boundHashOps(process.getTenantId()).put(process.getId(), JSON.toJSON(process));
}
}
return object;
}
/**
* 合并非list类型的数据,合并行数为list.size()
*
* @param writer 当前操作的ExcelWriter
* @param headerList 表头
* @param formDatas 数据
*/
private void mergeNotList(ExcelWriter writer, List<FieldBaseVO> headerList, List<Map<String, DataBaseVO>> formDatas) {
int headRow = anysisRowNumber(headerList, 1);
for (Map<String, DataBaseVO> map : formDatas) {
int maxRowNumber = getFormDataRowNumber(map, headerList);
if (maxRowNumber == 1) {
headRow = headRow + 1;
continue;
}
int colNumber = 0;
for (FieldBaseVO f : headerList) {
if (!"list".equals(f.getType())) {
writer.merge(headRow, headRow + maxRowNumber - 1, colNumber, colNumber);
colNumber++;
} else {
//获取list中的mp;
TableVO tableVO = (TableVO) f;
List<FieldBaseVO> fields = tableVO.getFields();
colNumber = colNumber + fields.size() + 1;
}
}
headRow = headRow + maxRowNumber;
}
}
/**
* 将临时文件上传,返回fastdfs保存路径
*
* @return fastdfs保存路径
*/
private String uploadTemplateByFile() {
String nginxUrl = "";
try {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
InputStream is = new FileInputStream(file);
String strBase64 = ioToBase64(is);
is.close();
FileTransferVo fileTransferVo = fileClient.upTemplate(strBase64, fileName);
nginxUrl = fileTransferVo.getPath();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
return nginxUrl;
}
/**
* 获取该调导出fromData数据所占的行数,适用于二级//后期做成递归
*
* @param map formData 所有数据,包含需要和非需要导出的具体数据
* @param headerList 必定导出字段的定义
* @return 返回所占的行数用于合并
*/
private int getFormDataRowNumber(Map<String, DataBaseVO> map, List<FieldBaseVO> headerList) {
int number = 1;
//如果要求导出的数据中不包含list类型的数据,直接返回number=1
for (int i = 0; i < headerList.size(); i++) {
if ("list".equals(headerList.get(i).getType())) {
break;
}
if (i == headerList.size() - 1) {
return number;
}
}
for (Map.Entry<String, DataBaseVO> m : map.entrySet()) {
JSONObject jb = JSONObject.parseObject(JSONObject.toJSONString(m.getValue()));
if ("list".equals(jb.getString("type"))) {
if (jb.getJSONArray("value").size() > number) {
number = jb.getJSONArray("value").size();
}
}
}
return number;
}