Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-api
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-api
Commits
7eeb97f0
Commit
7eeb97f0
authored
Mar 07, 2025
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:内容导出
parent
57b527df
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
171 additions
and
2 deletions
+171
-2
pom.xml
pom.xml
+12
-2
MarkdownToWordDto.java
src/main/java/cn/com/poc/expose/dto/MarkdownToWordDto.java
+18
-0
Markdown2WordRest.java
src/main/java/cn/com/poc/expose/rest/Markdown2WordRest.java
+21
-0
Markdown2WordRestImpl.java
...va/cn/com/poc/expose/rest/impl/Markdown2WordRestImpl.java
+66
-0
KnowledgeServiceTest.java
src/test/java/cn/com/poc/knowledge/KnowledgeServiceTest.java
+54
-0
No files found.
pom.xml
View file @
7eeb97f0
...
...
@@ -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>
...
...
src/main/java/cn/com/poc/expose/dto/MarkdownToWordDto.java
0 → 100644
View file @
7eeb97f0
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
;
}
}
src/main/java/cn/com/poc/expose/rest/Markdown2WordRest.java
0 → 100644
View file @
7eeb97f0
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
;
}
src/main/java/cn/com/poc/expose/rest/impl/Markdown2WordRestImpl.java
0 → 100644
View file @
7eeb97f0
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"
);
}
}
src/test/java/cn/com/poc/knowledge/KnowledgeServiceTest.java
View file @
7eeb97f0
...
...
@@ -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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment