feat(文档拆分): 下载导入模板
This commit is contained in:
+69
-13
@@ -10,8 +10,10 @@ import com.jero.common.constant.enums.LanguageEnum;
|
||||
import com.jero.common.constant.enums.YesOrNoEnum;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.system.vo.LoginUser;
|
||||
import com.jero.common.util.FileUtils;
|
||||
import com.jero.common.util.MessageUtils;
|
||||
import com.jero.common.util.UUIDUtils;
|
||||
import com.jero.common.util.ZipUtil;
|
||||
import com.jero.modules.laws.common.constant.FieldCommon;
|
||||
import com.jero.modules.laws.common.mapper.LawsCommonMapper;
|
||||
import com.jero.modules.laws.documenttool.common.DocumentSplitCommon;
|
||||
@@ -53,19 +55,20 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.OutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.net.URLEncoder.encode;
|
||||
|
||||
/**
|
||||
* @Author: yjz
|
||||
* @Date: 2023/10/12/23:03
|
||||
@@ -113,6 +116,9 @@ public class DocumentSplitServiceImpl implements IDocumentSplitService {
|
||||
this.sysDictItemService = sysDictItemService;
|
||||
}
|
||||
|
||||
@Value("${jero.path.exportExcelTempPath}")
|
||||
private String exportExcelTempPath;
|
||||
|
||||
@Override
|
||||
public IPage<DocumentSplitInfo> queryByPage(DocumentSplitInfo documentSplitInfo, Integer pageNo, Integer pageSize) {
|
||||
// 构建查询条件
|
||||
@@ -560,6 +566,7 @@ public class DocumentSplitServiceImpl implements IDocumentSplitService {
|
||||
@Override
|
||||
public void downloadImportTemplate(HttpServletRequest request, HttpServletResponse response) {
|
||||
try (Workbook workbook = new XSSFWorkbook()){
|
||||
String fileName = "拆分导入模版下载.xlsx";
|
||||
// sheet页
|
||||
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("拆分导入模版");
|
||||
// 列宽
|
||||
@@ -574,7 +581,7 @@ public class DocumentSplitServiceImpl implements IDocumentSplitService {
|
||||
// 水平、垂直居中
|
||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
// 问本格式
|
||||
// 文本格式
|
||||
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
|
||||
|
||||
for (int i = 0; i < titleArr.length; i++) {
|
||||
@@ -602,20 +609,69 @@ public class DocumentSplitServiceImpl implements IDocumentSplitService {
|
||||
CellRangeAddress region = new CellRangeAddress(1, 1, 0, 2);
|
||||
sheet.addMergedRegion(region);
|
||||
|
||||
// 返回流
|
||||
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Content-Disposition", "attachment;fileName=" +
|
||||
encode("拆分导入模版下载.xlsx", "UTF-8"));
|
||||
OutputStream outputStream = response.getOutputStream();
|
||||
workbook.write(outputStream);
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
// excel临时保存到本地并在同级创建文件夹
|
||||
generateExcel(workbook, fileName);
|
||||
|
||||
// 生成zip文件返回文件流
|
||||
ZipUtil.toZip(exportExcelTempPath, response.getOutputStream(), response, true);
|
||||
|
||||
//删除指定文件夹
|
||||
FileUtils.deleteFolders(exportExcelTempPath);
|
||||
}catch (Exception e){
|
||||
throw new JeroBootException("下载文件失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* private static void returnOutPutStream(HttpServletResponse response, String fileName, String zipPath) throws IOException {
|
||||
* ServletOutputStream outputStream = null;
|
||||
* try (InputStream in = Files.newInputStream(Paths.get(zipPath));){
|
||||
* response.reset();
|
||||
* response.setContentType("application/ostet-stream");
|
||||
* response.setHeader("content-type", "application/octet-stream");
|
||||
* response.setContentType("application/octet-stream");
|
||||
* response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + new String((fileName + ".zip").getBytes("GB2312"), StandardCharsets.ISO_8859_1));
|
||||
* response.setCharacterEncoding("UTF-8");
|
||||
* outputStream = response.getOutputStream();
|
||||
* //创建存放文件内容的数组
|
||||
* byte[] buff = new byte[1024];
|
||||
* //所读取的内容使用n来接收
|
||||
* int n;
|
||||
* //当没有读取完时,继续读取,循环
|
||||
* while ((n = in.read(buff)) != -1) {
|
||||
* //将字节数组的数据全部写入到输出流中
|
||||
* outputStream.write(buff, 0, n);
|
||||
* }
|
||||
* //强制将缓存区的数据进行输出
|
||||
* outputStream.println();
|
||||
* }
|
||||
* outputStream.close();
|
||||
* }
|
||||
*
|
||||
*/
|
||||
|
||||
private void generateExcel(Workbook workbook, String fileName) throws IOException {
|
||||
File saveFile = new File(exportExcelTempPath);
|
||||
// 导出路径 没有则创建
|
||||
if (!saveFile.exists()) {
|
||||
saveFile.mkdirs();
|
||||
}
|
||||
// 生成excel文件路径
|
||||
String excelPath = exportExcelTempPath + File.separator + fileName;
|
||||
// 写出excel文件
|
||||
try (FileOutputStream fos = new FileOutputStream(excelPath)){
|
||||
workbook.write(fos);
|
||||
|
||||
// 同级创建文件夹
|
||||
File savePaperFile = new File(exportExcelTempPath+ File.separator + "外部资源");
|
||||
if (!savePaperFile.exists()) {
|
||||
savePaperFile.mkdirs();
|
||||
}
|
||||
}catch (Exception e){
|
||||
throw new JeroBootException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SarFileSplitItemsValEO> queryInterpretationArticlesList(String itemId) {
|
||||
return documentSplitMapper.queryItemsValByItemId(itemId, DocumentSplitCommon.INTERPRETATION);
|
||||
|
||||
Reference in New Issue
Block a user