Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
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
徐少华
midjourney-proxy
Commits
8030520d
Commit
8030520d
authored
Jun 03, 2019
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
错题本发UAT
parent
71055296
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
427 additions
and
463 deletions
+427
-463
TranscodeFromEnum.java
.../java/com/pcloud/common/core/enums/TranscodeFromEnum.java
+6
-1
AppTypeEnum.java
...on/src/main/java/com/pcloud/common/enums/AppTypeEnum.java
+2
-2
WordUtils.java
...src/main/java/com/pcloud/common/utils/word/WordUtils.java
+419
-460
No files found.
pcloud-common-core/src/main/java/com/pcloud/common/core/enums/TranscodeFromEnum.java
View file @
8030520d
...
@@ -62,7 +62,12 @@ public enum TranscodeFromEnum {
...
@@ -62,7 +62,12 @@ public enum TranscodeFromEnum {
/**
/**
* 错题本-答案
* 错题本-答案
*/
*/
NOTE_ANSWER
(
"NOTE_ANSWER"
);
NOTE_ANSWER
(
"NOTE_ANSWER"
),
/**
* 错题本-打印
*/
NOTE_PRINT
(
"NOTE_PRINT"
);
/**
/**
* 值
* 值
...
...
pcloud-common/src/main/java/com/pcloud/common/enums/AppTypeEnum.java
View file @
8030520d
...
@@ -3,11 +3,11 @@
...
@@ -3,11 +3,11 @@
*/
*/
package
com
.
pcloud
.
common
.
enums
;
package
com
.
pcloud
.
common
.
enums
;
import
com.google.common.collect.Maps
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Map
;
import
java.util.Map
;
import
com.google.common.collect.Maps
;
/**
/**
* @描述:应用类型常量枚举
* @描述:应用类型常量枚举
* @作者:songx
* @作者:songx
...
...
pcloud-common/src/main/java/com/pcloud/common/utils/word/WordUtils.java
View file @
8030520d
/**
/**
*
*
*/
*/
package
com
.
pcloud
.
common
.
utils
.
word
;
package
com
.
pcloud
.
common
.
utils
.
word
;
...
@@ -55,481 +55,440 @@ import com.pcloud.common.utils.word.po.WordPO;
...
@@ -55,481 +55,440 @@ import com.pcloud.common.utils.word.po.WordPO;
import
com.pcloud.common.utils.word.po.WordTablePO
;
import
com.pcloud.common.utils.word.po.WordTablePO
;
/**
/**
*
*
* @author:songx
* @author:songx
* @date:2018年1月12日,下午4:19:51
* @date:2018年1月12日,下午4:19:51
*/
*/
public
class
WordUtils
{
public
class
WordUtils
{
/**
/**
*
*
*/
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
WordUtils
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
WordUtils
.
class
);
/**
/**
* 创建WORD
* 创建WORD
*
*
* @param pdfChunkD
Os
* @param wordP
Os
* @return
* @return
* @throws Exception
* @throws Exception
*/
*/
public
static
String
create
(
List
<
WordPO
>
wordPOs
)
throws
FileException
{
public
static
String
create
(
List
<
WordPO
>
wordPOs
)
throws
FileException
{
LOGGER
.
info
(
"【WORD】创建WORD文档,<START>"
);
LOGGER
.
info
(
"【WORD】创建WORD文档,<START>"
);
if
(
ListUtils
.
isEmpty
(
wordPOs
))
{
if
(
ListUtils
.
isEmpty
(
wordPOs
))
{
return
null
;
return
null
;
}
}
String
outPath
=
null
;
String
outPath
=
null
;
Document
document
=
null
;
Document
document
=
null
;
OutputStream
os
=
null
;
OutputStream
os
=
null
;
RtfWriter2
rtfWriter2
=
null
;
RtfWriter2
rtfWriter2
=
null
;
try
{
try
{
// 输出路径
// 输出路径
outPath
=
FilePathConst
.
WORD_PATH
+
UUIDUitl
.
taskName
()
+
".doc"
;
outPath
=
FilePathConst
.
WORD_PATH
+
UUIDUitl
.
taskName
()
+
".doc"
;
FileUtils
.
creatFiles
(
outPath
);
FileUtils
.
creatFiles
(
outPath
);
// 创建输出流
// 创建输出流
os
=
new
FileOutputStream
(
new
File
(
outPath
));
os
=
new
FileOutputStream
(
new
File
(
outPath
));
// 初始化文档
// 初始化文档
// 创建文档实例
// 创建文档实例
// 创建word文档,并设置纸张的大小
// 创建word文档,并设置纸张的大小
document
=
new
Document
(
PageSize
.
A4
);
document
=
new
Document
(
PageSize
.
A4
);
rtfWriter2
=
RtfWriter2
.
getInstance
(
document
,
os
);
rtfWriter2
=
RtfWriter2
.
getInstance
(
document
,
os
);
document
.
open
();
document
.
open
();
// 添加内容添加PDF实例中
// 添加内容添加PDF实例中
addChunks
(
wordPOs
,
document
);
addChunks
(
wordPOs
,
document
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
FileUtils
.
deleteFile
(
outPath
);
FileUtils
.
deleteFile
(
outPath
);
LOGGER
.
error
(
"【WORD】创建WORD文档失败:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【WORD】创建WORD文档失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
e
.
getMessage
());
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
e
.
getMessage
());
}
finally
{
}
finally
{
close
(
document
,
os
,
rtfWriter2
);
close
(
document
,
os
,
rtfWriter2
);
}
}
LOGGER
.
info
(
"【WORD】创建WORD文档,<END>"
);
LOGGER
.
info
(
"【WORD】创建WORD文档,<END>"
);
return
outPath
;
return
outPath
;
}
}
/**
/**
* 添加内容到PDF中
* 添加内容到PDF中
*
*
* @param pdf
POs
* @param word
POs
* @param document
* @param document
* @throws BizException
* @throws BizException
* @throws DocumentException
* @throws DocumentException
* @throws MalformedURLException
* @throws MalformedURLException
* @throws IOException
* @throws IOException
*/
*/
private
static
void
addChunks
(
List
<
WordPO
>
wordPOs
,
Document
document
)
private
static
void
addChunks
(
List
<
WordPO
>
wordPOs
,
Document
document
)
throws
BizException
,
DocumentException
,
MalformedURLException
,
IOException
{
throws
BizException
,
DocumentException
,
MalformedURLException
,
IOException
{
for
(
WordPO
wordPO
:
wordPOs
)
{
for
(
WordPO
wordPO
:
wordPOs
)
{
if
(
wordPO
instanceof
WordChunkPO
)
{
if
(
wordPO
instanceof
WordChunkPO
)
{
addChunk
(
wordPO
,
document
);
addChunk
(
wordPO
,
document
);
}
else
if
(
wordPO
instanceof
WordTablePO
)
{
}
else
if
(
wordPO
instanceof
WordTablePO
)
{
addTable
(
wordPO
,
document
);
addTable
(
wordPO
,
document
);
}
else
if
(
wordPO
instanceof
WordImagePO
)
{
}
else
if
(
wordPO
instanceof
WordImagePO
)
{
addImage
(
wordPO
,
document
);
addImage
(
wordPO
,
document
);
}
}
}
}
}
}
/**
/**
* 添加块
* 添加块
*
*
* @param pdf
PO
* @param word
PO
* @throws BizException
* @throws BizException
* @throws DocumentException
* @throws DocumentException
*/
*/
private
static
void
addChunk
(
WordPO
wordPO
,
Document
document
)
throws
BizException
,
DocumentException
{
private
static
void
addChunk
(
WordPO
wordPO
,
Document
document
)
throws
BizException
,
DocumentException
{
WordChunkPO
wordChunkPO
=
(
WordChunkPO
)
wordPO
;
WordChunkPO
wordChunkPO
=
(
WordChunkPO
)
wordPO
;
// 段落
// 段落
Paragraph
paragraph
=
null
;
Paragraph
paragraph
=
null
;
Font
font
=
WordFontUtils
.
getByType
(
wordChunkPO
.
getFont
());
Font
font
=
WordFontUtils
.
getByType
(
wordChunkPO
.
getFont
());
if
(
wordChunkPO
.
isLeanding
())
{
if
(
wordChunkPO
.
isLeanding
())
{
paragraph
=
new
Paragraph
(
wordChunkPO
.
getText
(),
font
);
paragraph
=
new
Paragraph
(
wordChunkPO
.
getText
(),
font
);
paragraph
.
setLeading
(
wordChunkPO
.
getLeading
()
>
0
?
wordChunkPO
.
getLeading
()
:
Float
.
NaN
);
paragraph
.
setLeading
(
wordChunkPO
.
getLeading
()
>
0
?
wordChunkPO
.
getLeading
()
:
Float
.
NaN
);
}
else
{
}
else
{
paragraph
=
new
Paragraph
();
paragraph
=
new
Paragraph
();
// 短语
// 短语
Phrase
phrase
=
new
Phrase
();
Phrase
phrase
=
new
Phrase
();
// 短语中的某一块
// 短语中的某一块
Chunk
chunk
=
new
Chunk
(
StringUtil
.
isEmpty
(
wordChunkPO
.
getText
())
?
""
:
wordChunkPO
.
getText
(),
font
);
Chunk
chunk
=
new
Chunk
(
StringUtil
.
isEmpty
(
wordChunkPO
.
getText
())
?
""
:
wordChunkPO
.
getText
(),
font
);
// skew大于0 的时候生效.字体倾斜角度
// skew大于0 的时候生效.字体倾斜角度
if
(
wordChunkPO
.
getSkew
()
>
0
)
{
if
(
wordChunkPO
.
getSkew
()
>
0
)
{
chunk
.
setSkew
(
0
,
wordChunkPO
.
getSkew
());
chunk
.
setSkew
(
0
,
wordChunkPO
.
getSkew
());
}
}
// 将块添加到短语
// 将块添加到短语
phrase
.
add
(
chunk
);
phrase
.
add
(
chunk
);
// 将短语添加到段落
// 将短语添加到段落
paragraph
.
add
(
phrase
);
paragraph
.
add
(
phrase
);
}
}
// 段落前间距
// 段落前间距
paragraph
.
setSpacingBefore
(
wordChunkPO
.
getSpacingBefore
()
>
0
?
wordChunkPO
.
getSpacingBefore
()
:
Float
.
NaN
);
paragraph
.
setSpacingBefore
(
wordChunkPO
.
getSpacingBefore
()
>
0
?
wordChunkPO
.
getSpacingBefore
()
:
Float
.
NaN
);
// 段落后间距
// 段落后间距
paragraph
.
setSpacingAfter
(
wordChunkPO
.
getSpacingAfter
()
>
0
?
wordChunkPO
.
getSpacingAfter
()
:
Float
.
NaN
);
paragraph
.
setSpacingAfter
(
wordChunkPO
.
getSpacingAfter
()
>
0
?
wordChunkPO
.
getSpacingAfter
()
:
Float
.
NaN
);
// 段落位置
// 段落位置
paragraph
.
setAlignment
(
wordChunkPO
.
getFontAlign
());
paragraph
.
setAlignment
(
wordChunkPO
.
getFontAlign
());
// 将段落添加到文档
// 将段落添加到文档
document
.
add
(
paragraph
);
document
.
add
(
paragraph
);
}
}
/**
/**
* 添加表格
* 添加表格
*
*
* @param pdf
PO
* @param word
PO
* @throws BizException
* @throws BizException
* @throws DocumentException
* @throws DocumentException
*/
*/
private
static
void
addTable
(
WordPO
wordPO
,
Document
document
)
throws
BizException
,
DocumentException
{
private
static
void
addTable
(
WordPO
wordPO
,
Document
document
)
throws
BizException
,
DocumentException
{
WordTablePO
wordTablePO
=
(
WordTablePO
)
wordPO
;
WordTablePO
wordTablePO
=
(
WordTablePO
)
wordPO
;
String
[][]
contents
=
wordTablePO
.
getContents
();
String
[][]
contents
=
wordTablePO
.
getContents
();
if
(
contents
==
null
||
contents
.
length
<
1
)
{
if
(
contents
==
null
||
contents
.
length
<
1
)
{
return
;
return
;
}
}
int
rowNum
=
contents
.
length
;
// 总行数
int
rowNum
=
contents
.
length
;
// 总行数
int
colNum
=
wordTablePO
.
getColumnWidths
().
length
;
// 总列数
int
colNum
=
wordTablePO
.
getColumnWidths
().
length
;
// 总列数
if
(
colNum
<
1
)
{
if
(
colNum
<
1
)
{
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
"表格列数与内容列数不一致"
);
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
"表格列数与内容列数不一致"
);
}
}
// 字体
// 字体
Font
font
=
WordFontUtils
.
getByType
(
wordTablePO
.
getFont
());
Font
font
=
WordFontUtils
.
getByType
(
wordTablePO
.
getFont
());
// 创建表格
// 创建表格
Table
table
=
new
Table
(
colNum
);
Table
table
=
new
Table
(
colNum
);
table
.
setWidths
(
wordTablePO
.
getColumnWidths
());
// 设置列宽
table
.
setWidths
(
wordTablePO
.
getColumnWidths
());
// 设置列宽
table
.
setLocked
(
true
);
// 锁定列宽
table
.
setLocked
(
true
);
// 锁定列宽
// 设置行和列
// 设置行和列
for
(
int
i
=
0
;
i
<
rowNum
;
i
++)
{
for
(
int
i
=
0
;
i
<
rowNum
;
i
++)
{
String
[]
cols
=
contents
[
i
];
String
[]
cols
=
contents
[
i
];
for
(
int
j
=
0
;
j
<
colNum
;
j
++)
{
for
(
int
j
=
0
;
j
<
colNum
;
j
++)
{
String
text
=
(
j
>=
cols
.
length
||
StringUtil
.
isEmpty
(
cols
[
j
]))
?
""
:
cols
[
j
];
String
text
=
(
j
>=
cols
.
length
||
StringUtil
.
isEmpty
(
cols
[
j
]))
?
""
:
cols
[
j
];
// 填充单元格内容
// 填充单元格内容
Cell
cell
=
new
Cell
(
new
Phrase
(
text
,
font
));
Cell
cell
=
new
Cell
(
new
Phrase
(
text
,
font
));
// 设置上边的边框宽度
// 设置上边的边框宽度
cell
.
setBorderWidthTop
(
0
);
cell
.
setBorderWidthTop
(
0
);
// 设置左边的边框宽度
// 设置左边的边框宽度
cell
.
setBorderWidthLeft
(
0
);
cell
.
setBorderWidthLeft
(
0
);
// 设置右边的边框宽度
// 设置右边的边框宽度
if
(
j
==
(
colNum
-
1
))
{
if
(
j
==
(
colNum
-
1
))
{
cell
.
setBorderWidthRight
(
1
);
cell
.
setBorderWidthRight
(
1
);
}
}
// 设置底部的边框宽度
// 设置底部的边框宽度
if
(
i
==
(
rowNum
-
1
))
{
if
(
i
==
(
rowNum
-
1
))
{
cell
.
setBorderWidthBottom
(
1
);
cell
.
setBorderWidthBottom
(
1
);
}
}
// 设置单元格高度
// 设置单元格高度
// cell.setMinimumHeight(wordTablePO.getMinimumHeight() > 0 ?
// cell.setMinimumHeight(wordTablePO.getMinimumHeight() > 0 ?
// wordTablePO.getMinimumHeight() : Float.NaN);
// wordTablePO.getMinimumHeight() : Float.NaN);
// 设置可以居中
// 设置可以居中
cell
.
setUseAscender
(
true
);
cell
.
setUseAscender
(
true
);
// 设置水平居中
// 设置水平居中
cell
.
setHorizontalAlignment
(
wordTablePO
.
getFontAlign
());
cell
.
setHorizontalAlignment
(
wordTablePO
.
getFontAlign
());
// 设置垂直居中
// 设置垂直居中
cell
.
setVerticalAlignment
(
Cell
.
ALIGN_MIDDLE
);
cell
.
setVerticalAlignment
(
Cell
.
ALIGN_MIDDLE
);
table
.
addCell
(
cell
);
table
.
addCell
(
cell
);
}
}
}
}
// 将表格添加到文档
// 将表格添加到文档
document
.
add
(
table
);
document
.
add
(
table
);
}
}
/**
/**
* 添加图片
* 添加图片
*
*
* @param pdf
PO
* @param word
PO
* @param document
* @param document
* @throws MalformedURLException
* @throws MalformedURLException
* @throws IOException
* @throws IOException
* @throws DocumentException
* @throws DocumentException
*/
*/
private
static
void
addImage
(
WordPO
wordPO
,
Document
document
)
private
static
void
addImage
(
WordPO
wordPO
,
Document
document
)
throws
MalformedURLException
,
IOException
,
DocumentException
{
throws
MalformedURLException
,
IOException
,
DocumentException
{
WordImagePO
wordImagePO
=
(
WordImagePO
)
wordPO
;
WordImagePO
wordImagePO
=
(
WordImagePO
)
wordPO
;
Image
image
=
null
;
Image
image
=
null
;
if
(!
StringUtil
.
isEmpty
(
wordImagePO
.
getImageUrl
()))
{
if
(!
StringUtil
.
isEmpty
(
wordImagePO
.
getImageUrl
()))
{
if
(
wordImagePO
.
getImageUrl
().
startsWith
(
"http"
))
{
if
(
wordImagePO
.
getImageUrl
().
startsWith
(
"http"
))
{
image
=
Image
.
getInstance
(
new
URL
(
wordImagePO
.
getImageUrl
()));
image
=
Image
.
getInstance
(
new
URL
(
wordImagePO
.
getImageUrl
()));
}
else
{
}
else
{
image
=
Image
.
getInstance
(
wordImagePO
.
getImageUrl
());
image
=
Image
.
getInstance
(
wordImagePO
.
getImageUrl
());
}
}
image
.
setAlignment
(
wordImagePO
.
getFontAlign
());
image
.
setAlignment
(
wordImagePO
.
getFontAlign
());
if
(
wordImagePO
.
getHeight
()
>
0
||
wordImagePO
.
getWidth
()
>
0
)
{
if
(
wordImagePO
.
getHeight
()
>
0
||
wordImagePO
.
getWidth
()
>
0
)
{
image
.
scaleAbsolute
(
wordImagePO
.
getHeight
(),
wordImagePO
.
getWidth
());
image
.
scaleAbsolute
(
wordImagePO
.
getHeight
(),
wordImagePO
.
getWidth
());
}
}
}
else
if
(
wordImagePO
.
getImageByte
()
!=
null
)
{
}
else
if
(
wordImagePO
.
getImageByte
()
!=
null
)
{
image
=
Image
.
getInstance
(
wordImagePO
.
getImageByte
());
image
=
Image
.
getInstance
(
wordImagePO
.
getImageByte
());
}
else
{
}
else
{
return
;
return
;
}
}
// 将表格添加到文档
// 将表格添加到文档
document
.
add
(
image
);
document
.
add
(
image
);
}
}
/**
/**
* 结束关闭文件流
* 结束关闭文件流
*
*
* @param document
* @param document
* @param os
* @param os
*/
*/
private
static
void
close
(
Document
document
,
OutputStream
os
,
RtfWriter2
rtfWriter2
)
{
private
static
void
close
(
Document
document
,
OutputStream
os
,
RtfWriter2
rtfWriter2
)
{
try
{
try
{
if
(
document
!=
null
)
{
if
(
document
!=
null
)
{
document
.
close
();
document
.
close
();
document
=
null
;
document
=
null
;
}
}
if
(
os
!=
null
)
{
if
(
os
!=
null
)
{
os
.
close
();
os
.
close
();
os
=
null
;
os
=
null
;
}
}
if
(
rtfWriter2
!=
null
)
{
if
(
rtfWriter2
!=
null
)
{
rtfWriter2
.
close
();
rtfWriter2
.
close
();
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【WORD】WORD文件流关闭失败:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【WORD】WORD文件流关闭失败:"
+
e
.
getMessage
(),
e
);
}
}
}
}
/**
/**
* 结束关闭文件流
* 创建WORD
*
*
* @param document
* @param wordPOs
* @param os
* @return
*/
* @throws Exception
private
static
void
close
(
CustomXWPFDocument
document
,
OutputStream
os
)
{
*/
try
{
public
static
String
create2
(
List
<
WordPO
>
wordPOs
)
throws
FileException
{
if
(
document
!=
null
)
{
LOGGER
.
info
(
"【WORD】创建WORD文档,<START>"
);
document
.
close
();
if
(
ListUtils
.
isEmpty
(
wordPOs
))
{
document
=
null
;
return
null
;
}
}
if
(
os
!=
null
)
{
String
wordTemplate
=
null
;
os
.
close
();
String
outPath
=
null
;
os
=
null
;
CustomXWPFDocument
document
=
null
;
}
FileOutputStream
fos
=
null
;
}
catch
(
Exception
e
)
{
try
{
LOGGER
.
error
(
"【WORD】WORD文件流关闭失败:"
+
e
.
getMessage
(),
e
);
wordTemplate
=
FilePathConst
.
WORD_PATH
+
UUIDUitl
.
taskName
()
+
".docx"
;
}
FileUtils
.
downloadFileFromUrl
(
WordConstant
.
TEMPLATE_URL
,
wordTemplate
);
}
OPCPackage
pack
=
POIXMLDocument
.
openPackage
(
wordTemplate
);
document
=
new
CustomXWPFDocument
(
pack
);
// 添加内容到文档中
addChunks
(
wordPOs
,
document
);
// 生成目标word
outPath
=
FilePathConst
.
WORD_PATH
+
UUIDUitl
.
taskName
()
+
".docx"
;
fos
=
new
FileOutputStream
(
outPath
);
document
.
write
(
fos
);
fos
.
flush
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【WORD】创建WORD文档失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
e
.
getMessage
());
}
finally
{
WordNewUtils
.
close
(
document
,
fos
);
FileUtils
.
deleteFile
(
wordTemplate
);
}
LOGGER
.
info
(
"【WORD】创建WORD文档,<END>.[outPath]="
+
outPath
);
return
outPath
;
}
/**
/**
* 创建WORD
* 添加内容到WORD中
*
*
* @param pdfChunkDOs
* @param wordPOs
* @return
* @param document
* @throws Exception
* @throws BizException
*/
* @throws DocumentException
public
static
String
create2
(
List
<
WordPO
>
wordPOs
)
throws
FileException
{
* @throws MalformedURLException
LOGGER
.
info
(
"【WORD】创建WORD文档,<START>"
);
* @throws IOException
if
(
ListUtils
.
isEmpty
(
wordPOs
))
{
*/
return
null
;
private
static
void
addChunks
(
List
<
WordPO
>
wordPOs
,
CustomXWPFDocument
document
)
}
throws
BizException
,
DocumentException
{
String
wordTemplate
=
null
;
for
(
WordPO
wordPO
:
wordPOs
)
{
String
outPath
=
null
;
if
(
wordPO
instanceof
WordChunkPO
)
{
CustomXWPFDocument
document
=
null
;
addChunk
(
wordPO
,
document
);
FileOutputStream
fos
=
null
;
}
else
if
(
wordPO
instanceof
WordTablePO
)
{
try
{
addTable
(
wordPO
,
document
);
wordTemplate
=
FilePathConst
.
WORD_PATH
+
UUIDUitl
.
taskName
()
+
".docx"
;
}
else
if
(
wordPO
instanceof
WordImagePO
)
{
FileUtils
.
downloadFileFromUrl
(
WordConstant
.
TEMPLATE_URL
,
wordTemplate
);
addImage
(
wordPO
,
document
);
OPCPackage
pack
=
POIXMLDocument
.
openPackage
(
wordTemplate
);
}
document
=
new
CustomXWPFDocument
(
pack
);
}
// 添加内容到文档中
}
addChunks
(
wordPOs
,
document
);
// 生成目标word
outPath
=
FilePathConst
.
WORD_PATH
+
UUIDUitl
.
taskName
()
+
".docx"
;
fos
=
new
FileOutputStream
(
outPath
);
document
.
write
(
fos
);
fos
.
flush
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【WORD】创建WORD文档失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
e
.
getMessage
());
}
finally
{
close
(
document
,
fos
);
FileUtils
.
deleteFile
(
wordTemplate
);
}
LOGGER
.
info
(
"【WORD】创建WORD文档,<END>.[outPath]="
+
outPath
);
return
outPath
;
}
/**
/**
* 添加内容到WORD中
* 添加块
*
*
* @param pdfPOs
* @param wordPO
* @param document
* @throws BizException
* @throws BizException
* @throws DocumentException
* @throws DocumentException
*/
* @throws MalformedURLException
private
static
void
addChunk
(
WordPO
wordPO
,
CustomXWPFDocument
document
)
throws
BizException
{
* @throws IOException
WordChunkPO
wordChunkPO
=
(
WordChunkPO
)
wordPO
;
*/
XWPFParagraph
paragraph
=
document
.
createParagraph
();
private
static
void
addChunks
(
List
<
WordPO
>
wordPOs
,
CustomXWPFDocument
document
)
XWPFRun
xwpfRun
=
paragraph
.
createRun
();
throws
BizException
,
DocumentException
,
MalformedURLException
,
IOException
{
xwpfRun
.
setText
(
wordChunkPO
.
getText
());
for
(
WordPO
wordPO
:
wordPOs
)
{
if
(
wordChunkPO
.
getFontSize
()
>
0
)
{
if
(
wordPO
instanceof
WordChunkPO
)
{
xwpfRun
.
setFontSize
(
wordChunkPO
.
getFontSize
());
addChunk
(
wordPO
,
document
);
}
}
else
if
(
wordPO
instanceof
WordTablePO
)
{
//
addTable
(
wordPO
,
document
);
paragraph
.
setFontAlignment
(
wordChunkPO
.
getFontAlign
());
}
else
if
(
wordPO
instanceof
WordImagePO
)
{
paragraph
.
setSpacingBeforeLines
((
int
)
wordChunkPO
.
getSpacingBefore
());
addImage
(
wordPO
,
document
);
paragraph
.
setSpacingAfterLines
((
int
)
wordChunkPO
.
getSpacingAfter
());
}
paragraph
.
setVerticalAlignment
(
TextAlignment
.
CENTER
);
}
}
}
/**
public
static
void
main
(
String
[]
args
)
throws
IOException
{
* 添加块
List
<
WordPO
>
wordPOs
=
Lists
.
newArrayList
();
*
wordPOs
.
add
(
new
WordChunkPO
(
"标题"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
20
,
20
,
20
));
* @param wordPO
wordPOs
.
add
(
new
WordChunkPO
(
"qweqweasdasd\raweqdasd\nasdasfas\tfasdfca"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
* @throws BizException
13
,
13
,
20
));
* @throws DocumentException
wordPOs
.
add
(
new
WordTablePO
(
new
String
[][]{{
"a"
,
"b"
,
"c"
,
"d"
},
{
"q"
,
"w"
,
"e"
}},
60
,
*/
XWPFVertAlign
.
CENTER
));
private
static
void
addChunk
(
WordPO
wordPO
,
CustomXWPFDocument
document
)
throws
BizException
{
wordPOs
.
add
(
new
WordImagePO
(
"D:\\1.png"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
200
,
200
,
"1."
,
null
));
WordChunkPO
wordChunkPO
=
(
WordChunkPO
)
wordPO
;
wordPOs
.
add
(
new
WordImagePO
(
"D:\\读者圈.jpg"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
200
,
200
,
"2."
,
null
));
XWPFParagraph
paragraph
=
document
.
createParagraph
();
try
{
XWPFRun
xwpfRun
=
paragraph
.
createRun
();
System
.
out
.
println
(
create2
(
wordPOs
));
xwpfRun
.
setText
(
wordChunkPO
.
getText
());
}
catch
(
Exception
e
)
{
if
(
wordChunkPO
.
getFontSize
()
>
0
)
LOGGER
.
error
(
e
.
getMessage
(),
e
);
xwpfRun
.
setFontSize
(
wordChunkPO
.
getFontSize
());
}
//
System
.
out
.
println
(
"Game over!"
);
paragraph
.
setFontAlignment
(
wordChunkPO
.
getFontAlign
());
}
paragraph
.
setSpacingBeforeLines
((
int
)
wordChunkPO
.
getSpacingBefore
());
paragraph
.
setSpacingAfterLines
((
int
)
wordChunkPO
.
getSpacingAfter
());
paragraph
.
setVerticalAlignment
(
TextAlignment
.
CENTER
);
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
/**
List
<
WordPO
>
wordPOs
=
Lists
.
newArrayList
();
* 添加表格
wordPOs
.
add
(
new
WordChunkPO
(
"标题"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
20
,
20
,
20
));
*
wordPOs
.
add
(
new
WordChunkPO
(
"qweqweasdasd\raweqdasd\nasdasfas\tfasdfca"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
* @param wordPO
13
,
13
,
20
));
* @throws BizException
wordPOs
.
add
(
new
WordTablePO
(
new
String
[][]
{
{
"a"
,
"b"
,
"c"
,
"d"
},
{
"q"
,
"w"
,
"e"
}
},
60
,
* @throws DocumentException
XWPFVertAlign
.
CENTER
));
*/
wordPOs
.
add
(
new
WordImagePO
(
"D:\\1.png"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
200
,
200
,
"1."
,
null
));
private
static
void
addTable
(
WordPO
wordPO
,
CustomXWPFDocument
document
)
throws
BizException
,
DocumentException
{
wordPOs
.
add
(
new
WordImagePO
(
"D:\\读者圈.jpg"
,
ParagraphAlignment
.
CENTER
.
getValue
(),
200
,
200
,
"2."
,
null
));
WordTablePO
wordTablePO
=
(
WordTablePO
)
wordPO
;
try
{
String
[][]
contents
=
wordTablePO
.
getContents
();
System
.
out
.
println
(
create2
(
wordPOs
));
if
(
contents
==
null
||
contents
.
length
<
1
)
{
}
catch
(
Exception
e
)
{
return
;
LOGGER
.
error
(
e
.
getMessage
(),
e
);
}
}
int
rowNum
=
contents
.
length
;
// 总行数
System
.
out
.
println
(
"Game over!"
);
int
colNum
=
wordTablePO
.
getColumnWidths
().
length
;
// 总列数
}
if
(
colNum
<
1
)
{
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
"表格列数与内容列数不一致"
);
}
// 创建表格
XWPFTable
ComTable
=
document
.
createTable
();
// 列宽自动分割
CTTblWidth
comTableWidth
=
ComTable
.
getCTTbl
().
addNewTblPr
().
addNewTblW
();
comTableWidth
.
setType
(
STTblWidth
.
DXA
);
comTableWidth
.
setW
(
BigInteger
.
valueOf
(
9072
));
// 设置行和列
for
(
int
i
=
0
;
i
<
rowNum
;
i
++)
{
String
[]
cols
=
contents
[
i
];
// 创建行
XWPFTableRow
comTableRow
=
i
==
0
?
ComTable
.
getRow
(
0
)
:
ComTable
.
createRow
();
for
(
int
j
=
0
;
j
<
colNum
;
j
++)
{
String
text
=
(
j
>=
cols
.
length
||
StringUtil
.
isEmpty
(
cols
[
j
]))
?
""
:
cols
[
j
];
// 填充单元格内容
XWPFTableCell
cell
=
null
;
if
(
i
==
0
)
{
cell
=
j
==
0
?
comTableRow
.
getCell
(
0
)
:
comTableRow
.
addNewTableCell
();
}
else
{
cell
=
comTableRow
.
getCell
(
j
);
}
cell
.
setText
(
text
);
cell
.
setVerticalAlignment
(
wordTablePO
.
getVertAlign
());
}
// 行高
if
(
wordTablePO
.
getHeight
()
>
0
)
{
comTableRow
.
setHeight
(
wordTablePO
.
getHeight
());
}
}
}
/**
/**
* 添加表格
* 添加图片
*
*
* @param pdfPO
* @param wordPO
* @throws BizException
* @param document
* @throws DocumentException
* @throws MalformedURLException
*/
* @throws IOException
private
static
void
addTable
(
WordPO
wordPO
,
CustomXWPFDocument
document
)
throws
BizException
,
DocumentException
{
* @throws DocumentException
WordTablePO
wordTablePO
=
(
WordTablePO
)
wordPO
;
* @throws InvalidFormatException
String
[][]
contents
=
wordTablePO
.
getContents
();
*/
if
(
contents
==
null
||
contents
.
length
<
1
)
{
private
static
void
addImage
(
WordPO
wordPO
,
CustomXWPFDocument
document
)
{
return
;
try
{
}
WordImagePO
wordImagePO
=
(
WordImagePO
)
wordPO
;
int
rowNum
=
contents
.
length
;
// 总行数
XWPFParagraph
paragraph
=
document
.
createParagraph
();
int
colNum
=
wordTablePO
.
getColumnWidths
().
length
;
// 总列数
String
relationId
=
null
;
if
(
colNum
<
1
)
{
if
(!
StringUtil
.
isEmpty
(
wordImagePO
.
getImageUrl
()))
{
throw
new
FileException
(
FileException
.
FILE_CONTENT_ERROR
,
"表格列数与内容列数不一致"
);
if
(
wordImagePO
.
getImageUrl
().
startsWith
(
"http"
))
{
}
relationId
=
document
.
addPictureData
(
FileUtils
.
downloadByteFromUrl
(
wordImagePO
.
getImageUrl
()),
// 创建表格
XWPFDocument
.
PICTURE_TYPE_JPEG
);
XWPFTable
ComTable
=
document
.
createTable
();
}
else
{
// 列宽自动分割
relationId
=
document
.
addPictureData
(
FileUtils
.
file2byte
(
wordImagePO
.
getImageUrl
()),
CTTblWidth
comTableWidth
=
ComTable
.
getCTTbl
().
addNewTblPr
().
addNewTblW
();
XWPFDocument
.
PICTURE_TYPE_JPEG
);
comTableWidth
.
setType
(
STTblWidth
.
DXA
);
}
comTableWidth
.
setW
(
BigInteger
.
valueOf
(
9072
));
}
else
if
(
wordImagePO
.
getImageByte
()
!=
null
)
{
// 设置行和列
relationId
=
document
.
addPictureData
(
wordImagePO
.
getImageByte
(),
XWPFDocument
.
PICTURE_TYPE_JPEG
);
for
(
int
i
=
0
;
i
<
rowNum
;
i
++)
{
}
else
{
String
[]
cols
=
contents
[
i
];
return
;
// 创建行
}
XWPFTableRow
comTableRow
=
i
==
0
?
ComTable
.
getRow
(
0
)
:
ComTable
.
createRow
();
if
(!
StringUtil
.
isEmpty
(
relationId
))
{
for
(
int
j
=
0
;
j
<
colNum
;
j
++)
{
// 前置文本
String
text
=
(
j
>=
cols
.
length
||
StringUtil
.
isEmpty
(
cols
[
j
]))
?
""
:
cols
[
j
];
String
beforeText
=
wordImagePO
.
getBeforeText
();
// 填充单元格内容
if
(!
StringUtil
.
isEmpty
(
beforeText
))
{
XWPFTableCell
cell
=
null
;
XWPFRun
xwpfRun
=
paragraph
.
createRun
();
if
(
i
==
0
)
{
xwpfRun
.
setText
(
beforeText
);
cell
=
j
==
0
?
comTableRow
.
getCell
(
0
)
:
comTableRow
.
addNewTableCell
();
if
(
wordImagePO
.
getFontSize
()
>
0
)
{
}
else
{
xwpfRun
.
setFontSize
(
wordImagePO
.
getFontSize
());
cell
=
comTableRow
.
getCell
(
j
);
}
}
}
cell
.
setText
(
text
);
// 图片
cell
.
setVerticalAlignment
(
wordTablePO
.
getVertAlign
());
int
id
=
document
.
getAllPictures
().
size
()
-
1
;
}
document
.
createPicture
(
paragraph
,
relationId
,
id
,
wordImagePO
.
getWidth
(),
wordImagePO
.
getHeight
());
// 行高
// 后置文本
if
(
wordTablePO
.
getHeight
()
>
0
)
String
afterText
=
wordImagePO
.
getAfterText
();
comTableRow
.
setHeight
(
wordTablePO
.
getHeight
());
if
(!
StringUtil
.
isEmpty
(
afterText
))
{
}
XWPFRun
xwpfRun
=
paragraph
.
createRun
();
}
xwpfRun
.
setText
(
afterText
);
if
(
wordImagePO
.
getFontSize
()
>
0
)
{
/**
xwpfRun
.
setFontSize
(
wordImagePO
.
getFontSize
());
* 添加图片
}
*
}
* @param pdfPO
paragraph
.
setFontAlignment
(
wordPO
.
getFontAlign
());
* @param document
}
* @throws MalformedURLException
}
catch
(
Exception
e
)
{
* @throws IOException
LOGGER
.
error
(
"【WORD】创建WORD文档失败:"
+
e
.
getMessage
(),
e
);
* @throws DocumentException
}
* @throws InvalidFormatException
}
*/
private
static
void
addImage
(
WordPO
wordPO
,
CustomXWPFDocument
document
)
{
try
{
WordImagePO
wordImagePO
=
(
WordImagePO
)
wordPO
;
XWPFParagraph
paragraph
=
document
.
createParagraph
();
String
relationId
=
null
;
if
(!
StringUtil
.
isEmpty
(
wordImagePO
.
getImageUrl
()))
{
if
(
wordImagePO
.
getImageUrl
().
startsWith
(
"http"
))
{
relationId
=
document
.
addPictureData
(
FileUtils
.
downloadByteFromUrl
(
wordImagePO
.
getImageUrl
()),
XWPFDocument
.
PICTURE_TYPE_JPEG
);
}
else
{
relationId
=
document
.
addPictureData
(
FileUtils
.
file2byte
(
wordImagePO
.
getImageUrl
()),
XWPFDocument
.
PICTURE_TYPE_JPEG
);
}
}
else
if
(
wordImagePO
.
getImageByte
()
!=
null
)
{
relationId
=
document
.
addPictureData
(
wordImagePO
.
getImageByte
(),
XWPFDocument
.
PICTURE_TYPE_JPEG
);
}
else
{
return
;
}
if
(!
StringUtil
.
isEmpty
(
relationId
))
{
// 前置文本
String
beforeText
=
wordImagePO
.
getBeforeText
();
if
(!
StringUtil
.
isEmpty
(
beforeText
))
{
XWPFRun
xwpfRun
=
paragraph
.
createRun
();
xwpfRun
.
setText
(
beforeText
);
if
(
wordImagePO
.
getFontSize
()
>
0
)
xwpfRun
.
setFontSize
(
wordImagePO
.
getFontSize
());
}
// 图片
int
id
=
document
.
getAllPictures
().
size
()
-
1
;
document
.
createPicture
(
paragraph
,
relationId
,
id
,
wordImagePO
.
getWidth
(),
wordImagePO
.
getHeight
());
// 后置文本
String
afterText
=
wordImagePO
.
getAfterText
();
if
(!
StringUtil
.
isEmpty
(
afterText
))
{
XWPFRun
xwpfRun
=
paragraph
.
createRun
();
xwpfRun
.
setText
(
afterText
);
if
(
wordImagePO
.
getFontSize
()
>
0
)
xwpfRun
.
setFontSize
(
wordImagePO
.
getFontSize
());
}
paragraph
.
setFontAlignment
(
wordPO
.
getFontAlign
());
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【WORD】创建WORD文档失败:"
+
e
.
getMessage
(),
e
);
}
}
/**
* 根据图片类型获取对应的图片类型代码
*
* @param picType
* @return
*/
public
static
int
getPictureType
(
String
picType
)
{
int
res
=
CustomXWPFDocument
.
PICTURE_TYPE_PICT
;
if
(
picType
!=
null
)
{
if
(
picType
.
equalsIgnoreCase
(
"png"
))
{
res
=
CustomXWPFDocument
.
PICTURE_TYPE_PNG
;
}
else
if
(
picType
.
equalsIgnoreCase
(
"dib"
))
{
res
=
CustomXWPFDocument
.
PICTURE_TYPE_DIB
;
}
else
if
(
picType
.
equalsIgnoreCase
(
"emf"
))
{
res
=
CustomXWPFDocument
.
PICTURE_TYPE_EMF
;
}
else
if
(
picType
.
equalsIgnoreCase
(
"jpg"
)
||
picType
.
equalsIgnoreCase
(
"jpeg"
))
{
res
=
CustomXWPFDocument
.
PICTURE_TYPE_JPEG
;
}
else
if
(
picType
.
equalsIgnoreCase
(
"wmf"
))
{
res
=
CustomXWPFDocument
.
PICTURE_TYPE_WMF
;
}
}
return
res
;
}
}
}
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