提交 b771441b 作者: 925993793@qq.com

自定义专题-研究中心:增加导出excel接口以及资讯列表接口增加 格式化时间字段 dateFormat

上级 c9915324
......@@ -404,6 +404,60 @@ public class FileController {
}
}
/**
* 导出数据-excel格式-研究中心
*
* @param searchCondition 筛选条件
* @author lkg
* @date 2024/12/25
*/
@PostMapping("/exportExcel")
public void exportExcel(@RequestBody InfoDataSearchCondition searchCondition, HttpServletResponse response) {
int count = esService.getCount(searchCondition);
if (count > 0) {
String subjectId = searchCondition.getSubjectId();
long timestamp = System.currentTimeMillis();
String filename = subjectId + "_" + timestamp + ".xlsx";
try {
String[] fetchFields = new String[]{"score", "title", "titleRaw", "summary", "summaryRaw", "content", "contentRaw", "author", "origin", "publishDate", "sourceAddress", "hitWords"};
searchCondition.setFetchFields(fetchFields);
searchCondition.setPageSize(1000);
String[] arr = new String[]{"得分", "标题", "标题译文", "摘要", "摘要译文", "正文", "正文译文", "作者", "来源", "发布时间", "网址", "命中词"};
List<String> headers = Arrays.asList(arr);
SXSSFWorkbook workbook = new SXSSFWorkbook();
for (int i = 1; ; i++) {
searchCondition.setPageNo(i);
List<SpecialInformation> informationList = esService.informationList(searchCondition);
log.info("本次循环-{},数据量为-{}", i, informationList.size());
if (CollectionUtils.isEmpty(informationList)) {
break;
}
List<List<String>> rows = new ArrayList<>();
informationList.forEach(e->rows.add(e.toExcelList()));
BigExcelExportUtil.exportExcelData(workbook, i - 1, headers, rows, "sheet" + i);
log.info("第【{}】个sheet页写入成功", i);
}
// 将Workbook写入字节流
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
workbook.write(outStream);
// 将字节流转换为InputStream
byte[] bytes = outStream.toByteArray();
filename = URLEncoder.encode(filename, "UTF-8").replace("+", " ");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentLength(bytes.length);
OutputStream out = response.getOutputStream();
out.write(bytes);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
throw new FileExportException("数据量为0,无法导出");
}
}
private byte[] getBytes(InfoDataSearchCondition searchCondition, Integer pageSize) {
String[] fetchFields = new String[]{"score", "title", "titleRaw", "summary", "summaryRaw", "content", "contentRaw", "author", "origin", "publishDate", "sourceAddress", "classificationType", "hitWords", "labels"};
......
......@@ -156,7 +156,11 @@ public class InformationServiceImpl implements InformationService {
for (SpecialInformation specialInformation : records) {
DisplayInfo info = new DisplayInfo();
BeanUtils.copyProperties(specialInformation, info);
info.setPublishDate(EsDateUtil.esFieldDateMapping(info.getPublishDate()));
String publishDate = EsDateUtil.esFieldDateMapping(info.getPublishDate());
String dateFormat = searchCondition.getDateFormat();
if (!dateFormat.equals("yyyy-MM-dd HH:mm:ss")) {
info.setPublishDate(DateUtil.formatStr(publishDate,"yyyy-MM-dd HH:mm:ss", dateFormat));
}
//标签处理
List<LabelModelVo> modelVoList = modelMap.get(info.getSubjectId());
formatLabel(modelVoList, info);
......
......@@ -33,7 +33,8 @@ public class InfoDataSearchCondition {
//搜索词
private String searchWord;
//时间格式化
private String dateFormat = "yyyy-MM-dd HH:mm:ss";
//发布时间
private String publishDate;
......
package com.zzsn.event.vo.es;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class SpecialInformation {
......@@ -144,4 +148,33 @@ public class SpecialInformation {
/**数据来源(1-人工导入;空-正常采集)*/
private Integer dataFrom;
public List<String> toExcelList(){
List<String> list = new ArrayList<>();
list.add(getRealValue(score));
list.add(getRealValue(title));
list.add(getRealValue(titleRaw));
list.add(getRealValue(summary));
list.add(getRealValue(summaryRaw));
list.add(getRealValue(content));
list.add(getRealValue(contentRaw));
list.add(getRealValue(author));
list.add(getRealValue(origin));
list.add(getRealValue(publishDate));
list.add(getRealValue(sourceAddress));
if (CollectionUtils.isNotEmpty(hitWords)) {
list.add(String.join(",", hitWords));
} else {
list.add("");
}
return list;
}
private String getRealValue(Object obj){
if (ObjectUtils.isEmpty(obj)) {
return "";
}
return String.valueOf(obj);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论