Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gj_common
Overview
Overview
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
weiguanying
gj_common
Commits
86dee380
Commit
86dee380
authored
Mar 21, 2023
by
weiguanying
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
国结公共模块代码上传
parent
019ef2d7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
3 deletions
+88
-3
FileController.java
...java/cn/com/brilliance/doc/controller/FileController.java
+16
-1
WordTemplateService.java
...va/cn/com/brilliance/doc/service/WordTemplateService.java
+3
-0
WordTemplateServiceImpl.java
.../brilliance/doc/service/impl/WordTemplateServiceImpl.java
+69
-2
No files found.
doc/src/main/java/cn/com/brilliance/doc/controller/FileController.java
View file @
86dee380
...
...
@@ -6,7 +6,10 @@ import cn.com.brilliance.doc.service.WordTemplateService;
import
cn.com.brilliance.doc.utils.WordToPDF
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.util.Map
;
...
...
@@ -65,7 +68,6 @@ public class FileController {
wordToPDF
.
doxcTopdf
(
filePath
,
targetPath
,
licenseKey
);
return
targetPath
;
}
/**
* word-docx模板生成word-docx
* @param paramMap map
...
...
@@ -76,4 +78,17 @@ public class FileController {
public
String
wordTemplateToWord
(
@RequestBody
Map
<
String
,
Object
>
paramMap
)
{
return
wordTemplateService
.
poiCreateWord
(
paramMap
);
}
/**
*
* 读取模板文件并生成docx返回base码预览
* @param paramMap 模板所需参数
* @param response 请求应答
* @return
* @throws IOException
*/
@PostMapping
(
"word"
)
public
String
wordConvertAndPreview
(
@RequestBody
Map
<
String
,
Object
>
paramMap
,
HttpServletResponse
response
)
throws
IOException
{
return
wordTemplateService
.
wordConvertAndPreview
(
paramMap
,
response
);
}
}
doc/src/main/java/cn/com/brilliance/doc/service/WordTemplateService.java
View file @
86dee380
...
...
@@ -2,10 +2,13 @@ package cn.com.brilliance.doc.service;
import
org.springframework.stereotype.Repository
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.util.Map
;
@Repository
public
interface
WordTemplateService
{
String
docxTemplateToDocx
(
String
templateName
);
String
poiCreateWord
(
Map
<
String
,
Object
>
paramMap
);
String
wordConvertAndPreview
(
Map
<
String
,
Object
>
paramMap
,
HttpServletResponse
response
)
throws
IOException
;
}
doc/src/main/java/cn/com/brilliance/doc/service/impl/WordTemplateServiceImpl.java
View file @
86dee380
...
...
@@ -5,6 +5,7 @@ import cn.com.brilliance.doc.service.WordTemplateService;
import
com.deepoove.poi.XWPFTemplate
;
import
fr.opensagres.xdocreport.document.json.JSONObject
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.stereotype.Service
;
import
fr.opensagres.xdocreport.document.IXDocReport
;
import
fr.opensagres.xdocreport.document.registry.XDocReportRegistry
;
...
...
@@ -12,8 +13,13 @@ import fr.opensagres.xdocreport.template.IContext;
import
fr.opensagres.xdocreport.template.TemplateEngineKind
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
sun.misc.BASE64Encoder
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.net.URLEncoder
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.UUID
;
...
...
@@ -29,6 +35,7 @@ public class WordTemplateServiceImpl implements WordTemplateService {
@Autowired
private
DocBean
docBean
;
@Override
public
String
docxTemplateToDocx
(
String
templateName
)
{
InputStream
ins
=
null
;
OutputStream
out
=
null
;
...
...
@@ -79,12 +86,13 @@ public class WordTemplateServiceImpl implements WordTemplateService {
}
/**
*
* 基于poi-tl模板引擎生成docx模板文件为docx
*
* @param paramMap 模板文件替换参数
* @return
*/
public
String
poiCreateWord
(
Map
<
String
,
Object
>
paramMap
)
{
@Override
public
String
poiCreateWord
(
Map
<
String
,
Object
>
paramMap
)
{
String
templatePath
=
docBean
.
getTemplatesPath
()
+
paramMap
.
get
(
"templateName"
);
String
targetPath
=
docBean
.
getFilePath
()
+
paramMap
.
get
(
"fileName"
);
// 读取模板渲染参数
...
...
@@ -100,4 +108,63 @@ public class WordTemplateServiceImpl implements WordTemplateService {
return
targetPath
;
}
/**
* 读取模板文件并生成docx返回base码预览和下载
* @date 2023-03-21
* @author weiguanying
* @param paramMap
* @param response
* @return
* @throws IOException
*/
@Override
public
String
wordConvertAndPreview
(
Map
<
String
,
Object
>
paramMap
,
HttpServletResponse
response
)
throws
IOException
{
File
sourceFile
=
new
File
(
docBean
.
getTemplatesPath
()
+
paramMap
.
get
(
"templateName"
));
//在模板文件中任意表格位置填充数据
Map
<
String
,
Object
>
data
=
new
HashMap
();
data
.
put
(
"title"
,
paramMap
.
get
(
"title"
));
//浏览器端下载
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
setContentType
(
"application/msword"
+
";charset=UTF-8"
);
String
fileName
=
"preview"
+
".docx"
;
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
.
concat
(
String
.
valueOf
(
URLEncoder
.
encode
(
fileName
,
"UTF-8"
))));
response
.
flushBuffer
();
BufferedInputStream
bis
=
new
BufferedInputStream
(
new
FileInputStream
(
sourceFile
));
//创建输出流
OutputStream
os
=
response
.
getOutputStream
();
//最终编译渲染并输出
XWPFTemplate
.
compile
(
sourceFile
).
render
(
data
).
writeAndClose
(
os
);
// 创建字节输出流
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
byte
[]
buffer
=
new
byte
[
1024
];
int
i
=
bis
.
read
(
buffer
);
while
(
i
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
i
);
outputStream
.
write
(
buffer
,
0
,
i
);
i
=
bis
.
read
(
buffer
);
}
// 读取输出流字节
byte
[]
bytes
=
outputStream
.
toByteArray
();
// 转BASE64编码字符串
BASE64Encoder
encoder
=
new
BASE64Encoder
();
String
base64String
=
encoder
.
encodeBuffer
(
bytes
).
trim
();
if
(
bis
!=
null
)
{
bis
.
close
();
}
if
(
os
!=
null
)
{
os
.
close
();
}
if
(
outputStream
!=
null
)
{
outputStream
.
close
();
}
System
.
out
.
println
(
base64String
);
// response.se
return
base64String
;
// return base64String.replaceAll("\n", "").replaceAll("\r", "");
}
}
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