Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-mvp
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
fangxinjiang
yifu-mvp
Commits
81ea44b7
Commit
81ea44b7
authored
Dec 16, 2024
by
fangxinjiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
代码优化-fxj
parent
7d4448dc
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
9 deletions
+21
-9
ExcelToImage.java
...com/yifu/cloud/plus/v1/yifu/social/util/ExcelToImage.java
+21
-9
No files found.
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/util/ExcelToImage.java
View file @
81ea44b7
...
@@ -4,6 +4,7 @@ package com.yifu.cloud.plus.v1.yifu.social.util;
...
@@ -4,6 +4,7 @@ package com.yifu.cloud.plus.v1.yifu.social.util;
import
com.spire.xls.Workbook
;
import
com.spire.xls.Workbook
;
import
com.spire.xls.Worksheet
;
import
com.spire.xls.Worksheet
;
import
com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants
;
import
com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.Common
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig
;
import
com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig
;
...
@@ -27,10 +28,18 @@ import java.util.Base64;
...
@@ -27,10 +28,18 @@ import java.util.Base64;
@Component
@Component
public
class
ExcelToImage
{
public
class
ExcelToImage
{
private
final
static
String
toImag
=
"\\ToImg.png"
;
private
final
static
String
toImagParamError
=
":带印章excel生成图片传参异常"
;
private
final
static
String
toImagConvertError
=
":ExcelToImage转换异常-"
;
private
final
static
String
toImagOSSError
=
":ExcelToImage上传OSS异常"
;
public
R
<
String
>
excelToImg
(
InputStream
inputStream
,
SocialFriendConfig
socialFriendConfig
public
R
<
String
>
excelToImg
(
InputStream
inputStream
,
SocialFriendConfig
socialFriendConfig
,
RestTemplate
restTemplate
,
TSocialFriendBackLog
backLog
){
,
RestTemplate
restTemplate
,
TSocialFriendBackLog
backLog
){
if
(
null
==
inputStream
||
null
==
socialFriendConfig
){
if
(
null
==
inputStream
||
null
==
socialFriendConfig
){
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
":带印章excel生成图片传参异常"
);
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagParamError
);
}
}
String
projectRoot
=
System
.
getProperty
(
CommonConstants
.
USER_DIR
);
String
projectRoot
=
System
.
getProperty
(
CommonConstants
.
USER_DIR
);
//加载Excel工作表
//加载Excel工作表
...
@@ -40,20 +49,23 @@ public class ExcelToImage {
...
@@ -40,20 +49,23 @@ public class ExcelToImage {
Worksheet
sheet
=
wb
.
getWorksheets
().
get
(
0
);
Worksheet
sheet
=
wb
.
getWorksheets
().
get
(
0
);
//调用方法将Excel工作表保存为图片
//调用方法将Excel工作表保存为图片
sheet
.
saveToImage
(
projectRoot
+
"\\ToImg.png"
);
sheet
.
saveToImage
(
projectRoot
+
toImag
);
File
file
=
null
;
File
file
=
null
;
try
{
try
{
// 上传税友文件服务器,返回文件路径
// 上传税友文件服务器,返回文件路径
file
=
new
File
(
projectRoot
+
"\\ToImg.png"
);
file
=
new
File
(
projectRoot
+
toImag
);
if
(
file
.
exists
()){
if
(
file
.
exists
()){
String
key
=
System
.
currentTimeMillis
()
+
file
.
getName
();
String
key
=
System
.
currentTimeMillis
()
+
file
.
getName
();
byte
[]
fileContent
=
Files
.
readAllBytes
(
file
.
toPath
());
byte
[]
fileContent
=
Files
.
readAllBytes
(
file
.
toPath
());
String
base64String
=
Base64
.
getEncoder
().
encodeToString
(
fileContent
);
String
base64String
=
Base64
.
getEncoder
().
encodeToString
(
fileContent
);
String
url
=
socialFriendConfig
.
uploadPng
(
restTemplate
,
key
,
base64String
,
backLog
);
String
url
=
socialFriendConfig
.
uploadPng
(
restTemplate
,
key
,
base64String
,
backLog
);
if
(
Common
.
isEmpty
(
url
)){
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagOSSError
);
}
return
R
.
ok
(
url
);
return
R
.
ok
(
url
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
":ExcelToImage转换异常-"
+
e
.
getMessage
());
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagConvertError
+
e
.
getMessage
());
}
finally
{
}
finally
{
if
(
file
.
exists
()){
if
(
file
.
exists
()){
file
.
delete
();
file
.
delete
();
...
@@ -70,7 +82,7 @@ public class ExcelToImage {
...
@@ -70,7 +82,7 @@ public class ExcelToImage {
}
}
public
R
<
URL
>
excelToImg
(
InputStream
inputStream
,
OSSUtil
ossUtil
){
public
R
<
URL
>
excelToImg
(
InputStream
inputStream
,
OSSUtil
ossUtil
){
if
(
null
==
inputStream
||
null
==
ossUtil
){
if
(
null
==
inputStream
||
null
==
ossUtil
){
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
":带印章excel生成图片传参异常"
);
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagParamError
);
}
}
String
projectRoot
=
System
.
getProperty
(
CommonConstants
.
USER_DIR
);
String
projectRoot
=
System
.
getProperty
(
CommonConstants
.
USER_DIR
);
//加载Excel工作表
//加载Excel工作表
...
@@ -79,20 +91,20 @@ public class ExcelToImage {
...
@@ -79,20 +91,20 @@ public class ExcelToImage {
//获取工作表
//获取工作表
Worksheet
sheet
=
wb
.
getWorksheets
().
get
(
0
);
Worksheet
sheet
=
wb
.
getWorksheets
().
get
(
0
);
//调用方法将Excel工作表保存为图片
//调用方法将Excel工作表保存为图片
sheet
.
saveToImage
(
projectRoot
+
"\\ToImg.png"
);
sheet
.
saveToImage
(
projectRoot
+
toImag
);
File
file
=
null
;
File
file
=
null
;
try
{
try
{
file
=
new
File
(
projectRoot
+
"\\ToImg.png"
);
file
=
new
File
(
projectRoot
+
toImag
);
if
(
file
.
exists
()){
if
(
file
.
exists
()){
String
key
=
System
.
currentTimeMillis
()
+
file
.
getName
();
String
key
=
System
.
currentTimeMillis
()
+
file
.
getName
();
if
(
ossUtil
.
uploadFileByFile
(
file
,
key
,
null
)){
if
(
ossUtil
.
uploadFileByFile
(
file
,
key
,
null
)){
return
R
.
ok
(
ossUtil
.
getObjectUrl
(
null
,
key
));
return
R
.
ok
(
ossUtil
.
getObjectUrl
(
null
,
key
));
}
else
{
}
else
{
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
":ExcelToImage上传OSS异常"
);
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagOSSError
);
}
}
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
":ExcelToImage转换异常-"
+
e
.
getMessage
());
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagConvertError
+
e
.
getMessage
());
}
finally
{
}
finally
{
if
(
file
.
exists
()){
if
(
file
.
exists
()){
file
.
delete
();
file
.
delete
();
...
...
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