提交 e573ff04 作者: chenshiqiang

add export

上级 f0645305
......@@ -38,7 +38,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* @Description: 知识
* 知识
* @Author: chenshiqiang
* @Version: V1.0
*/
......@@ -221,86 +221,67 @@ public class KnowledgeController {
return Result.OK();
}
/*
* 导出excel(专题-已采集)
*
* @param request
* @param
* @update by wp
* 异步调用
/**
* 预导出
*
* @param newsIds 资讯知识ids
* @param fileIds 文件知识ids
*/
@PostMapping(value = "/exportCollectionXls")
public Result<?> exportCollectionXls(HttpServletRequest request, HttpServletResponse response, KnowledgeVO knowledgeVO,
String ids,
@PostMapping(value = "/export")
public Result<?> exportCollectionXls(HttpServletRequest request,
String newsIds, String fileIds,
@RequestParam(name = "isAll", defaultValue = "0") String isAll) {
//获取数据
if (redisUtil.get(ids) == null) {
try {
String[] arr = new String[]{"标题", "正文", "作者", "来源", "发布时间"};
List<KnowledgeVO> informationList = knowledgeService.listByIds(ids);
if (CollectionUtils.isNotEmpty(informationList)) {
//动态补充表头
List<String> headers = Arrays.asList(arr);
//每个sheet页最多5000条数据
List<List<KnowledgeVO>> partition = ListUtils.partition(informationList, 5000);
XSSFWorkbook workbook = new XSSFWorkbook();
for (int i = 0; i < partition.size(); i++) {
List<List<String>> rows = formatData(partition.get(i));
ExcelExportUtil.exportExcelData(workbook, i, headers, rows, "sheet" + (i + 1));
}
File fileDir = ExcelExportUtil.getFileDir();
String filePath = fileDir.getAbsolutePath() + File.separator + UUID.randomUUID().toString().replace("-","") + "info.xlsx";
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
redisUtil.set(ids, filePath, 3600*24);
}
} catch (Exception e) {
e.printStackTrace();
return Result.OK(false);
}
}
knowledgeService.exportExcel(newsIds);
knowledgeService.exportPackage(fileIds);
return Result.OK(true);
}
/*
* 导出excel(专题-已入库)
*
* @param request
* @param
* @update by wp
* 异步调用
/**
* 导出
*
* @param newsIds 资讯知识ids
* @param fileIds 文件知识ids
*/
@RequestMapping(value = "/downloadXls")
public void downloadXls(HttpServletResponse response,String ids) {
@RequestMapping(value = "/download")
public void downloadXls(HttpServletResponse response, String newsIds, String fileIds) {
//获取下载路径
Object object = redisUtil.get(ids);
if (object != null) {
String filePath = object.toString();
try {
redisUtil.del(ids);
Object newsObject = redisUtil.get(newsIds);
Object fileObject = redisUtil.get(fileIds);
String newsFilePath = null;
String fileFilePath = null;
if (fileObject != null) {
newsFilePath = newsObject.toString();
}
if (fileObject != null) {
fileFilePath = fileObject.toString();
}
try {
if ((!newsIds.isEmpty())&&(!fileIds.isEmpty())) {
log.info("files and news package downloading ");
List<String> list=new ArrayList<>();
list.add(newsFilePath);
list.add(fileFilePath);
String filePath = knowledgeService.comprocessByName(list, UUID.randomUUID().toString().replace("-", ""));
ExcelExportUtil.download(response, filePath, true);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
if (fileIds.isEmpty()) {//news
log.info("only news downloading");
redisUtil.del(newsIds);
ExcelExportUtil.download(response, newsFilePath, true);
return;
}
log.info("only files downloading");
ExcelExportUtil.download(response, fileFilePath, true);
private List<List<String>> formatData(List<KnowledgeVO> knowledgeVOS) {
List<List<String>> list = new ArrayList<>(knowledgeVOS.size());
for (KnowledgeVO knowledgeVO : knowledgeVOS) {
List<String> innerList=new ArrayList<>();
innerList.add(knowledgeVO.getTitle());
innerList.add(knowledgeVO.getContentAll());
innerList.add(knowledgeVO.getAuthor());
innerList.add(knowledgeVO.getOrigin());
innerList.add(knowledgeVO.getPublishDate());
list.add(innerList);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
return list;
}
}
@Data
......
......@@ -55,4 +55,10 @@ public interface IKnowledgeService {
void doExcel(HttpServletRequest request, String fileSuffix,String userId);
List<KnowledgeVO> listByIds(String ids);
void exportExcel(String ids);
String exportPackage(String ids);
String comprocessByName(List<String> list, String filePath);
}
......@@ -19,6 +19,8 @@ import com.zzsn.knowbase.vo.KnowledgeVO;
import com.zzsn.knowbase.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.pdfbox.pdmodel.PDDocument;
......@@ -26,6 +28,7 @@ import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
......@@ -47,13 +50,12 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @Description: 知识
......@@ -682,6 +684,114 @@ class KnowledgeServiceImpl implements IKnowledgeService {
return list;
}
@Override
public void exportExcel(String ids) {
//获取数据
if (redisUtil.get(ids) == null) {
try {
String[] arr = new String[]{"标题", "正文", "作者", "来源", "发布时间"};
List<KnowledgeVO> informationList = this.listByIds(ids);
if (CollectionUtils.isNotEmpty(informationList)) {
//动态补充表头
List<String> headers = Arrays.asList(arr);
//每个sheet页最多5000条数据
List<List<KnowledgeVO>> partition = ListUtils.partition(informationList, 5000);
XSSFWorkbook workbook = new XSSFWorkbook();
for (int i = 0; i < partition.size(); i++) {
List<List<String>> rows = formatData(partition.get(i));
ExcelExportUtil.exportExcelData(workbook, i, headers, rows, "sheet" + (i + 1));
}
File fileDir = ExcelExportUtil.getFileDir();
String filePath = fileDir.getAbsolutePath() + File.separator + UUID.randomUUID().toString().replace("-", "") + "info.xlsx";
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
redisUtil.set(ids, filePath, 3600 * 24);
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
}
@Override
public String exportPackage(String ids) {
List<KnowledgeVO> knowledgeVOS = this.listByIds(ids);
List<String> pathList = new ArrayList<>(knowledgeVOS.size());
for (KnowledgeVO knowledgeVO : knowledgeVOS) {
pathList.add(filesStorage + knowledgeVO.getFiles().get(0).getFilePath());
}
String filePackage = comprocessByName(pathList, UUID.randomUUID().toString().replace("-",""));
redisUtil.set(ids, filePackage, 3600 * 24);
return filePackage;
}
/*
* 将所有的文件都压缩到一个文件中去
*/
@Override
public String comprocessByName(List<String> fileNameList, String downloadFileName) {
List<File> fileList = new ArrayList<File>();
for (String fileName : fileNameList) {
fileList.add(new File(fileName));
}
return comprocessByFile(fileList, downloadFileName);
}
/*
* 将所有的文件都压缩到一个文件中去
*/
public String comprocessByFile(List<File> fileNamemList, String key) {
String tmpFile = Constants.USER_HOME + Constants.FSP + "brap" + Constants.FSP + "download"
+ Constants.FSP;
byte[] buffer = new byte[4096];
FileInputStream fis = null;
ZipOutputStream zos = null;
File zipFile = null;
try {
zipFile = new File(tmpFile + key + ".zip");
zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File file : fileNamemList) {
try {
fis = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(file.getName()));
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.flush();
zos.closeEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
FileUtils.forceDelete(file);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != zos) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return zipFile.getAbsolutePath();
}
//导入发布库数据
public void importInfo(List<List<String>> lists, String kbKnowledgeId, String knowledgeProjectId, String typeId, String createBy) {
for (List<String> info : lists) {
......@@ -761,4 +871,19 @@ class KnowledgeServiceImpl implements IKnowledgeService {
searchInfo + "#";
}
private List<List<String>> formatData(List<KnowledgeVO> knowledgeVOS) {
List<List<String>> list = new ArrayList<>(knowledgeVOS.size());
for (KnowledgeVO knowledgeVO : knowledgeVOS) {
List<String> innerList = new ArrayList<>();
innerList.add(knowledgeVO.getTitle());
innerList.add(knowledgeVO.getContentAll());
innerList.add(knowledgeVO.getAuthor());
innerList.add(knowledgeVO.getOrigin());
innerList.add(knowledgeVO.getPublishDate());
list.add(innerList);
}
return list;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论