Commit 7eeb97f0 authored by alex yao's avatar alex yao

feat:内容导出

parent 57b527df
......@@ -212,13 +212,13 @@
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
......@@ -345,6 +345,16 @@
<artifactId>jsoup</artifactId>
<version>1.19.1</version>
</dependency>
<!-- flexmark-java for parsing Markdown -->
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-all</artifactId>
<version>0.62.2</version>
</dependency>
<!-- Apache POI for generating Word documents -->
</dependencies>
......
package cn.com.poc.expose.dto;
/**
* @author alex.yao
* @date 2025/3/7
*/
public class MarkdownToWordDto {
private String markdown;
public String getMarkdown() {
return markdown;
}
public void setMarkdown(String markdown) {
this.markdown = markdown;
}
}
package cn.com.poc.expose.rest;
import cn.com.poc.expose.dto.MarkdownToWordDto;
import cn.com.yict.framemax.core.rest.BaseRest;
import cn.com.yict.framemax.web.permission.Access;
import cn.com.yict.framemax.web.permission.Permission;
import org.springframework.web.bind.annotation.RequestBody;
import java.io.IOException;
/**
* @author alex.yao
* @date 2025/3/7
*/
@Permission(Access.Anonymous)
public interface Markdown2WordRest extends BaseRest {
String markdown2Word(@RequestBody MarkdownToWordDto dto) throws IOException;
}
package cn.com.poc.expose.rest.impl;
import cn.com.poc.common.service.BosConfigService;
import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.common.utils.UUIDTool;
import cn.com.poc.expose.dto.MarkdownToWordDto;
import cn.com.poc.expose.rest.Markdown2WordRest;
import cn.com.yict.framemax.core.exception.BusinessException;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.*;
import java.util.UUID;
/**
* @author alex.yao
* @date 2025/3/7
*/
@Component
public class Markdown2WordRestImpl implements Markdown2WordRest {
@Resource
private BosConfigService bosConfigService;
@Override
public String markdown2Word(MarkdownToWordDto dto) throws IOException {
String markdown = dto.getMarkdown();
if (StringUtils.isBlank(markdown)) {
throw new BusinessException("markdown is blank");
}
String htmlContent = convertMarkdownToHtml(markdown);
return convertHtmlToWord(htmlContent);
}
private String convertMarkdownToHtml(String markdown) {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder().build();
Document document = parser.parse(markdown);
return renderer.render(document);
}
private String convertHtmlToWord(String html) throws IOException {
File file = File.createTempFile(UUIDTool.getUUID(), ".docx");
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(html);
FileOutputStream outputStream = new FileOutputStream(file);
ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes());//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(outputStream);
FileInputStream fileInputStream = new FileInputStream(file);
return bosConfigService.upload(fileInputStream, "docx", "application/msword");
}
}
......@@ -2,6 +2,14 @@ package cn.com.poc.knowledge;
import cn.com.poc.knowledge.aggregate.KnowledgeService;
import cn.com.yict.framemax.core.spring.SingleContextInitializer;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.util.ast.Document;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -9,8 +17,12 @@ import org.mockito.MockitoAnnotations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.vladsch.flexmark.parser.Parser;
import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author alex.yao
......@@ -34,4 +46,46 @@ public class KnowledgeServiceTest {
public void test_delDocument() {
knowledgeService.delDocument(138, 1071);
}
// @Test
public static void main(String[] args) throws IOException {
// Document doc = new Document("Input.md");
// doc.save("Output.docx");
String markdownContent = readMarkdownFile("C:\\Users\\52747\\Desktop\\1.md");
String htmlContent = convertMarkdownToHtml(markdownContent);
convertHtmlToWord(htmlContent, "C:\\Users\\52747\\Desktop\\1.docx");
}
private static String readMarkdownFile(String filePath) {
try {
return new String(Files.readAllBytes(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
private static String convertMarkdownToHtml(String markdown) {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder().build();
Document document = parser.parse(markdown);
return renderer.render(document);
}
private static void convertHtmlToWord(String html, String outputPath) throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(html);
FileOutputStream outputStream = new FileOutputStream(outputPath);
ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes());//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(outputStream);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment