提交 4f189a27 作者: chenshiqiang

import excel

上级 cf818d6e
......@@ -6,11 +6,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzsn.knowbase.entity.KbAuthorizedUser;
import com.zzsn.knowbase.entity.KnowFile;
import com.zzsn.knowbase.entity.Knowledge;
import com.zzsn.knowbase.entity.KnowledgeExcel;
import com.zzsn.knowbase.service.IKnowledgeService;
import com.zzsn.knowbase.service.ILocalFileService;
import com.zzsn.knowbase.util.DocUtil;
import com.zzsn.knowbase.util.HttpUtil;
import com.zzsn.knowbase.util.SpringContextUtils;
import com.zzsn.knowbase.util.*;
import com.zzsn.knowbase.vo.IntelligentQaParam;
import com.zzsn.knowbase.vo.KnowledgeParam;
import com.zzsn.knowbase.vo.KnowledgeVO;
......@@ -27,7 +26,10 @@ import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
......@@ -178,6 +180,25 @@ public class KnowledgeController {
*/
@RequestMapping(value = "/importInfo", method = RequestMethod.POST)
public Result<?> importInfo(HttpServletRequest request, HttpServletResponse response) {
return knowledgeService.doImportInfo(request);
KbAuthorizedUser userInfo = SpringContextUtils.getUserInfo();
assert userInfo != null;
knowledgeService.doImportInfo(request,userInfo.getId());
return Result.OK("导入正在后台进行");
}
/**
* 下载导入模板
*/
@RequestMapping(value = "/download")
public Result<?> download(HttpServletRequest request, HttpServletResponse response) {
try {
File fileDir = ExcelExportUtil.getFileDir();
List<KnowledgeExcel> dataList = new ArrayList<>();
String filePath = fileDir.getAbsolutePath() + File.separator + "info.xlsx";
ExcelExportUtil.writeExcelFront(dataList, "知识库列表导入模板", filePath, KnowledgeExcel.class);
ExcelExportUtil.download(response, filePath, true);
} catch (Exception e) {
e.printStackTrace();
}
return Result.OK();
}
}
package com.zzsn.knowbase.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jeecgframework.poi.excel.annotation.Excel;
import java.io.Serializable;
import java.util.List;
/**
* @Description: 知识
* @Author: chenshiqiang
* @Version: V1.0
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class KnowledgeExcel {
/**
* 标题
*/
@ExcelProperty(value = "标题", index = 0)
private String title;
@ExcelProperty(value = "正文", index = 1)
private String content;
/**
* 作者
*/
@ExcelProperty(value = "作者", index = 2)
private String author;
/**
* 来源
*/
@ExcelProperty(value = "来源", index = 3)
private String origin;
/**
* 发布时间
*/
@ExcelProperty(value = "发布时间", index = 4)
private String publishDate;
}
......@@ -35,6 +35,7 @@ public class KnowledgeMessage {
* 审核状态
*/
private Integer verifyStatus;
private Integer verifyTime;
/**
* 审核人名字
*/
......
......@@ -49,7 +49,7 @@ public interface IKnowledgeService {
KnowledgeVO getById(String id);
Result<?> doImportInfo(HttpServletRequest request);
Result<?> doImportInfo(HttpServletRequest request,String userId);
void doExcel(HttpServletRequest request, String fileSuffix);
void doExcel(HttpServletRequest request, String fileSuffix,String userId);
}
......@@ -23,12 +23,8 @@ public class AsyncService {
//异步执行文件解析
@Async
public void doimport(HttpServletRequest request, MultipartFile multipartFile, String fileSuffix) {
String subjectId = request.getParameter("subjectId");
String isTopping = request.getParameter("isTopping");
String isExamine = request.getParameter("isExamine");
knowledgeService.doExcel(request, fileSuffix);
public void doimport(HttpServletRequest request, String fileSuffix,String userId) {
knowledgeService.doExcel(request, fileSuffix,userId);
}
......
......@@ -155,12 +155,6 @@ class KnowledgeServiceImpl implements IKnowledgeService {
KnowledgeMessage knowledgeMessage = new KnowledgeMessage();
BeanUtils.copyProperties(knowledge, knowledgeMessage);
knowledgeMessage.setType(knowledge.getTypeId());
// CleanerProperties props = new CleanerProperties();
// props.setPruneTags("table");
// String htmlWithoutTable = new HtmlCleaner(props).clean(html).getText().toString();
// htmlWithoutTable = htmlWithoutTable.replace("<p>", "");
// List<String>contentStringWithoutTableList = Arrays.asList(htmlWithoutTable.split("</p>"));
knowledgeMessage.setContents(contentList);
produceInfo.sendKnowledgeContents(knowledgeMessage);
}
......@@ -456,7 +450,7 @@ class KnowledgeServiceImpl implements IKnowledgeService {
}
@Override
public Result<?> doImportInfo(HttpServletRequest request) {
public Result<?> doImportInfo(HttpServletRequest request,String userId) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
if (fileMap.size() < 1) {
......@@ -466,7 +460,7 @@ class KnowledgeServiceImpl implements IKnowledgeService {
int index = multipartFile.getOriginalFilename().lastIndexOf(".");
String fileSuffix = multipartFile.getOriginalFilename().substring(index + 1);
if ("doc".equals(fileSuffix) || "docx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) {
asyncService.doimport(request, multipartFile, fileSuffix);
asyncService.doimport(request, fileSuffix,userId);
return Result.OK("已进行处理");
} else {
return Result.error("不支持的文件类型");
......@@ -475,11 +469,10 @@ class KnowledgeServiceImpl implements IKnowledgeService {
}
@Override
public void doExcel(HttpServletRequest request, String fileSuffix) {
public void doExcel(HttpServletRequest request, String fileSuffix,String userId) {
String kbKnowledgeId = request.getParameter("kbKnowledgeId");
String knowledgeProjectId = request.getParameter("knowledgeProjectId");
String typeId = request.getParameter("typeId");
String createBy = request.getParameter("createBy");
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
......@@ -488,9 +481,9 @@ class KnowledgeServiceImpl implements IKnowledgeService {
// 将MultipartFile的文件内容保存到字节数组
byte[] fileData = multipartFile.getBytes();
List<List<String>> lists = ExcelExportUtil.readExcel(new ByteArrayInputStream(fileData), 1, 9);
List<List<String>> lists = ExcelExportUtil.readExcel(new ByteArrayInputStream(fileData), 1, 5);
//存入es
importInfo(lists, kbKnowledgeId, knowledgeProjectId, typeId, createBy);
importInfo(lists, kbKnowledgeId, knowledgeProjectId, typeId, userId);
} catch (Exception e) {
e.printStackTrace();
}
......@@ -508,26 +501,37 @@ class KnowledgeServiceImpl implements IKnowledgeService {
.importData(1)
.build();
specialInformation.setId(codeGenerateUtil.geneIdNo(Constants.FINANCE, 8));
//判断正文是否为空
if (StringUtils.isEmpty(info.get(3))) {
log.error("上传的数据为空");
return;
if (StringUtils.isNotEmpty(info.get(0))) {
specialInformation.setTitle(info.get(0));
}else {
log.error("上传的数据{}标题为空,此条数据忽略",info.get(0));
continue;
}
if (StringUtils.isNotEmpty(info.get(1))) {
specialInformation.setTitle(info.get(1));
specialInformation.setContents(Collections.singletonList(Content.builder()
.contentId(codeGenerateUtil.geneIdNo(Constants.FINANCE, 8))
.content(info.get(1)).build()));
}else {
log.error("上传的数据{}正文为空,此条数据忽略",info.get(0));
continue;
}
if (StringUtils.isNotEmpty(info.get(4))) {
specialInformation.setAuthor(info.get(4));
if (StringUtils.isNotEmpty(info.get(2))) {
specialInformation.setAuthor(info.get(2));
}
if (StringUtils.isNotEmpty(info.get(5))) {
specialInformation.setOrigin(info.get(5));
if (StringUtils.isNotEmpty(info.get(3))) {
specialInformation.setOrigin(info.get(3));
}
if (StringUtils.isNotEmpty(info.get(6))) {
specialInformation.setPublishDate(EsDateUtil.esFieldDateFormat(info.get(6)));
if (StringUtils.isNotEmpty(info.get(4))) {
specialInformation.setPublishDate(EsDateUtil.esFieldDateFormat(info.get(4)));
}
specialInformation.setDeleteFlag(0);
specialInformation.setCreateTime(cn.hutool.core.date.DateUtil.format(new Date(), "yyyy-MM-dd'T'HH:mm:ss"));
esOpUtil.docSavaByEntity(Constants.ES_DATA_FOR_KNOWLEDGE, specialInformation.getId(), specialInformation);
KnowledgeMessage knowledgeMessage = new KnowledgeMessage();
BeanUtils.copyProperties(specialInformation, knowledgeMessage);
knowledgeMessage.setType(specialInformation.getTypeId());
knowledgeMessage.setContents(specialInformation.getContents());
produceInfo.sendKnowledgeContents(knowledgeMessage);
} catch (NumberFormatException e) {
log.error("处理异常");
}
......
package com.zzsn.knowbase.util;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.zzsn.knowbase.entity.KnowledgeExcel;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
......@@ -66,4 +77,118 @@ public class ExcelExportUtil {
return xssfCell.getStringCellValue();
}
}
public static File getFileDir() {
// 构建上传文件的存放 "文件夹" 路径
String fileDirPath = new String("src/main/resources/static/uploadFiles" );
File fileDir = new File(fileDirPath);
if (!fileDir.exists()) {
// 递归生成文件夹
fileDir.mkdirs();
}
return fileDir;
}
/**
* 导出
* @param data
* @param sheetName
* @param filePath
* @param clazz
* @throws Exception
*/
public static Boolean writeExcelFront(List data, String sheetName, String filePath, Class clazz) throws Exception {
//表头样式
System.out.println("开始写入");
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置表头居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置内容靠左对齐
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
int total = data.size();//总数
int max = 50000;//每sheet页允许最大数
int avg = total / max;//sheet页个数
ExcelWriter excelWriter= EasyExcel.write(filePath, clazz).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(horizontalCellStyleStrategy).build();
try{
for (int j = 0; j < avg + 1; j++) {
String sheetnames=sheetName;
int num = j * max;
List dataList1 = new ArrayList<>();
if (StringUtils.isNotBlank(sheetnames)) {
sheetnames = sheetnames + (j + 1);
} else {
sheetnames = j + "";
}
for (int n = num; n < total; n++) {//n即为每个sheet页应该开始的数
if(n>=num+50000){
break;
}
dataList1.add(data.get(n));//从总的list数据里面取出该处于哪个sheet页的数据,然后加进exportList,exportList即为当前sheet页应该有的数据
}
WriteSheet writeSheet = EasyExcel.writerSheet(j, sheetnames).build();
excelWriter.write(dataList1,writeSheet);
}
}finally {
// 千万别忘记finish 会帮忙关闭流
if (excelWriter != null) {
excelWriter.finish();
}
}
System.out.println("写入完成");
return true;
}
public static void download(HttpServletResponse response, String filePath, boolean delete) throws UnsupportedEncodingException {
File file = new File(filePath);
if (file.exists()) {
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(), "utf8"));
//加上设置大小 下载下来的excel文件才不会在打开前提示修复
response.addHeader("Content-Length", String.valueOf(file.length()));
byte[] buffer = new byte[1024];
//输出流
OutputStream os = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer);
os.flush();
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (delete) {
//文件下载结束后,删除上传到项目的文件
file.delete();
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论