Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
8
8timerapiv200
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
8timerv2
8timerapiv200
Commits
2304e69a
Commit
2304e69a
authored
Mar 10, 2022
by
翁国栋
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
投保管理
parent
460d1280
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
838 additions
and
0 deletions
+838
-0
src/main/java/cn/timer/api/controller/insure/InsureContorll.java
+327
-0
src/main/java/cn/timer/api/utils/ExcelUtils.java
+511
-0
No files found.
src/main/java/cn/timer/api/controller/insure/InsureContorll.java
0 → 100644
View file @
2304e69a
package
cn
.
timer
.
api
.
controller
.
insure
;
//import cn.timer.api.bean.insure.InsuredUser;
import
cn.timer.api.bean.qyzx.QyzxEntInfoM
;
import
cn.timer.api.bean.yggl.YgglMainEmp
;
import
cn.timer.api.config.annotation.CurrentUser
;
import
cn.timer.api.config.annotation.UserBean
;
import
cn.timer.api.controller.insure.bean.ExcelBean
;
import
cn.timer.api.utils.*
;
import
com.alibaba.druid.util.Base64
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.beust.jcommander.internal.Lists
;
import
com.github.xiaoymin.knife4j.annotations.ApiOperationSupport
;
import
com.google.common.collect.Maps
;
import
com.google.gson.JsonObject
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.ibatis.annotations.Param
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.*
;
import
java.net.URLEncoder
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.*
;
import
com.alibaba.fastjson.JSONObject
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* @Description TODO
* @Author wgd
* @Date 2022/3/1 11:34
*/
@Api
(
tags
=
"8.0保险列表"
)
@RestController
@Transactional
@RequestMapping
(
value
=
"/insure"
,
produces
=
{
"application/json"
})
public
class
InsureContorll
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
InsureContorll
.
class
);
/*测试用*/
private
static
String
appid
=
"1002303100602312445"
;
/*测试用*/
private
static
String
secret
=
"acb329868c31d5b3ba03de40dac13dd9"
;
/*保全测试用*/
private
static
String
appidq
=
"1000115041006006938"
;
/*保全测试用*/
private
static
String
secretq
=
"6ba1bf4aa0bd14368c95ff0b9934a17b"
;
/*测试用投保上传文件地址*/
private
static
String
uploadUrl
=
" http://sandbox.portal.unistar-ins.com/service/Home/Index/fileUpload"
;
/*测试用投保申请地址*/
private
static
String
insuredUrl
=
"http://sandbox.portal.unistar-ins.com/mall/Home/Index/createQuotePolicy"
;
/*测试用保全上传文件地址*/
private
static
String
uploadUrl2
=
"http://sandbox.portal.unistar-ins.com/fuli/Home/Index/file_upload"
;
/**
* 设置请求参数
* @param sign
* @return
*/
private
Map
setParams
(
String
sign
){
/*当前时间戳*/
long
timestamp
=
System
.
currentTimeMillis
()/
1000
;
log
.
info
(
"时间戳"
+
timestamp
);
Map
paramsMap
=
Maps
.
newHashMap
();
paramsMap
.
put
(
"pid"
,
appid
);
paramsMap
.
put
(
"timestamp"
,
String
.
valueOf
(
timestamp
));
paramsMap
.
put
(
"trace_id"
,
appid
+
timestamp
+
new
Random
().
nextInt
((
9999
-
100
)
+
1
)
+
10
);
// paramsMap.put("sign",Md5.md5(appid+secret+timestamp+sign.trim()));
String
value
=
appid
+
secret
+
timestamp
+
sign
;
log
.
info
(
"body参数======"
+
sign
);
paramsMap
.
put
(
"sign"
,
DigestUtils
.
md5Hex
(
value
));
log
.
info
(
"params参数======"
+
JSONObject
.
toJSONString
(
paramsMap
));
return
paramsMap
;
}
/*保全*/
private
Map
setParams2
(
String
sign
){
/*当前时间戳*/
long
timestamp
=
System
.
currentTimeMillis
()/
1000
;
log
.
info
(
"时间戳"
+
timestamp
);
Map
paramsMap
=
Maps
.
newHashMap
();
paramsMap
.
put
(
"pid"
,
appidq
);
paramsMap
.
put
(
"timestamp"
,
String
.
valueOf
(
timestamp
/
1000
));
paramsMap
.
put
(
"trace_id"
,
appidq
+
timestamp
+
new
Random
().
nextInt
((
9999
-
100
)
+
1
)
+
10
);
// paramsMap.put("sign",Md5.md5(appidq+secret+timestamp+sign.trim()));
String
value
=
appidq
+
secretq
+
timestamp
+
sign
;
log
.
info
(
"body参数======"
+
sign
);
paramsMap
.
put
(
"sign"
,
DigestUtils
.
md5Hex
(
value
));
log
.
info
(
"params参数======"
+
JSONObject
.
toJSONString
(
paramsMap
));
return
paramsMap
;
}
@PostMapping
(
value
=
"/insured_add"
)
@ApiOperation
(
value
=
"1.投保申请"
,
httpMethod
=
"Post"
,
notes
=
"投保申请"
)
@ApiOperationSupport
(
order
=
2
)
public
Result
<
Object
>
insured_add
(
@CurrentUser
UserBean
userBean
,
@RequestParam
(
required
=
true
)
String
url
)
{
QyzxEntInfoM
qyzxEntInfoM
=
QyzxEntInfoM
.
builder
().
id
(
userBean
.
getOrgCode
()).
build
().
selectById
();
// 企业信息
DateTimeFormatter
dtf2
=
DateTimeFormatter
.
ofPattern
(
"yyyyMMddHHmmss"
);
LocalDateTime
now
=
LocalDateTime
.
now
();
Map
bodyMap
=
Maps
.
newHashMap
();
ArrayList
<
Map
>
quotationsArry
=
new
ArrayList
<
Map
>();
bodyMap
.
put
(
"is_ss"
,
0
);
Map
quotations
=
Maps
.
newHashMap
();
quotations
.
put
(
"trans_id"
,
"MB_"
+
dtf2
.
format
(
now
));
/*报价请求流水号*/
quotations
.
put
(
"applicant_type"
,
"2"
);
/*投保人类型:2-企业*/
quotations
.
put
(
"quotation_type"
,
3
);
/*报价类型:3-投保申请*/
quotations
.
put
(
"currency"
,
"5"
);
/*币种:5人民币*/
quotations
.
put
(
"source_type"
,
""
);
/*币种:5人民币*/
quotations
.
put
(
"cps_email"
,
""
);
/*币种:5人民币*/
quotations
.
put
(
"cps_tel"
,
""
);
/*币种:5人民币*/
quotations
.
put
(
"last_serial_number"
,
""
);
/*币种:5人民币*/
quotations
.
put
(
"from_source"
,
""
);
/*币种:5人民币*/
quotations
.
put
(
"operator_name"
,
""
);
/*币种:5人民币*/
Map
basicMap
=
Maps
.
newHashMap
();
basicMap
.
put
(
"partner_pid"
,
"1231"
);
/*分销商pid*/
basicMap
.
put
(
"plan_code_id"
,
"61b85207b73d2ab09fb2959"
);
/*计划代码,选项值:61b85207b73d2ab09fb2959 (生产环境) 61b85207b73d2ab09fb2959 (沙箱环境)*/
basicMap
.
put
(
"product_code_id"
,
"61b85207a9f87da19fb5986"
);
/*产品代码,选项值:61b85207a9f87da19fb5986 (生产环境) 61b85207a9f87da19fb5986 (沙箱环境)*/
basicMap
.
put
(
"policy_date_start"
,
"2022-04-01 00:00:00"
);
basicMap
.
put
(
"policy_date_end"
,
"2022-4-30 23:59:59"
);
basicMap
.
put
(
"channel"
,
""
);
basicMap
.
put
(
"fee"
,
""
);
basicMap
.
put
(
"partner_user_rate"
,
""
);
basicMap
.
put
(
"promotion_fee"
,
""
);
basicMap
.
put
(
"agent_id"
,
""
);
basicMap
.
put
(
"policy_insure_notify"
,
""
);
basicMap
.
put
(
"promotion_id"
,
0
);
basicMap
.
put
(
"policy_number_last_year"
,
""
);
quotations
.
put
(
"basic"
,
basicMap
);
quotations
.
put
(
"individual_applicant"
,
Maps
.
newHashMap
());
/*企业信息*/
Map
companyApplicantMap
=
Maps
.
newHashMap
();
companyApplicantMap
.
put
(
"applicant_e_name"
,
"测试企业一"
);
/*企业/网点名称*/
companyApplicantMap
.
put
(
"applicant_e_address"
,
"测试地址"
);
/*企业/网点地址*/
companyApplicantMap
.
put
(
"applicant_e_contacts"
,
"测试人"
);
/*企业授权操作人姓名*/
companyApplicantMap
.
put
(
"applicant_e_phone"
,
"13800138000"
);
/*操作人的联系电话*/
companyApplicantMap
.
put
(
"applicant_e_no_type"
,
"3646"
);
/*企业证件类型*/
companyApplicantMap
.
put
(
"applicant_e_no"
,
"91310230MA1JXL5059"
);
/*企业证件号码*/
companyApplicantMap
.
put
(
"applicant_e_mail"
,
"123@123.com"
);
/*企业邮箱*/
companyApplicantMap
.
put
(
"applicant_province_city"
,
"21721"
);
/*省市区*/
/*人员清单填被保人员*/
companyApplicantMap
.
put
(
"applicant_employee_list"
,
url
);
quotations
.
put
(
"company_applicant"
,
companyApplicantMap
);
/*被保人员 填被保企业信息*/
ArrayList
<
Map
>
insuredArray
=
new
ArrayList
<>();
Map
insured
=
Maps
.
newHashMap
();
insured
.
put
(
"trans_id"
,
"P_"
+
qyzxEntInfoM
.
getId
()+
"_"
+
dtf2
.
format
(
now
));
insured
.
put
(
"price"
,
"75"
);
insured
.
put
(
"insured_applicant_relations"
,
"36960"
);
insured
.
put
(
"insured_mobile"
,
qyzxEntInfoM
.
getLinkManPhone
());
insured
.
put
(
"insured_e_contact"
,
qyzxEntInfoM
.
getLinkMan
());
insured
.
put
(
"insured_name"
,
qyzxEntInfoM
.
getName
());
insured
.
put
(
"insured_no_type"
,
"3306"
);
insured
.
put
(
"insured_no"
,
qyzxEntInfoM
.
getCreditCode
());
insured
.
put
(
"insured_zm_openid"
,
""
);
insured
.
put
(
"benefit_basic_plan"
,
"36969"
);
insured
.
put
(
"benefit_occupation_category"
,
"63119"
);
insured
.
put
(
"benefit_EL_employee_number_t"
,
"5"
);
insuredArray
.
add
(
insured
);
quotations
.
put
(
"insured"
,
insuredArray
);
/*quotations.put("bd_id","");不需要*/
quotations
.
put
(
"file"
,
Lists
.
newArrayList
());
quotations
.
put
(
"specifically_agreed_file"
,
Lists
.
newArrayList
());
quotations
.
put
(
"policy_no"
,
""
);
quotations
.
put
(
"callback_policy"
,
"/callBack/insuredCallBack"
);
quotations
.
put
(
"insured"
,
insuredArray
);
quotationsArry
.
add
(
quotations
);
bodyMap
.
put
(
"quotations"
,
quotationsArry
);
log
.
info
(
JSONObject
.
toJSONString
(
bodyMap
));
Object
o
=
requestAgent
(
bodyMap
);
return
ResultUtil
.
data
(
o
);
}
/*测试用生产环境不需要*/
synchronized
private
Object
requestAgent
(
Map
bodyMap
){
String
data
=
HttpUtils
.
sendPost
(
insuredUrl
,
setParams
(
JSONObject
.
toJSONString
(
bodyMap
)),
bodyMap
);
Map
<
String
,
Object
>
dataMap
=
JSONObject
.
parseObject
(
data
);
if
(
dataMap
.
get
(
"errcode"
).
toString
().
equals
(
"suc"
))
{
return
dataMap
.
get
(
"data"
);
}
else
{
if
(
dataMap
.
get
(
"errcode"
).
toString
().
equals
(
"e1"
)&&
dataMap
.
get
(
"errmsg"
).
toString
().
equals
(
"出单中"
)){
try
{
Thread
.
sleep
(
30000
);
requestAgent
(
bodyMap
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
return
dataMap
.
get
(
"data"
);
}
}
@PostMapping
(
value
=
"/file_upload"
)
@ApiOperation
(
value
=
"3.投保上传文件"
,
httpMethod
=
"Post"
,
notes
=
"上传文件"
)
@ApiOperationSupport
(
order
=
2
)
public
Result
<
Object
>
fileUpload
(
@CurrentUser
UserBean
userBean
,
@RequestParam
(
required
=
true
)
MultipartFile
file
)
{
if
(
file
==
null
)
{
return
ResultUtil
.
error
(
"上传失败,请选择上传文件"
);
}
byte
[]
bytes
=
null
;
try
{
bytes
=
file
.
getBytes
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
log
.
warn
(
"文件转换Base64异常"
);
}
String
fileBase64
=
Base64
.
byteArrayToBase64
(
bytes
);
/*body参数 begin*/
Map
bodyMap
=
Maps
.
newHashMap
();
bodyMap
.
put
(
"app"
,
"mall"
);
bodyMap
.
put
(
"name"
,
file
.
getName
());
bodyMap
.
put
(
"file"
,
fileBase64
);
/*end*/
String
data
=
HttpUtils
.
sendPost
(
uploadUrl
,
setParams
(
JSONObject
.
toJSONString
(
bodyMap
)),
bodyMap
);
log
.
info
(
"请求返回的结果====="
+
data
);
/*文件流base64*/
return
ResultUtil
.
data
(
message
(
data
));
};
@PostMapping
(
value
=
"/file_upload2"
)
@ApiOperation
(
value
=
"3.保全上传文件"
,
httpMethod
=
"Post"
,
notes
=
"上传文件"
)
@ApiOperationSupport
(
order
=
2
)
public
Result
<
Object
>
fileUpload2
(
@CurrentUser
UserBean
userBean
,
@RequestParam
(
required
=
true
)
MultipartFile
file
)
{
if
(
file
==
null
)
{
return
ResultUtil
.
error
(
"上传失败,请选择上传文件"
);
}
byte
[]
bytes
=
null
;
try
{
bytes
=
file
.
getBytes
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
log
.
warn
(
"文件转换Base64异常"
);
}
String
fileBase64
=
Base64
.
byteArrayToBase64
(
bytes
);
/*body参数 begin*/
Map
bodyMap
=
Maps
.
newHashMap
();
// bodyMap.put("app","mall");
// bodyMap.put("name","劳务公司批增-月单(1).xlsx");
bodyMap
.
put
(
"file"
,
fileBase64
);
/*end*/
String
data
=
HttpUtils
.
sendPost
(
uploadUrl2
,
setParams2
(
JSONObject
.
toJSONString
(
bodyMap
)),
bodyMap
);
log
.
info
(
"请求返回的结果====="
+
data
);
/*文件流base64*/
return
ResultUtil
.
data
(
message
(
data
));
};
// @PostMapping(value = "/baoquan")
// @ApiOperation(value = "2.单个添加保全", httpMethod = "Post", notes = "上传文件")
// @ApiOperationSupport(order = 2)
// public Result<Object> baoquan_add(InsuredUser user) {
//return null;
// }
/**
* 设置错误信息
* @param data
* @return
*/
private
Object
message
(
String
data
){
Map
<
String
,
Object
>
dataMap
=
JSONObject
.
parseObject
(
data
);
if
(
dataMap
.
get
(
"errcode"
).
toString
().
equals
(
"suc"
))
{
return
dataMap
.
get
(
"data"
);
}
return
dataMap
.
get
(
"errmsg"
);
}
@GetMapping
(
value
=
"/downExcel"
)
@ApiOperation
(
value
=
"2.下载投保文件"
,
httpMethod
=
"Get"
,
notes
=
"下载投保文件"
)
@ApiOperationSupport
(
order
=
2
)
public
void
downExcel
(
@CurrentUser
UserBean
userBean
,
HttpServletRequest
request
,
HttpServletResponse
resp
){
LocalDate
localDate
=
LocalDate
.
now
();
/*获取该企业下所有未投保员工*/
List
<
YgglMainEmp
>
ygglMainEmpList
=
YgglMainEmp
.
builder
().
build
().
selectList
(
new
QueryWrapper
<
YgglMainEmp
>().
lambda
().
eq
(
YgglMainEmp:
:
getOrgCode
,
userBean
.
getOrgCode
())
.
eq
(
YgglMainEmp:
:
getIsInsure
,
0
));
ExcelBean
excelBean
;
List
list
=
Lists
.
newArrayList
();
for
(
YgglMainEmp
y:
ygglMainEmpList
.
subList
(
0
,
5
))
{
String
[]
arr
=
new
String
[]{
String
.
valueOf
(
y
.
getId
()),
y
.
getName
(),
"身份证"
,
y
.
getZjNum
(),
"50万意外/5万医疗/扩展24小时(A类)"
,
""
,
"深圳名邦人力资源管理有限公司"
,
"无"
,
"A类"
};
list
.
add
(
arr
);
}
String
[]
rowName
=
new
String
[]{
"ID"
,
"name"
,
"ID_type"
,
"ID_number"
,
"Scheme_name"
,
"Date_start"
,
"Branch"
,
"Tricycle_frame_number"
,
"benefit_occupation_category"
};
/*随机抽取五名幸运用户投保*/
OutputStream
os
=
null
;
try
{
resp
.
setContentType
(
"application/octet-stream"
);
resp
.
setHeader
(
"content-disposition"
,
"attachment; filename="
+
URLEncoder
.
encode
(
localDate
+
"_"
+
list
.
size
()+
"名人员清单.xlsx"
,
"UTF-8"
));
resp
.
setCharacterEncoding
(
"UTF-8"
);
os
=
resp
.
getOutputStream
();
ExcelUtils
.
exportExcel
(
localDate
+
"_"
+
list
.
size
()+
"名人员清单.xlsx"
,
rowName
,
list
,
os
);
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
os
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
src/main/java/cn/timer/api/utils/ExcelUtils.java
0 → 100644
View file @
2304e69a
package
cn
.
timer
.
api
.
utils
;
import
cn.timer.api.controller.insure.InsureContorll
;
import
org.apache.poi.hssf.usermodel.*
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.ss.util.CellRangeAddress
;
import
org.apache.poi.xssf.usermodel.*
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* Excel工具类
*/
public
class
ExcelUtils
{
// 导出时excel的扩展名
public
static
final
String
EXTENSION_NAME
=
".xlsx"
;
// 03版excel扩展名
private
static
final
String
XLS
=
".xls"
;
// 07版excel扩展名
private
static
final
String
XLSX
=
".xlsx"
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ExcelUtils
.
class
);
/**
* 导入Excel到数据库
*
* @param filePath 导入的excel文件所在的绝对路径
* @param startRow 开始解析的行数
* @param startCol 开始解析的列数
* @param sheetNum 开始解析的sheet序号,如果不指定,默认传值为-1,则会解析所有sheet
* @return
*/
public
static
List
<
List
<
Map
<
String
,
Object
>>>
importExcel
(
String
filePath
,
int
startRow
,
int
startCol
,
int
sheetNum
)
{
logger
.
info
(
"========================= ExcelUtils.java ->> importExcel()从Excel表格中获取数据 ->> 开始 ========================="
);
// 用于存储最终整个Excel表格的数据
List
<
List
<
Map
<
String
,
Object
>>>
resultList
=
new
ArrayList
<>();
// 得到指定路径的文件File对象
File
file
=
new
File
(
filePath
);
// 如果不存在
if
(!
file
.
exists
())
{
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 错误操作:要读取Excel文件在指定路径("
+
filePath
+
")下找不到"
);
throw
new
RuntimeException
(
"错误操作:要读取Excel文件在指定路径("
+
filePath
+
")下找不到"
);
}
InputStream
input
=
null
;
Workbook
workbook
=
null
;
try
{
// 得到文件的资源输入流
input
=
new
FileInputStream
(
file
);
// 得到处理excel的Workbook对象
workbook
=
ExcelUtils
.
getWorkbookByExtensionName
(
input
,
filePath
);
// 创建一个公式计算器,用于计算并得到Excel中的公式结果
FormulaEvaluator
formulaEvaluator
=
workbook
.
getCreationHelper
().
createFormulaEvaluator
();
// 得到Excel表格中sheet的数量
int
totalSheetNum
=
workbook
.
getNumberOfSheets
();
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 用户指定解析的Sheet表格序号sheetNum = "
+
sheetNum
);
Sheet
sheet
=
null
;
if
(
sheetNum
==
-
1
)
{
// 循环遍历sheet
for
(
int
m
=
0
;
m
<
totalSheetNum
;
m
++)
{
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 开始解析第"
+
(
m
+
1
)
+
"个Sheet表格"
);
// 获取每一个sheet
sheet
=
workbook
.
getSheetAt
(
m
);
// 保存Sheet中的数据到List集合中
List
<
Map
<
String
,
Object
>>
sheetList
=
ExcelUtils
.
getDataBySheet
(
sheet
,
startRow
,
startCol
,
formulaEvaluator
);
// 保存存储有每个sheet数据的List到结果集List中去
resultList
.
add
(
sheetList
);
}
}
else
if
(
sheetNum
>
0
&&
(
sheetNum
-
1
)
<
totalSheetNum
)
{
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 开始解析第"
+
sheetNum
+
"个Sheet表格"
);
// 获取指定sheet序号的sheet表格
sheet
=
workbook
.
getSheetAt
((
sheetNum
-
1
));
// 保存Sheet中的数据到List集合中
List
<
Map
<
String
,
Object
>>
sheetList
=
ExcelUtils
.
getDataBySheet
(
sheet
,
startRow
,
startCol
,
formulaEvaluator
);
resultList
.
add
(
sheetList
);
}
else
{
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 该Excel表格只有"
+
totalSheetNum
+
"个Sheet表,而用户指定解析的Sheet表序号为"
+
sheetNum
+
",不在范围内"
);
throw
new
RuntimeException
(
"异常信息:该Excel表格只有"
+
totalSheetNum
+
"个Sheet表,而用户指定解析的Sheet表序号为"
+
sheetNum
+
",不在范围内"
);
}
}
catch
(
Exception
e
)
{
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 异常信息:"
+
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
try
{
if
(
workbook
!=
null
)
{
workbook
.
close
();
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 关闭Workbook资源"
);
}
if
(
input
!=
null
)
{
input
.
close
();
logger
.
info
(
"ExcelUtils.java ->> importExcel() ->> 关闭InputStream资源"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
logger
.
info
(
"========================= ExcelUtils.java ->> importExcel()从Excel表格中获取数据 ->> 结束 ========================="
);
}
return
resultList
;
}
/**
* 根据文件扩展名(.xls或.xlsx)获取对应的Workbook类型(HSSFWorkbook或XSSFWorkbook)
*
* @param input 关联excel文件的资源输入流
* @param filePath 文件路径
* @return
* @throws Exception
*/
private
static
Workbook
getWorkbookByExtensionName
(
InputStream
input
,
String
filePath
)
throws
Exception
{
logger
.
info
(
"========================= ExcelUtils.java ->> getWorkbookByExtensionName()根据excel文件后缀(.xls或.xlsx)获取Workbook的方法 ->> 开始 ========================="
);
if
(
filePath
==
null
||
""
.
equals
(
filePath
)
||
filePath
.
trim
().
isEmpty
())
{
logger
.
info
(
"ExcelUtils.java ->> getWorkbookByExtensionName() ->> 异常信息:excel文件路径不能为空"
);
throw
new
RuntimeException
(
"异常信息:excel文件路径不能为空"
);
}
int
index
=
filePath
.
lastIndexOf
(
"."
);
if
(
index
==
-
1
)
{
logger
.
info
(
"ExcelUtils.java ->> getWorkbookByExtensionName() ->> 异常信息:filePath指定的文件不带有扩展名,不属于文件类型"
);
throw
new
RuntimeException
(
"异常信息:filePath指定的文件不带有扩展名,不属于文件类型"
);
}
Workbook
wk
=
null
;
String
suffix
=
filePath
.
substring
(
index
);
logger
.
info
(
"ExcelUtils.java ->> getWorkbookByExtensionName() ->> filePath指定的文件扩展名为 = "
+
suffix
);
if
(
ExcelUtils
.
XLS
.
equals
(
suffix
))
{
wk
=
new
HSSFWorkbook
(
input
);
}
else
if
(
ExcelUtils
.
XLSX
.
equals
(
suffix
))
{
wk
=
new
XSSFWorkbook
(
input
);
}
else
{
logger
.
info
(
"ExcelUtils.java ->> getWorkbookByExtensionName() ->> 异常信息:filePath指定的文件扩展名不是excel文件格式(只能是.xls和.xlsx格式)"
);
throw
new
RuntimeException
(
"异常信息:filePath指定的文件扩展名不是excel文件格式(只能是.xls和.xlsx格式)"
);
}
logger
.
info
(
"========================= ExcelUtils.java ->> getWorkbookByExtensionName()根据excel文件后缀(.xls或.xlsx)获取Workbook的方法 ->> 结束 ========================="
);
return
wk
;
}
/**
* 获取sheet中的数据并返回一个List集合
*
* @param sheet Sheet对象
* @param startRow 开始解析的行数
* @param startCol 开始解析的列数
* @param formulaEvaluator 公式计算器实例
* @return
* @throws Exception
*/
private
static
List
<
Map
<
String
,
Object
>>
getDataBySheet
(
Sheet
sheet
,
int
startRow
,
int
startCol
,
FormulaEvaluator
formulaEvaluator
)
throws
Exception
{
logger
.
info
(
"========================= ExcelUtils.java ->> getDataBySheet()从Sheet表格中获取数据 ->> 开始 ========================="
);
List
<
Map
<
String
,
Object
>>
sheetList
=
new
ArrayList
<>();
/*
Sheet中的getPhysicalNumberOfRows()和getLastRowNum()区别:
> getPhysicalNumberOfRows():获取的是物理行数,即会跳过空行的情况。
> getLastRowNum():获取的是最后一行的行编号(编号从0开始)。
*/
// 得到表格中总共的行数,会比实际的行数小1
int
totalRowNum
=
sheet
.
getLastRowNum
()
+
1
;
logger
.
info
(
"ExcelUtils.java ->> getDataBySheet() ->> 当前Sheet表格中总行数totalRowNum = "
+
totalRowNum
);
// 循环当前表格中所有行
for
(
int
i
=
(
startRow
-
1
);
i
<
totalRowNum
;
i
++)
{
// 得到Row行对象
Row
row
=
sheet
.
getRow
(
i
);
if
(
row
==
null
||
row
.
toString
().
trim
().
isEmpty
()
||
""
.
equals
(
row
.
toString
())
||
"null"
.
equals
(
row
.
toString
()))
{
logger
.
info
(
"ExcelUtils.java ->> getDataBySheet() ->> 第"
+
(
i
+
1
)
+
"行的内容为空,因此解析下一行"
);
continue
;
}
/*
Row中的getPhysicalNumberOfCells()和getLastCellNum()区别:
> getPhysicalNumberOfCells():获取的是物理列数,即会跳过空列的情况。
> getLastCellNum():获取的是最后一列的列编号(编号从0开始)。
*/
// 得到当前行中所有的单元格数量
int
totalCellNum
=
row
.
getLastCellNum
();
logger
.
info
(
"ExcelUtils.java ->> getDataBySheet() ->> 第"
+
(
i
+
1
)
+
"行的总列数totalCellNum = "
+
totalCellNum
);
// 创建Map集合用于存储当前行中所有的单元格数据
Map
<
String
,
Object
>
rowMap
=
new
HashMap
<>();
// 循环当前行中所有单元格
for
(
int
j
=
(
startCol
-
1
);
j
<
totalCellNum
;
j
++)
{
// 得到Cell列对象
Cell
cell
=
row
.
getCell
(
j
);
// 如果等于空
if
(
cell
==
null
||
cell
.
toString
().
trim
().
isEmpty
()
||
""
.
equals
(
cell
.
toString
())
||
"null"
.
equals
(
cell
.
toString
()))
{
rowMap
.
put
(
"Row"
+
(
i
+
1
)
+
"-Col"
+
(
j
+
1
),
""
);
logger
.
info
(
"ExcelUtils.java ->> getDataBySheet() ->> 第"
+
(
i
+
1
)
+
"行的第"
+
(
j
+
1
)
+
"列"
+
"的单元格的内容为空,因此解析下一个单元格"
);
continue
;
}
// 进行公式解析,最后只存在Boolean、Numeric和String三种数据类型,此外就是Error了
// 其余数据类型,根据官方文档,完全可以忽略:http://poi.apache.org/spreadsheet/eval.html
CellValue
cellValue
=
formulaEvaluator
.
evaluate
(
cell
);
// 得到对应单元格的内容
String
result
=
ExcelUtils
.
getCellResultByCellType
(
cell
,
cellValue
,
formulaEvaluator
);
logger
.
info
(
"ExcelUtils.java ->> getDataBySheet() ->> 第"
+
(
i
+
1
)
+
"行的第"
+
(
j
+
1
)
+
"列的单元格内容 = "
+
result
);
// 保存到Map集合
rowMap
.
put
(
"Row"
+
(
i
+
1
)
+
"-Col"
+
(
j
+
1
),
result
);
}
// 将每个行对象保存到List中
sheetList
.
add
(
rowMap
);
}
logger
.
info
(
"========================= ExcelUtils.java ->> getDataBySheet()从Sheet表格中获取数据 ->> 结束 ========================="
);
return
sheetList
;
}
/**
* 根据单元格数据类型获取单元格的值
*
* @param cell Cell单元格类型
* @param cellValue CellValue单元格值类型
* @param formulaEvaluator 公式计算器
* @return
*/
private
static
String
getCellResultByCellType
(
Cell
cell
,
CellValue
cellValue
,
FormulaEvaluator
formulaEvaluator
)
{
String
result
=
null
;
CellType
cellTypeEnum
=
cellValue
.
getCellTypeEnum
();
if
(
cellTypeEnum
==
CellType
.
NUMERIC
)
{
// 判断当前单元格是否为日期格式
if
(
HSSFDateUtil
.
isCellDateFormatted
(
cell
))
{
logger
.
info
(
"ExcelUtils.java ->> getCellResultByCellType() ->> 当前单元格类型为数值型中的日期类型"
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
result
=
sdf
.
format
(
cell
.
getDateCellValue
());
}
else
{
logger
.
info
(
"ExcelUtils.java ->> getCellResultByCellType() ->> 当前单元格类型为数值型中的整数型"
);
DataFormatter
dataFormatter
=
new
DataFormatter
();
result
=
dataFormatter
.
formatCellValue
(
cell
,
formulaEvaluator
);
}
}
else
if
(
cellTypeEnum
==
CellType
.
STRING
)
{
logger
.
info
(
"ExcelUtils.java ->> getCellResultByCellType() ->> 当前单元格类型为字符串"
);
result
=
cellValue
.
getStringValue
();
}
else
if
(
cellTypeEnum
==
CellType
.
BOOLEAN
)
{
logger
.
info
(
"ExcelUtils.java ->> getCellResultByCellType() ->> 当前单元格类型为布尔类型"
);
result
=
String
.
valueOf
(
cellValue
.
getBooleanValue
());
}
return
result
;
}
/*
* 上面是Excel导入功能
* ====================================================================================================================================
* 下面是Excel导出功能
*/
/**
* 导出数据到Excel
*
* @param title Excel表格中sheet的名称以及大标题行的标题
* @param rowName 小标题行的标题
* @param dataList 主体数据
* @param out 输出流
* @throws Exception
*/
public
static
void
exportExcel
(
String
title
,
String
[]
rowName
,
List
<
Object
[]>
dataList
,
OutputStream
out
)
{
logger
.
info
(
"========================= ExcelUtils.java ->> exportExcel()导出数据到Excel中 ->> 开始 ========================="
);
XSSFWorkbook
workbook
=
null
;
try
{
/*
1,创建工作簿对象,然后创建大标题行,并设置标题
*/
// 创建工作簿对象
workbook
=
new
XSSFWorkbook
();
// 创建一个表格对象
XSSFSheet
sheet
=
workbook
.
createSheet
(
"Sheet1"
);
String
mark
=
"title"
;
// 定义大标题行的样式
XSSFCellStyle
titleCellStyle
=
ExcelUtils
.
getCellStyle
(
workbook
,
mark
);
// 如果参数title不等空,则设置Sheet表格的大标题
if
(!
"null"
.
equals
(
title
)
&&
title
!=
null
&&
!
""
.
equals
(
title
)
&&
!
title
.
trim
().
isEmpty
())
{
// 创建表格大标题行
XSSFRow
titleRow
=
sheet
.
createRow
(
0
);
// 创建表格大标题行的第一个单元格
XSSFCell
titleCell
=
titleRow
.
createCell
(
0
);
// 定义大标题行的宽度和高度(合并单元格)
sheet
.
addMergedRegion
(
new
CellRangeAddress
(
0
,
1
,
0
,
(
rowName
.
length
-
1
)));
// 设置大标题行的单元格样式
titleCell
.
setCellStyle
(
titleCellStyle
);
// 设置大标题行的单元格名称
titleCell
.
setCellValue
(
title
);
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 导出到Excel中Sheet表格的标题title = "
+
title
);
}
/*
2,创建小标题行并设置标题
*/
// 定义所需列数 = 参数数组长度
int
columnNum
=
rowName
.
length
;
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 导出到Excel中Sheet表格的标题列数columnNum = "
+
columnNum
);
// 创建小标题行,由于0行和1行用作大标题行,所以小标题行从2开始
XSSFRow
subTitleRow
=
sheet
.
createRow
(
2
);
// 将列头设置到sheet的单元格中
for
(
int
i
=
0
;
i
<
columnNum
;
i
++)
{
// 创建小标题行的单元格
XSSFCell
subTitleCell
=
subTitleRow
.
createCell
(
i
);
// 设置单元格的单元格类型
subTitleCell
.
setCellType
(
CellType
.
STRING
);
// 使用数组中的数据作为单元格的文本来创建小标题
XSSFRichTextString
text
=
new
XSSFRichTextString
(
rowName
[
i
]);
// 设置小标题单元格的样式
subTitleCell
.
setCellStyle
(
titleCellStyle
);
// 设置文本到小标题单元格中
subTitleCell
.
setCellValue
(
text
);
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 设置小标题行的第"
+
(
i
+
1
)
+
"列的标题为 = "
+
text
);
}
/*
3,开始循环主体数据,并设置到sheet表格中
*/
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 要导入到Excel表格中的数据行数 = "
+
dataList
.
size
());
mark
=
""
;
// 定义普通行的样式
XSSFCellStyle
style
=
ExcelUtils
.
getCellStyle
(
workbook
,
mark
);
// 循环遍历参数主体数据
for
(
int
i
=
0
;
i
<
dataList
.
size
();
i
++)
{
// 遍历每个Object数组
Object
[]
objArr
=
dataList
.
get
(
i
);
// 创建当前要填充数据的行对象
XSSFRow
currentRow
=
sheet
.
createRow
(
i
+
3
);
// 循环遍历Object数组
for
(
int
j
=
0
;
j
<
objArr
.
length
;
j
++)
{
XSSFCell
cell
=
null
;
// 如果是每行的第一个单元格
if
(
j
==
0
)
{
// 创建单元格,且设置单元格类型为数值型
cell
=
currentRow
.
createCell
(
j
,
CellType
.
NUMERIC
);
cell
.
setCellValue
(
objArr
[
j
].
toString
());
// 设置单元格的样式
cell
.
setCellStyle
(
titleCellStyle
);
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 每行的第一个单元格作为行序号标记,当前序号为 = "
+
(
i
+
1
));
}
else
{
// 创建单元格,且设置单元格类型为字符串型
cell
=
currentRow
.
createCell
(
j
,
CellType
.
STRING
);
// 如果数组中的内容不为空
if
(
objArr
[
j
]
!=
null
&&
!
""
.
equals
(
objArr
[
j
]))
{
// 设置到单元格中
cell
.
setCellValue
(
objArr
[
j
].
toString
());
}
// 设置单元格的样式
cell
.
setCellStyle
(
style
);
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 第"
+
(
i
+
1
)
+
"行的第"
+
(
j
+
1
)
+
"个单元格的内容为 = "
+
(
i
+
1
));
}
}
}
// 让列宽随着导出的列长自动适应
for
(
int
colNum
=
0
;
colNum
<
columnNum
;
colNum
++)
{
//
int
columnWidth
=
sheet
.
getColumnWidth
(
colNum
)
/
256
;
for
(
int
rowNum
=
0
;
rowNum
<
sheet
.
getLastRowNum
();
rowNum
++)
{
XSSFRow
currentRow
;
if
(
sheet
.
getRow
(
rowNum
)
==
null
)
{
currentRow
=
sheet
.
createRow
(
rowNum
);
}
else
{
currentRow
=
sheet
.
getRow
(
rowNum
);
}
if
(
currentRow
.
getCell
(
colNum
)
!=
null
)
{
XSSFCell
currentCell
=
currentRow
.
getCell
(
colNum
);
if
(
currentCell
.
getCellTypeEnum
()
==
CellType
.
STRING
)
{
int
length
=
currentCell
.
getStringCellValue
().
getBytes
().
length
;
if
(
columnWidth
<
length
)
{
columnWidth
=
length
;
}
}
}
}
// 设置列宽
if
(
colNum
==
0
)
{
// 如果是首列
sheet
.
setColumnWidth
(
colNum
,
(
columnWidth
-
2
)
*
256
);
}
else
{
// 否则
sheet
.
setColumnWidth
(
colNum
,
(
columnWidth
+
4
)
*
256
);
}
}
workbook
.
write
(
out
);
}
catch
(
Exception
e
)
{
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 异常信息:"
+
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
try
{
if
(
workbook
!=
null
)
{
workbook
.
close
();
logger
.
info
(
"ExcelUtils.java ->> exportExcel() ->> 关闭Workbook资源"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
logger
.
info
(
"========================= ExcelUtils.java ->> exportExcel()导出数据到Excel中 ->> 结束 ========================="
);
}
}
/**
* 得到单元格样式
*
* @param workbook XSSFWorkbook类型对象
* @param mark 标记,当且仅当为title时获取标题行的样式,否则为普通单元格样式
* @return
*/
private
static
XSSFCellStyle
getCellStyle
(
XSSFWorkbook
workbook
,
String
mark
)
{
// 创建字体对象
XSSFFont
font
=
workbook
.
createFont
();
// 创建单元格样式对象
XSSFCellStyle
cellStyle
=
workbook
.
createCellStyle
();
if
(
"title"
.
equals
(
mark
))
{
// 设置字体的大小
font
.
setFontHeightInPoints
((
short
)
11
);
// 设置字体加粗
font
.
setBold
(
true
);
// 设置字体高度
// font.setFontHeight((short) 240);
}
else
{
// 设置字体的大小
font
.
setFontHeightInPoints
((
short
)
10
);
// 设置字体加粗
font
.
setBold
(
false
);
// 设置单元格左边框
cellStyle
.
setBorderLeft
(
BorderStyle
.
THIN
);
// 设置单元格左边框颜色
cellStyle
.
setLeftBorderColor
(
new
XSSFColor
());
}
// 设置单元格的边框样式
ExcelUtils
.
setCellCommonStyle
(
cellStyle
,
font
);
return
cellStyle
;
}
/**
* 设置单元格的边框样式
*
* @param cellStyle XSSFCellStyle单元格样式对象
* @param font XSSFFont字体对象
*/
private
static
void
setCellCommonStyle
(
XSSFCellStyle
cellStyle
,
XSSFFont
font
)
{
// 设置字体的格式
font
.
setFontName
(
"等线"
);
// 设置单元格上边框
cellStyle
.
setBorderTop
(
BorderStyle
.
THIN
);
// 设置单元格上边框颜色
cellStyle
.
setTopBorderColor
(
new
XSSFColor
());
// 设置单元格右边框
cellStyle
.
setBorderRight
(
BorderStyle
.
THIN
);
// 设置单元格右边框颜色
cellStyle
.
setRightBorderColor
(
new
XSSFColor
());
// 设置单元格底边框
cellStyle
.
setBorderBottom
(
BorderStyle
.
THIN
);
// 设置单元格底边框颜色
cellStyle
.
setBottomBorderColor
(
new
XSSFColor
());
// 在单元格样式用应用设置的字体
cellStyle
.
setFont
(
font
);
// 设置是否自动换行
cellStyle
.
setWrapText
(
false
);
// 设置水平对齐的样式为居中对齐
cellStyle
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
// 设置垂直对齐的样式为居中对齐
cellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
}
}
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