feat:add

This commit is contained in:
nieziyan 2023-10-27 19:45:03 +08:00
parent 789a67bd03
commit 8dd0ddd2dd
2 changed files with 62 additions and 39 deletions

View File

@ -1,7 +1,9 @@
package org.jeecg.common.util;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
@ -15,6 +17,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -102,9 +106,23 @@ public class ExportUtil {
String pathPrefix = "excelTemplate/";
String path = pathPrefix + template;
InputStream inputStream = classPathStream(path);
String templatePath = "";
return new TemplateExportParams(templatePath);
String tempDir = System.getProperty("java.io.tmpdir");
String tempPath = tempDir + File.separator + template;
try {
File tempFile = File.createTempFile(tempPath, null);
FileUtils.copyInputStreamToFile(inputStream, tempFile);
String templatePath = tempFile.getAbsolutePath();
return new TemplateExportParams(templatePath);
} catch (IOException e) {
e.printStackTrace();
return null;
}finally {
try {
if (ObjectUtil.isNotNull(inputStream)) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void exportXls(HttpServletResponse response, String template,

View File

@ -15,6 +15,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.cache.Cache;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.shiro.SecurityUtils;
@ -4398,45 +4399,49 @@ public class SpectrumAnalysisServiceImpl implements ISpectrumAnalysisService {
analyze.put("gasRoi", roiChannels(roiChannelsDtosG));
analyze.putAll(roiResults(roiResultsDtos));
analyze.put("resultSummary", xeResults(xeResultsDtos));
// 使数据适配导出模板
String pathPrefix = "excelTemplate/";
String path = pathPrefix + SAVETOTXT.getName();
String template = ClassUtil.classPath(path);
List<String> lines = FileUtil.readUtf8Lines(template);
// 正则表达式匹配${}中的内容
String regex = "\\$\\{([^}]+)}";
List<String> newLines = new ArrayList<>();
List<String> list = ListUtil.toList("sampleNewCalibration","sampleRoi", "detNewCalibration", "detRoi", "gasNewCalibration", "gasRoi","grossRoi",
"netRoi", "concRoi", "resultSummary");
List<String> skip = ListUtil.toList("${sampleNewCalibration}","${sampleRoi}", "${detNewCalibration}", "${detRoi}", "${gasNewCalibration}", "${gasRoi}",
"${grossRoi}", "${netRoi}", "${concRoi}", "${resultSummary}");
for (String line : lines) {
List<String> fieldNames = ReUtil.findAllGroup1(regex, line);
if (CollUtil.isEmpty(fieldNames)){
newLines.add(line);
continue;
}
for (String fieldName : fieldNames) {
Object value = analyze.get(fieldName);
if (CollUtil.contains(list, fieldName)){
newLines.addAll((List<String>)value);
}else {
String search = "${" + fieldName + "}";
String replacement = StrUtil.toString(value);
replacement = StrUtil.isBlank(replacement) ? "null" : replacement;
line = StrUtil.replace(line, search, replacement);
}
}
if (!CollUtil.contains(skip, line))
newLines.add(line);
}
PrintWriter writer = null;
try {
String export = "SaveToTxt.txt";
writer = ExportUtil.streamWriter(response, export);
for (String newLine : newLines) {
writer.println(newLine);
// 使数据适配导出模板
String pathPrefix = "excelTemplate/";
String path = pathPrefix + SAVETOTXT.getName();
/*String template = ClassUtil.classPath(path);
List<String> lines = FileUtil.readUtf8Lines(template);*/
InputStream inputStream = ClassUtil.classPathStream(path);
List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
// 正则表达式匹配${}中的内容
String regex = "\\$\\{([^}]+)}";
List<String> newLines = new ArrayList<>();
List<String> list = ListUtil.toList("sampleNewCalibration","sampleRoi", "detNewCalibration", "detRoi", "gasNewCalibration", "gasRoi","grossRoi",
"netRoi", "concRoi", "resultSummary");
List<String> skip = ListUtil.toList("${sampleNewCalibration}","${sampleRoi}", "${detNewCalibration}", "${detRoi}", "${gasNewCalibration}", "${gasRoi}",
"${grossRoi}", "${netRoi}", "${concRoi}", "${resultSummary}");
for (String line : lines) {
List<String> fieldNames = ReUtil.findAllGroup1(regex, line);
if (CollUtil.isEmpty(fieldNames)){
newLines.add(line);
continue;
}
for (String fieldName : fieldNames) {
Object value = analyze.get(fieldName);
if (CollUtil.contains(list, fieldName)){
newLines.addAll((List<String>)value);
}else {
String search = "${" + fieldName + "}";
String replacement = StrUtil.toString(value);
replacement = StrUtil.isBlank(replacement) ? "null" : replacement;
line = StrUtil.replace(line, search, replacement);
}
}
if (!CollUtil.contains(skip, line))
newLines.add(line);
}
String export = "SaveToTxt.txt";
writer = ExportUtil.streamWriter(response, export);
for (String newLine : newLines) {
writer.println(newLine);
}
} catch (IOException e) {
e.printStackTrace();
}finally {