mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-09-27 17:14:03 +08:00
1.not copy template; 2.fixed pinTable bug
This commit is contained in:
@ -1,13 +1,10 @@
|
||||
package ai.chat2db.server.web.api.controller.rdb.doc.constant;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
/**
|
||||
* CommonConstant
|
||||
*
|
||||
* @author lzy
|
||||
**/
|
||||
@Log
|
||||
public final class CommonConstant {
|
||||
/**
|
||||
* 表head
|
||||
|
@ -14,7 +14,7 @@ import lombok.SneakyThrows;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -86,8 +86,7 @@ public class ExportHtmlService extends DatabaseExportService {
|
||||
}
|
||||
catalogue.append("</li>");
|
||||
|
||||
String filePath = GlobalDict.templateDir + GlobalDict.TEMPLATE_FILE.get(0);
|
||||
try (FileInputStream inputStream = new FileInputStream(filePath);
|
||||
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/" + GlobalDict.TEMPLATE_FILE.get(0));
|
||||
ByteArrayOutputStream result = new ByteArrayOutputStream()) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
|
@ -16,7 +16,7 @@ import com.deepoove.poi.data.Rows;
|
||||
import com.deepoove.poi.data.Tables;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -45,9 +45,8 @@ public class ExportWordSuperService extends DatabaseExportService {
|
||||
@Override
|
||||
public void export(OutputStream outputStream, ExportOptions exportOptions) {
|
||||
boolean isExportIndex = exportOptions.getIsExportIndex();
|
||||
String filePath = GlobalDict.templateDir + GlobalDict.TEMPLATE_FILE.get(1);
|
||||
String subFile = GlobalDict.templateDir + GlobalDict.TEMPLATE_FILE.get(2);
|
||||
File importWordFile = new File(filePath);
|
||||
InputStream filePath = this.getClass().getClassLoader().getResourceAsStream("template/" + GlobalDict.TEMPLATE_FILE.get(1));
|
||||
InputStream subFile = this.getClass().getClassLoader().getResourceAsStream("template/" + GlobalDict.TEMPLATE_FILE.get(2));
|
||||
Map<String, List<Map.Entry<String, List<TableParameter>>>> allMap = listMap.entrySet()
|
||||
.stream().collect(Collectors.groupingBy(v -> v.getKey().split("---")[0]));
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
@ -86,9 +85,9 @@ public class ExportWordSuperService extends DatabaseExportService {
|
||||
list.add(tableData);
|
||||
}
|
||||
}
|
||||
myDataMap.put("mydata", Includes.ofLocal(subFile).setRenderModel(list).create());
|
||||
myDataMap.put("mydata", Includes.ofStream(subFile).setRenderModel(list).create());
|
||||
/*根据模板生成文档*/
|
||||
XWPFTemplate template = XWPFTemplate.compile(importWordFile).render(myDataMap);
|
||||
XWPFTemplate template = XWPFTemplate.compile(filePath).render(myDataMap);
|
||||
AddToTopic.generateTOC(template.getXWPFDocument(), outputStream);
|
||||
}
|
||||
|
||||
|
@ -1,56 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.rdb.factory;
|
||||
|
||||
import ai.chat2db.server.tools.common.util.ConfigUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CopyTemplate
|
||||
*
|
||||
* @author lzy
|
||||
**/
|
||||
@Slf4j
|
||||
public class CopyTemplate {
|
||||
/**
|
||||
* 模板文件
|
||||
**/
|
||||
private static final List<String> TEMPLATE_FILE = Arrays.asList("template.html", "template_diy.docx", "sub_template_diy.docx");
|
||||
|
||||
public static void copyTemplateFile() {
|
||||
String templateDir = ConfigUtils.CONFIG_BASE_PATH + File.separator + "template";
|
||||
File file = new File(templateDir);
|
||||
if (!file.exists()) {
|
||||
file.mkdir();
|
||||
}
|
||||
for (String template : TEMPLATE_FILE) {
|
||||
saveFile(templateDir, template, true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveFile(String dir, String path, boolean isOverride) {
|
||||
if (!isOverride) {
|
||||
File file = new File(dir + File.separator + path);
|
||||
if (file.exists()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
try (// 模板文件输入输出地址 读取resources下文件
|
||||
FileOutputStream outputStream = new FileOutputStream(dir + File.separator + path);
|
||||
//返回读取指定资源的输入流
|
||||
InputStream inputStream = CopyTemplate.class.getClassLoader().getResourceAsStream("template/" + path)) {
|
||||
byte[] buffer = new byte[4096];
|
||||
int n = 0;
|
||||
while (-1 != (n = inputStream.read(buffer))) {
|
||||
outputStream.write(buffer, 0, n);
|
||||
}
|
||||
outputStream.flush();
|
||||
} catch (Exception e) {
|
||||
log.error("saveFile error", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,6 @@ public class ExportServiceFactory {
|
||||
REPORT_POOL.put(ExportTypeEnum.MARKDOWN.name(), ExportMarkdownService.class);
|
||||
REPORT_POOL.put(ExportTypeEnum.HTML.name(), ExportHtmlService.class);
|
||||
REPORT_POOL.put(ExportTypeEnum.PDF.name(), ExportPdfService.class);
|
||||
CopyTemplate.copyTemplateFile();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user