Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
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
项目统一框架
amos-boot-biz
Commits
383ba898
Commit
383ba898
authored
Jul 02, 2021
by
tangwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解决冲突
parent
f72d279a
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
693 additions
and
623 deletions
+693
-623
BaseDto.java
...in/java/com/yeejoin/amos/boot/biz/common/dto/BaseDto.java
+6
-6
FireChemicalDto.java
...yeejoin/amos/boot/module/jcs/api/dto/FireChemicalDto.java
+25
-11
ExcelListener.java
...yeejoin/amos/boot/module/jcs/api/excel/ExcelListener.java
+48
-48
ExcelUtil.java
...com/yeejoin/amos/boot/module/jcs/api/excel/ExcelUtil.java
+246
-201
ExplicitConstraint.java
...in/amos/boot/module/jcs/api/excel/ExplicitConstraint.java
+20
-20
ExplicitInterface.java
...oin/amos/boot/module/jcs/api/excel/ExplicitInterface.java
+12
-12
RoleNameExplicitConstraint.java
...boot/module/jcs/api/excel/RoleNameExplicitConstraint.java
+29
-29
TemplateCellWriteHandler.java
...s/boot/module/jcs/api/excel/TemplateCellWriteHandler.java
+81
-78
TemplateCellWriteHandlerDate.java
...ot/module/jcs/api/excel/TemplateCellWriteHandlerDate.java
+115
-115
ExcelController.java
.../amos/boot/module/jcs/biz/controller/ExcelController.java
+101
-101
pom.xml
pom.xml
+10
-2
No files found.
amos-boot-biz-common/src/main/java/com/yeejoin/amos/boot/biz/common/dto/BaseDto.java
View file @
383ba898
package
com
.
yeejoin
.
amos
.
boot
.
biz
.
common
.
dto
;
//
import com.alibaba.excel.annotation.ExcelIgnore;
import
com.alibaba.excel.annotation.ExcelIgnore
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
...
...
@@ -18,22 +18,22 @@ public class BaseDto implements Serializable{
/**
*
*/
//
@ExcelIgnore
@ExcelIgnore
private
static
final
long
serialVersionUID
=
1L
;
//
@ExcelIgnore
@ExcelIgnore
@ApiModelProperty
(
value
=
"主键ID"
)
protected
Long
sequenceNbr
;
//
@ExcelIgnore
@ExcelIgnore
@ApiModelProperty
(
value
=
"更新时间"
)
protected
Date
recDate
;
//
@ExcelIgnore
@ExcelIgnore
@ApiModelProperty
(
value
=
"更新人"
)
protected
String
recUserId
;
/**
* 是否删除
*/
//
@ExcelIgnore
@ExcelIgnore
@TableField
(
value
=
"is_delete"
)
private
Boolean
isDelete
;
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/dto/FireChemicalDto.java
View file @
383ba898
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
dto
;
//
import com.alibaba.excel.annotation.ExcelIgnore;
//
import com.alibaba.excel.annotation.ExcelProperty;
import
com.alibaba.excel.annotation.ExcelIgnore
;
import
com.alibaba.excel.annotation.ExcelProperty
;
import
com.yeejoin.amos.boot.biz.common.dto.BaseDto
;
//
import com.yeejoin.amos.boot.module.jcs.api.excel.ExplicitConstraint;
//
import com.yeejoin.amos.boot.module.jcs.api.excel.RoleNameExplicitConstraint;
import
com.yeejoin.amos.boot.module.jcs.api.excel.ExplicitConstraint
;
import
com.yeejoin.amos.boot.module.jcs.api.excel.RoleNameExplicitConstraint
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
...
...
@@ -22,67 +22,81 @@ import java.util.Date;
@EqualsAndHashCode
(
callSuper
=
true
)
@ApiModel
(
value
=
"FireChemicalDto"
,
description
=
"危化品"
)
public
class
FireChemicalDto
extends
BaseDto
{
@ExcelIgnore
private
static
final
long
serialVersionUID
=
1L
;
@ApiModelProperty
(
value
=
"casNo"
)
@ExcelProperty
(
value
=
"casNo"
,
index
=
1
)
private
String
casNo
;
@ApiModelProperty
(
value
=
"国际危险号"
)
@ExcelProperty
(
value
=
"国际危险号"
,
index
=
2
)
private
String
dangerId
;
@ApiModelProperty
(
value
=
"防护处理"
)
@ExcelProperty
(
value
=
"防护处理"
,
index
=
3
)
private
String
defendWay
;
@ApiModelProperty
(
value
=
"处理措施"
)
@ExcelProperty
(
value
=
"处理措施"
,
index
=
4
)
private
String
dispose
;
@ApiModelProperty
(
value
=
"英文名"
)
@ExcelProperty
(
value
=
"英文名"
,
index
=
5
)
private
String
englishName
;
@ApiModelProperty
(
value
=
"分子式"
)
@ExcelProperty
(
value
=
"分子式"
,
index
=
6
)
private
String
formula
;
@ApiModelProperty
(
value
=
"主要成分"
)
@ExcelProperty
(
value
=
"主要成分"
,
index
=
7
)
private
String
ingredient
;
@ApiModelProperty
(
value
=
"泄漏处理"
)
@ExcelProperty
(
value
=
"泄漏处理"
,
index
=
8
)
private
String
leakWay
;
@ExcelProperty
(
value
=
"中文名"
,
index
=
0
)
@ApiModelProperty
(
value
=
"中文名"
)
private
String
name
;
@ApiModelProperty
(
value
=
"性状"
)
@ExcelProperty
(
value
=
"性状"
,
index
=
9
)
private
String
property
;
@ApiModelProperty
(
value
=
"贮藏方法"
)
@ExcelProperty
(
value
=
"贮藏方法"
,
index
=
10
)
private
String
store
;
@ApiModelProperty
(
value
=
"症状"
)
@ExcelProperty
(
value
=
"症状"
,
index
=
11
)
private
String
symptom
;
@ApiModelProperty
(
value
=
"禁忌物/禁忌"
)
@ExcelProperty
(
value
=
"禁忌物/禁忌"
,
index
=
12
)
private
String
tabu
;
@ExcelIgnore
@ApiModelProperty
(
value
=
"类型code"
)
private
String
typeCode
;
@ExplicitConstraint
(
type
=
"CHEMICALTYPE"
,
indexNum
=
13
,
sourceClass
=
RoleNameExplicitConstraint
.
class
)
//动态下拉内容
@ApiModelProperty
(
value
=
"类型名称"
)
@ExcelProperty
(
value
=
"类型名称"
,
index
=
13
)
private
String
type
;
// @ExplicitConstraint(indexNum=14,source = {"男","女"}) //固定下拉内容
@ExcelProperty
(
value
=
"国标号"
,
index
=
14
)
@ApiModelProperty
(
value
=
"国标号"
)
private
String
un
;
@ApiModelProperty
(
value
=
"化学品图片"
)
@ExcelProperty
(
value
=
"化学品图片"
,
index
=
15
)
private
String
image
;
@ExcelIgnore
@ApiModelProperty
(
value
=
"更新时间"
)
private
Date
updateTime
;
@ExcelIgnore
@ApiModelProperty
(
value
=
"操作人名称"
)
private
String
recUserName
;
...
...
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/ExcelListener.java
View file @
383ba898
//
package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//
import java.util.ArrayList;
//
import java.util.List;
//
//
import com.alibaba.excel.context.AnalysisContext;
//
import com.alibaba.excel.event.AnalysisEventListener;
//
import com.alibaba.fastjson.JSON;
//
//
//
如果没有特殊说明,下面的案例将默认使用这个监听器
//
public class ExcelListener<T> extends AnalysisEventListener<T> {
//
//
List<T> list = new ArrayList<T>();
//
//
public List<T> getList() {
//
return list;
//
}
//
//
public void setList(List<T> list) {
//
this.list = list;
//
}
//
/
//
**
//
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
//
*/
//
public ExcelListener() {}
//
//
/**
//
* 这个每一条数据解析都会来调用
//
*
//
* @param data
//
* @param context
//
*/
//
@Override
//
public void invoke(T data, AnalysisContext context) {
//
list.add(data);
//
}
//
//
/**
//
* 所有数据解析完成了 都会来调用
//
*
//
* @param context
//
*/
//
@Override
//
public void doAfterAllAnalysed(AnalysisContext context) {
//
//
}
//
}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
java.util.ArrayList
;
import
java.util.List
;
import
com.alibaba.excel.context.AnalysisContext
;
import
com.alibaba.excel.event.AnalysisEventListener
;
import
com.alibaba.fastjson.JSON
;
//如果没有特殊说明,下面的案例将默认使用这个监听器
public
class
ExcelListener
<
T
>
extends
AnalysisEventListener
<
T
>
{
List
<
T
>
list
=
new
ArrayList
<
T
>();
public
List
<
T
>
getList
()
{
return
list
;
}
public
void
setList
(
List
<
T
>
list
)
{
this
.
list
=
list
;
}
/**
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
*/
public
ExcelListener
()
{}
/**
* 这个每一条数据解析都会来调用
*
* @param data
* @param context
*/
@Override
public
void
invoke
(
T
data
,
AnalysisContext
context
)
{
list
.
add
(
data
);
}
/**
* 所有数据解析完成了 都会来调用
*
* @param context
*/
@Override
public
void
doAfterAllAnalysed
(
AnalysisContext
context
)
{
}
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/ExcelUtil.java
View file @
383ba898
//package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.lang.reflect.Field;
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import javax.servlet.http.HttpServletResponse;
//import org.apache.poi.ss.usermodel.HorizontalAlignment;
//import org.apache.poi.ss.usermodel.IndexedColors;
//import org.springframework.web.multipart.MultipartFile;
//
//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.ExcelReader;
//import com.alibaba.excel.read.metadata.ReadSheet;
//import com.alibaba.excel.support.ExcelTypeEnum;
//import com.alibaba.excel.write.metadata.style.WriteCellStyle;
//import com.alibaba.excel.write.metadata.style.WriteFont;
//import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
//import com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper;
//
//public class ExcelUtil {
// /**
// * 生成excel模板
// *
// * @param response
// * @param fileName 下载的文件名,
// * @param sheetName sheet名
// * @param data 导出的数据
// * @param model 导出的头
// * @param heardHeight 头行高
// * @param flag true模板填充下拉 false 不填充
// */
// public static void createTemplate(HttpServletResponse response, String fileName,
// String sheetName, List<? extends Object> data,
// Class<?> model, DataDictionaryMapper dataDictionaryMapper,boolean flag) {
//
// HorizontalCellStyleStrategy horizontalCellStyleStrategy = setMyCellStyle();
// try {
// //下拉列表集合
// Map<Integer, String[]> explicitListConstraintMap = new HashMap<>();
// if(flag) {
// //循环获取对应列得下拉列表信息
// Field[] declaredFields = model.getDeclaredFields();
// for (int i = 0; i < declaredFields.length; i++) {
// Field field = declaredFields[i];
// //解析注解信息
// ExplicitConstraint explicitConstraint = field.getAnnotation(ExplicitConstraint.class);
// resolveExplicitConstraint(explicitListConstraintMap,explicitConstraint,dataDictionaryMapper);
// }
// }
// EasyExcel.write(getOutputStream(fileName, response, ExcelTypeEnum.XLSX), model).
// excelType(ExcelTypeEnum.XLSX).sheet(sheetName)
// .registerWriteHandler(new TemplateCellWriteHandlerDate(explicitListConstraintMap))
// .registerWriteHandler(new TemplateCellWriteHandler())
// .registerWriteHandler(horizontalCellStyleStrategy)
// .doWrite(data);
// } catch (Exception e) {
// e.printStackTrace();
// throw new RuntimeException("系统异常!");
//
// }
// }
// /**
// * 读取 Excel(第一个 sheet) 指定行开始读取
// * @param excel 文件
// * @param rowType 模板实体类
// * @param header 指定不读取的表头行数,
// * @param <T>
// * @return 集合数据
// * @throws ExcelException
// */
// public static <T> List<T> readFirstSheetExcel(MultipartFile excel, Class<T> rowType,int header) throws Exception {
// ExcelReader reader = getReader(excel, header);
// if (reader == null) {
// return new ArrayList<>();
// }
// return readExcel(reader, rowType, 0);
// }
//
//
//
// /**
// * 读取 Excel(多个 sheet)
// * @param reader 读取的excel
// * @param rowModel excel模板实体类
// * @param sheetCount sheet
// * @param <T>
// * @return
// */
// public static <T> List<T> readExcel(ExcelReader reader, Class<T> rowModel, int sheetCount) {
// if (reader == null) {
// return new ArrayList<>();
// }
// ExcelListener<T> excelListener = new ExcelListener<>();
// ReadSheet readSheet = EasyExcel.readSheet(sheetCount)
// .head(rowModel)
// .registerReadListener(excelListener)
// .build();
// reader.read(readSheet);
// return excelListener.getList();
// }
//
//
//
//
// /**
// *
// * @param excel 需要解析的 Excel 文件
// * @param header 指定不读取表头行数,
// * @return
// * @throws ExcelException
// */
// public static ExcelReader getReader(MultipartFile excel,int header) throws Exception {
// String fileName = excel.getOriginalFilename();
// if (fileName == null) {
// throw new Exception("文件不存在!");
// }
// if (!fileName.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue()) && !fileName.toLowerCase().endsWith(ExcelTypeEnum.XLSX.getValue())) {
// throw new Exception("文件类型异常!");
// }
// InputStream inputStream;
// try {
// inputStream = excel.getInputStream();
// return EasyExcel.read(inputStream).
// headRowNumber(header).
// build();
// } catch (IOException e) {
// //do something
// }
// return null;
// }
//
//
//
// /**
// * 解析注解内容 获取下列表信息
// * @param explicitConstraint
// * @return
// */
// public static Map<Integer, String[]> resolveExplicitConstraint( Map<Integer, String[]> explicitListConstraintMap,ExplicitConstraint explicitConstraint, DataDictionaryMapper dataDictionaryMapper){
// if (explicitConstraint == null) {
// return null;
// }
// //固定下拉信息
// String[] source = explicitConstraint.source();
// if (source.length > 0) {
// explicitListConstraintMap.put(explicitConstraint.indexNum(), source);
// }
// //动态下拉信息
// Class<? extends ExplicitInterface>[] classes = explicitConstraint.sourceClass();
// if (classes.length>0){
// ExplicitInterface explicitInterface = null;
// try {
// explicitInterface = classes[0].newInstance();
// String[] source1 = explicitInterface.source(explicitConstraint.type(),dataDictionaryMapper);
// if (source1.length>0){
// explicitListConstraintMap.put(explicitConstraint.indexNum(), source1);
// }
// } catch (InstantiationException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
// }
// return null;
// }
//
//
//
//
// /**
// * 导出文件时为Writer生成OutputStream
// */
// private static OutputStream getOutputStream(String fileName, HttpServletResponse response, ExcelTypeEnum excelTypeEnum) throws Exception {
// //创建本地文件
// String filePath = fileName + excelTypeEnum.getValue();
// try {
// fileName = new String(filePath.getBytes(), StandardCharsets.ISO_8859_1);
// response.setCharacterEncoding(StandardCharsets.UTF_8.name());
// response.setContentType("application/vnd.ms-excel");
// response.addHeader("Content-Disposition", "filename=" + fileName);
// return response.getOutputStream();
// } catch (IOException e) {
// throw new Exception("系统异常");
// }
// }
//
// /**
// * 创建我的cell 策略
// *
// * @return
// */
// public static HorizontalCellStyleStrategy setMyCellStyle() {
//
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.lang.reflect.Field
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.poi.ss.usermodel.BorderStyle
;
import
org.apache.poi.ss.usermodel.HorizontalAlignment
;
import
org.apache.poi.ss.usermodel.IndexedColors
;
import
org.apache.poi.ss.usermodel.VerticalAlignment
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.excel.ExcelReader
;
import
com.alibaba.excel.read.metadata.ReadSheet
;
import
com.alibaba.excel.support.ExcelTypeEnum
;
import
com.alibaba.excel.write.metadata.style.WriteCellStyle
;
import
com.alibaba.excel.write.metadata.style.WriteFont
;
import
com.alibaba.excel.write.style.HorizontalCellStyleStrategy
;
import
com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper
;
public
class
ExcelUtil
{
/**
* 生成excel模板
*
* @param response
* @param fileName 下载的文件名,
* @param sheetName sheet名
* @param data 导出的数据
* @param model 导出的头
* @param heardHeight 头行高
* @param flag true模板填充下拉 false 不填充
*/
public
static
void
createTemplate
(
HttpServletResponse
response
,
String
fileName
,
String
sheetName
,
List
<?
extends
Object
>
data
,
Class
<?>
model
,
DataDictionaryMapper
dataDictionaryMapper
,
boolean
flag
)
{
HorizontalCellStyleStrategy
horizontalCellStyleStrategy
=
setMyCellStyle
();
try
{
//下拉列表集合
Map
<
Integer
,
String
[]>
explicitListConstraintMap
=
new
HashMap
<>();
if
(
flag
)
{
//循环获取对应列得下拉列表信息
Field
[]
declaredFields
=
model
.
getDeclaredFields
();
for
(
int
i
=
0
;
i
<
declaredFields
.
length
;
i
++)
{
Field
field
=
declaredFields
[
i
];
//解析注解信息
ExplicitConstraint
explicitConstraint
=
field
.
getAnnotation
(
ExplicitConstraint
.
class
);
resolveExplicitConstraint
(
explicitListConstraintMap
,
explicitConstraint
,
dataDictionaryMapper
);
}
}
EasyExcel
.
write
(
getOutputStream
(
fileName
,
response
,
ExcelTypeEnum
.
XLSX
),
model
).
excelType
(
ExcelTypeEnum
.
XLSX
).
sheet
(
sheetName
)
.
registerWriteHandler
(
new
TemplateCellWriteHandlerDate
(
explicitListConstraintMap
))
.
registerWriteHandler
(
new
TemplateCellWriteHandler
())
.
registerWriteHandler
(
horizontalCellStyleStrategy
)
.
doWrite
(
data
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
RuntimeException
(
"系统异常!"
);
}
}
/**
* 读取 Excel(第一个 sheet) 指定行开始读取
* @param excel 文件
* @param rowType 模板实体类
* @param header 指定不读取的表头行数,
* @param <T>
* @return 集合数据
* @throws ExcelException
*/
public
static
<
T
>
List
<
T
>
readFirstSheetExcel
(
MultipartFile
excel
,
Class
<
T
>
rowType
,
int
header
)
throws
Exception
{
ExcelReader
reader
=
getReader
(
excel
,
header
);
if
(
reader
==
null
)
{
return
new
ArrayList
<>();
}
return
readExcel
(
reader
,
rowType
,
0
);
}
/**
* 读取 Excel(多个 sheet)
* @param reader 读取的excel
* @param rowModel excel模板实体类
* @param sheetCount sheet
* @param <T>
* @return
*/
public
static
<
T
>
List
<
T
>
readExcel
(
ExcelReader
reader
,
Class
<
T
>
rowModel
,
int
sheetCount
)
{
if
(
reader
==
null
)
{
return
new
ArrayList
<>();
}
ExcelListener
<
T
>
excelListener
=
new
ExcelListener
<>();
ReadSheet
readSheet
=
EasyExcel
.
readSheet
(
sheetCount
)
.
head
(
rowModel
)
.
registerReadListener
(
excelListener
)
.
build
();
reader
.
read
(
readSheet
);
return
excelListener
.
getList
();
}
/**
*
* @param excel 需要解析的 Excel 文件
* @param header 指定不读取表头行数,
* @return
* @throws ExcelException
*/
public
static
ExcelReader
getReader
(
MultipartFile
excel
,
int
header
)
throws
Exception
{
String
fileName
=
excel
.
getOriginalFilename
();
if
(
fileName
==
null
)
{
throw
new
Exception
(
"文件不存在!"
);
}
if
(!
fileName
.
toLowerCase
().
endsWith
(
ExcelTypeEnum
.
XLS
.
getValue
())
&&
!
fileName
.
toLowerCase
().
endsWith
(
ExcelTypeEnum
.
XLSX
.
getValue
()))
{
throw
new
Exception
(
"文件类型异常!"
);
}
InputStream
inputStream
;
try
{
inputStream
=
excel
.
getInputStream
();
return
EasyExcel
.
read
(
inputStream
).
headRowNumber
(
header
).
build
();
}
catch
(
IOException
e
)
{
//do something
}
return
null
;
}
/**
* 解析注解内容 获取下列表信息
* @param explicitConstraint
* @return
*/
public
static
Map
<
Integer
,
String
[]>
resolveExplicitConstraint
(
Map
<
Integer
,
String
[]>
explicitListConstraintMap
,
ExplicitConstraint
explicitConstraint
,
DataDictionaryMapper
dataDictionaryMapper
){
if
(
explicitConstraint
==
null
)
{
return
null
;
}
//固定下拉信息
String
[]
source
=
explicitConstraint
.
source
();
if
(
source
.
length
>
0
)
{
explicitListConstraintMap
.
put
(
explicitConstraint
.
indexNum
(),
source
);
}
//动态下拉信息
Class
<?
extends
ExplicitInterface
>[]
classes
=
explicitConstraint
.
sourceClass
();
if
(
classes
.
length
>
0
){
ExplicitInterface
explicitInterface
=
null
;
try
{
explicitInterface
=
classes
[
0
].
newInstance
();
String
[]
source1
=
explicitInterface
.
source
(
explicitConstraint
.
type
(),
dataDictionaryMapper
);
if
(
source1
.
length
>
0
){
explicitListConstraintMap
.
put
(
explicitConstraint
.
indexNum
(),
source1
);
}
}
catch
(
InstantiationException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
}
return
null
;
}
/**
* 导出文件时为Writer生成OutputStream
*/
private
static
OutputStream
getOutputStream
(
String
fileName
,
HttpServletResponse
response
,
ExcelTypeEnum
excelTypeEnum
)
throws
Exception
{
//创建本地文件
String
filePath
=
fileName
+
excelTypeEnum
.
getValue
();
try
{
fileName
=
new
String
(
filePath
.
getBytes
(),
StandardCharsets
.
ISO_8859_1
);
response
.
setCharacterEncoding
(
StandardCharsets
.
UTF_8
.
name
());
response
.
setContentType
(
"application/vnd.ms-excel"
);
response
.
addHeader
(
"Content-Disposition"
,
"filename="
+
fileName
);
return
response
.
getOutputStream
();
}
catch
(
IOException
e
)
{
throw
new
Exception
(
"系统异常"
);
}
}
/**
* 创建我的cell 策略
*
* @return
*/
public
static
HorizontalCellStyleStrategy
setMyCellStyle
()
{
// // 头的策略
// WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// // 设置表头居中对齐
// headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// // 颜色
// headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//
// WriteFont headWriteFont = new WriteFont();
// headWriteFont.setFontHeightInPoints((short) 10);
// // 字体
...
...
@@ -216,5 +220,46 @@
// HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
// // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// return horizontalCellStyleStrategy;
// }
//}
// 表头样式策略
WriteCellStyle
headWriteCellStyle
=
new
WriteCellStyle
();
//设置表头居中对齐
headWriteCellStyle
.
setHorizontalAlignment
(
HorizontalAlignment
.
CENTER
);
//表头前景色
headWriteCellStyle
.
setFillForegroundColor
(
IndexedColors
.
GREY_25_PERCENT
.
getIndex
());
WriteFont
headWriteFont
=
new
WriteFont
();
headWriteFont
.
setBold
(
true
);
headWriteFont
.
setFontName
(
"宋体"
);
headWriteFont
.
setFontHeightInPoints
((
short
)
10
);
headWriteCellStyle
.
setWriteFont
(
headWriteFont
);
//2 内容样式策略
WriteCellStyle
contentWriteCellStyle
=
new
WriteCellStyle
();
WriteFont
contentWriteFont
=
new
WriteFont
();
//内容字体大小
contentWriteFont
.
setFontName
(
"宋体"
);
contentWriteFont
.
setFontHeightInPoints
((
short
)
10
);
contentWriteCellStyle
.
setWriteFont
(
contentWriteFont
);
//设置自动换行
contentWriteCellStyle
.
setWrapped
(
true
);
//设置垂直居中
contentWriteCellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
// 头默认了 FillPatternType所以可以不指定。
// contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
//设置水平居中
contentWriteCellStyle
.
setHorizontalAlignment
(
HorizontalAlignment
.
CENTER
);
//设置边框样式
contentWriteCellStyle
.
setBorderLeft
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setBorderTop
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setBorderRight
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setBorderBottom
(
BorderStyle
.
THIN
);
return
new
HorizontalCellStyleStrategy
(
headWriteCellStyle
,
contentWriteCellStyle
);
}
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/ExplicitConstraint.java
View file @
383ba898
//
package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//
import java.lang.annotation.*;
/
//
**
//
* 导出模板数据
//
*/
//
@Documented
//
@Retention(RetentionPolicy.RUNTIME)
//
@Target({ElementType.FIELD})
//
public @interface ExplicitConstraint {
//
//定义固定下拉内容
//
String[]source()default {};
//
//定义动态下拉内容,
//
Class[]sourceClass()default {};
//
//列标号必须和字段下标一致
//
int indexNum() default 0;
//
字典type
//
String type() default "";
//
//
}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
java.lang.annotation.*
;
/**
* 导出模板数据
*/
@Documented
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Target
({
ElementType
.
FIELD
})
public
@interface
ExplicitConstraint
{
//定义固定下拉内容
String
[]
source
()
default
{};
//定义动态下拉内容,
Class
[]
sourceClass
()
default
{};
//列标号必须和字段下标一致
int
indexNum
()
default
0
;
//
字典type
String
type
()
default
""
;
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/ExplicitInterface.java
View file @
383ba898
//
package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//
import com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper;
//
//
public interface ExplicitInterface {
//
/**
//
* 动态下拉列表的内容数组
//
* @return
//
* type 字典类型
//
*/
//
String[] source(String type,DataDictionaryMapper dataDictionaryMapper);
//
}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper
;
public
interface
ExplicitInterface
{
/**
* 动态下拉列表的内容数组
* @return
* type 字典类型
*/
String
[]
source
(
String
type
,
DataDictionaryMapper
dataDictionaryMapper
);
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/RoleNameExplicitConstraint.java
View file @
383ba898
//
package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//
import java.util.List;
//
import java.util.stream.Collectors;
//
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//
import com.yeejoin.amos.boot.module.jcs.api.entity.DataDictionary;
//
import com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper;
//
/
//
***
//
*
//
* 获取动态值
//
*
//
* **/
//
public class RoleNameExplicitConstraint implements ExplicitInterface {
//
//
@Override
//
public String[] source(String type,DataDictionaryMapper dataDictionaryMapper) {
//
QueryWrapper<DataDictionary> queryWrapper = new QueryWrapper<>();
//
queryWrapper.eq("type", type);
//
queryWrapper.orderByAsc("sort_num");
//
List<DataDictionary> list= dataDictionaryMapper.selectList(queryWrapper);
//
List<String> names = list.stream().map(dataDictionary->{
//
return dataDictionary.getName()+"@"+dataDictionary.getCode();
//
}
//
).collect(Collectors.toList());
//
String[] str=names.toArray(new String[names.size()]);
//
return str;
//
}
//
}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.yeejoin.amos.boot.module.jcs.api.entity.DataDictionary
;
import
com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper
;
/***
*
* 获取动态值
*
* **/
public
class
RoleNameExplicitConstraint
implements
ExplicitInterface
{
@Override
public
String
[]
source
(
String
type
,
DataDictionaryMapper
dataDictionaryMapper
)
{
QueryWrapper
<
DataDictionary
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"type"
,
type
);
queryWrapper
.
orderByAsc
(
"sort_num"
);
List
<
DataDictionary
>
list
=
dataDictionaryMapper
.
selectList
(
queryWrapper
);
List
<
String
>
names
=
list
.
stream
().
map
(
dataDictionary
->{
return
dataDictionary
.
getName
()+
"@"
+
dataDictionary
.
getCode
();
}
).
collect
(
Collectors
.
toList
());
String
[]
str
=
names
.
toArray
(
new
String
[
names
.
size
()]);
return
str
;
}
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/TemplateCellWriteHandler.java
View file @
383ba898
//package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.CellStyle;
//import org.apache.poi.ss.usermodel.DataFormat;
//import org.apache.poi.ss.usermodel.FillPatternType;
//import org.apache.poi.ss.usermodel.Font;
//import org.apache.poi.ss.usermodel.HorizontalAlignment;
//import org.apache.poi.ss.usermodel.IndexedColors;
//import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.ss.usermodel.VerticalAlignment;
//import org.apache.poi.ss.usermodel.Workbook;
//import com.alibaba.excel.metadata.CellData;
//import com.alibaba.excel.metadata.Head;
//import com.alibaba.excel.write.handler.CellWriteHandler;
//import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
//import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
//
///**
// * excel通用单元格格式类
// */
//public class TemplateCellWriteHandler implements CellWriteHandler {
//
// @Override
// public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
// Head head, int relativeRowIndex, boolean isHead) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData,
// Cell cell, Head head, int relativeRowIndex, boolean isHead) {
// Workbook workbooks = writeSheetHolder.getSheet().getWorkbook();
// writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), 20 * 256);
// CellStyle cellStyle = workbooks.createCellStyle();
// cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//居中
// cellStyle.setAlignment(HorizontalAlignment.CENTER);
// cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置前景填充样式
// cellStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());//前景填充色
// Font font1 = workbooks.createFont();//设置字体
// font1.setBold(true);
// font1.setColor((short)1);
// font1.setFontHeightInPoints((short)15);
// cellStyle.setFont(font1);
// cell.setCellStyle(cellStyle);
// //其他列
// if (!isHead){
// CellStyle style = workbooks.createCellStyle();
// DataFormat dataFormat = workbooks.createDataFormat();
// style.setDataFormat(dataFormat.getFormat("@"));
// style.setVerticalAlignment(VerticalAlignment.CENTER);
// style.setAlignment(HorizontalAlignment.CENTER);
// cell.setCellStyle(style);
// }
// //设置日期
// if (!isHead && cell.getColumnIndex()==19 || !isHead && cell.getColumnIndex()==21|| !isHead && cell.getColumnIndex()==20){
// CellStyle style = workbooks.createCellStyle();
// DataFormat dataFormat = workbooks.createDataFormat();
// style.setDataFormat(dataFormat.getFormat("yyyy/mm/dd hh:mm:ss"));
// style.setVerticalAlignment(VerticalAlignment.CENTER);
// style.setAlignment(HorizontalAlignment.CENTER);
// cell.setCellStyle(style);
// }
// //设置金额
// if (!isHead && cell.getColumnIndex()==15 ||!isHead && cell.getColumnIndex()==16||!isHead && cell.getColumnIndex()==22 ||!isHead && cell.getColumnIndex()==24||!isHead && cell.getColumnIndex()==25){
// CellStyle style = workbooks.createCellStyle();
// DataFormat dataFormat = workbooks.createDataFormat();
// style.setDataFormat(dataFormat.getFormat("0.00"));
// style.setVerticalAlignment(VerticalAlignment.CENTER);
// style.setAlignment(HorizontalAlignment.CENTER);
// cell.setCellStyle(style);
// }
// }
//
//
//
//}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.CellStyle
;
import
org.apache.poi.ss.usermodel.DataFormat
;
import
org.apache.poi.ss.usermodel.FillPatternType
;
import
org.apache.poi.ss.usermodel.Font
;
import
org.apache.poi.ss.usermodel.HorizontalAlignment
;
import
org.apache.poi.ss.usermodel.IndexedColors
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.VerticalAlignment
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
com.alibaba.excel.metadata.CellData
;
import
com.alibaba.excel.metadata.Head
;
import
com.alibaba.excel.write.handler.CellWriteHandler
;
import
com.alibaba.excel.write.metadata.holder.WriteSheetHolder
;
import
com.alibaba.excel.write.metadata.holder.WriteTableHolder
;
/**
* excel通用单元格格式类
*/
public
class
TemplateCellWriteHandler
implements
CellWriteHandler
{
@Override
public
void
beforeCellCreate
(
WriteSheetHolder
writeSheetHolder
,
WriteTableHolder
writeTableHolder
,
Row
row
,
Head
head
,
int
relativeRowIndex
,
boolean
isHead
)
{
// TODO Auto-generated method stub
}
@Override
public
void
afterCellCreate
(
WriteSheetHolder
writeSheetHolder
,
WriteTableHolder
writeTableHolder
,
CellData
cellData
,
Cell
cell
,
Head
head
,
int
relativeRowIndex
,
boolean
isHead
)
{
Workbook
workbooks
=
writeSheetHolder
.
getSheet
().
getWorkbook
();
if
(
1
==
cell
.
getRowIndex
())
{
writeSheetHolder
.
getSheet
().
setColumnWidth
(
cell
.
getColumnIndex
(),
20
*
256
);
CellStyle
cellStyle
=
workbooks
.
createCellStyle
();
cellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
//居中
cellStyle
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cellStyle
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
//设置前景填充样式
cellStyle
.
setFillForegroundColor
(
IndexedColors
.
ROYAL_BLUE
.
getIndex
());
//前景填充色
Font
font1
=
workbooks
.
createFont
();
//设置字体
font1
.
setBold
(
true
);
font1
.
setColor
((
short
)
1
);
font1
.
setFontHeightInPoints
((
short
)
15
);
cellStyle
.
setFont
(
font1
);
cell
.
setCellStyle
(
cellStyle
);
//其他列
if
(!
isHead
){
CellStyle
style
=
workbooks
.
createCellStyle
();
DataFormat
dataFormat
=
workbooks
.
createDataFormat
();
style
.
setDataFormat
(
dataFormat
.
getFormat
(
"@"
));
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cell
.
setCellStyle
(
style
);
}
//设置日期
if
(!
isHead
&&
cell
.
getColumnIndex
()==
19
||
!
isHead
&&
cell
.
getColumnIndex
()==
21
||
!
isHead
&&
cell
.
getColumnIndex
()==
20
){
CellStyle
style
=
workbooks
.
createCellStyle
();
DataFormat
dataFormat
=
workbooks
.
createDataFormat
();
style
.
setDataFormat
(
dataFormat
.
getFormat
(
"yyyy/mm/dd hh:mm:ss"
));
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cell
.
setCellStyle
(
style
);
}
//设置金额
if
(!
isHead
&&
cell
.
getColumnIndex
()==
15
||!
isHead
&&
cell
.
getColumnIndex
()==
16
||!
isHead
&&
cell
.
getColumnIndex
()==
22
||!
isHead
&&
cell
.
getColumnIndex
()==
24
||!
isHead
&&
cell
.
getColumnIndex
()==
25
){
CellStyle
style
=
workbooks
.
createCellStyle
();
DataFormat
dataFormat
=
workbooks
.
createDataFormat
();
style
.
setDataFormat
(
dataFormat
.
getFormat
(
"0.00"
));
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cell
.
setCellStyle
(
style
);
}
}
}
}
amos-boot-module/amos-boot-module-api/amos-boot-module-jcs-api/src/main/java/com/yeejoin/amos/boot/module/jcs/api/excel/TemplateCellWriteHandlerDate.java
View file @
383ba898
//
package com.yeejoin.amos.boot.module.jcs.api.excel;
//
//
//
import org.apache.poi.ss.usermodel.DataValidationHelper;
//
import org.apache.poi.ss.usermodel.Name;
//
import org.apache.poi.ss.usermodel.Workbook;
//
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
//
import com.alibaba.excel.write.handler.SheetWriteHandler;
//
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
//
import org.apache.poi.ss.usermodel.DataValidation;
//
import org.apache.poi.ss.usermodel.DataValidationConstraint;
//
import org.apache.poi.ss.usermodel.Sheet;
//
import org.apache.poi.ss.util.CellRangeAddressList;
//
import java.util.HashMap;
//
import java.util.Map;
//
/
//
**
//
* excel通用单元格格式类下拉框赋值
//
*/
//
public class TemplateCellWriteHandlerDate implements SheetWriteHandler {
//
//
/**
//
* 构造器注入
//
*/
//
//
private Map<Integer, String[]> explicitListConstraintMap = new HashMap<>();
//
public TemplateCellWriteHandlerDate(Map<Integer, String[]> explicitListConstraintMap) {
//
this.explicitListConstraintMap = explicitListConstraintMap;
//
}
//
/**
//
* 避免生成的导入模板下拉值获取不到
//
*/
//
private static final Integer LIMIT_NUMBER = 50;
//
//
//
//
/**
//
* 返回excel列标A-Z-AA-ZZ
//
*
//
* @param num 列数
//
* @return java.lang.String
//
*/
//
private String getExcelLine(int num) {
//
String line = "";
//
int first = num / 26;
//
int second = num % 26;
//
if (first > 0) {
//
line = (char) ('A' + first - 1) + "";
//
}
//
line += (char) ('A' + second) + "";
//
return line;
//
}
//
//
@Override
//
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
//
// TODO Auto-generated method stub
//
//
}
//
//
@Override
//
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
//
//
if(explicitListConstraintMap!=null) {
//
// 这里可以对cell进行任何操作
//
Sheet sheet = writeSheetHolder.getSheet();
//
DataValidationHelper helper = sheet.getDataValidationHelper();
//
//
// k 为存在下拉数据集的单元格下表 v为下拉数据集
//
explicitListConstraintMap.forEach((k, v) -> {
//
// 设置下拉单元格的首行 末行 首列 末列
//
CellRangeAddressList rangeList = new CellRangeAddressList(1, 65536, k, k);
//
// 如果下拉值总数大于100,则使用一个新sheet存储,避免生成的导入模板下拉值获取不到
//
if (v.length > LIMIT_NUMBER) {
//
//定义sheet的名称
//
//1.创建一个隐藏的sheet 名称为 hidden + k
//
String sheetName = "hidden" + k;
//
Workbook workbook = writeWorkbookHolder.getWorkbook();
//
Sheet hiddenSheet = workbook.createSheet(sheetName);
//
for (int i = 0, length = v.length; i < length; i++) {
//
// 开始的行数i,列数k
//
hiddenSheet.createRow(i).createCell(k).setCellValue(v[i]);
//
}
//
Name category1Name = workbook.createName();
//
category1Name.setNameName(sheetName);
//
String excelLine = getExcelLine(k);
//
// =hidden!$H:$1:$H$50 sheet为hidden的 H1列开始H50行数据获取下拉数组
//
String refers = "=" + sheetName + "!$" + excelLine + "$1:$" + excelLine + "$" + (v.length + 1);
//
// 将刚才设置的sheet引用到你的下拉列表中
//
DataValidationConstraint constraint = helper.createFormulaListConstraint(refers);
//
DataValidation dataValidation = helper.createValidation(constraint, rangeList);
//
writeSheetHolder.getSheet().addValidationData(dataValidation);
//
// 设置存储下拉列值得sheet为隐藏
//
int hiddenIndex = workbook.getSheetIndex(sheetName);
//
if (!workbook.isSheetHidden(hiddenIndex)) {
//
workbook.setSheetHidden(hiddenIndex, true);
//
}
//
}
//
// 下拉列表约束数据
//
DataValidationConstraint constraint = helper.createExplicitListConstraint(v);
//
// 设置约束
//
DataValidation validation = helper.createValidation(constraint, rangeList);
//
// 阻止输入非下拉选项的值
//
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
//
validation.setShowErrorBox(true);
//
validation.setSuppressDropDownArrow(true);
//
validation.createErrorBox("提示", "此值与单元格定义格式不一致");
//
// validation.createPromptBox("填写说明:","填写内容只能为下拉数据集中的单位,其他单位将会导致无法入仓");
//
sheet.addValidationData(validation);
//
});
//
}
//
//
//
}
//
//
}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
api
.
excel
;
import
org.apache.poi.ss.usermodel.DataValidationHelper
;
import
org.apache.poi.ss.usermodel.Name
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
com.alibaba.excel.write.metadata.holder.WriteSheetHolder
;
import
com.alibaba.excel.write.handler.SheetWriteHandler
;
import
com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder
;
import
org.apache.poi.ss.usermodel.DataValidation
;
import
org.apache.poi.ss.usermodel.DataValidationConstraint
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.util.CellRangeAddressList
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* excel通用单元格格式类下拉框赋值
*/
public
class
TemplateCellWriteHandlerDate
implements
SheetWriteHandler
{
/**
* 构造器注入
*/
private
Map
<
Integer
,
String
[]>
explicitListConstraintMap
=
new
HashMap
<>();
public
TemplateCellWriteHandlerDate
(
Map
<
Integer
,
String
[]>
explicitListConstraintMap
)
{
this
.
explicitListConstraintMap
=
explicitListConstraintMap
;
}
/**
* 避免生成的导入模板下拉值获取不到
*/
private
static
final
Integer
LIMIT_NUMBER
=
50
;
/**
* 返回excel列标A-Z-AA-ZZ
*
* @param num 列数
* @return java.lang.String
*/
private
String
getExcelLine
(
int
num
)
{
String
line
=
""
;
int
first
=
num
/
26
;
int
second
=
num
%
26
;
if
(
first
>
0
)
{
line
=
(
char
)
(
'A'
+
first
-
1
)
+
""
;
}
line
+=
(
char
)
(
'A'
+
second
)
+
""
;
return
line
;
}
@Override
public
void
beforeSheetCreate
(
WriteWorkbookHolder
writeWorkbookHolder
,
WriteSheetHolder
writeSheetHolder
)
{
// TODO Auto-generated method stub
}
@Override
public
void
afterSheetCreate
(
WriteWorkbookHolder
writeWorkbookHolder
,
WriteSheetHolder
writeSheetHolder
)
{
if
(
explicitListConstraintMap
!=
null
)
{
// 这里可以对cell进行任何操作
Sheet
sheet
=
writeSheetHolder
.
getSheet
();
DataValidationHelper
helper
=
sheet
.
getDataValidationHelper
();
// k 为存在下拉数据集的单元格下表 v为下拉数据集
explicitListConstraintMap
.
forEach
((
k
,
v
)
->
{
// 设置下拉单元格的首行 末行 首列 末列
CellRangeAddressList
rangeList
=
new
CellRangeAddressList
(
1
,
65536
,
k
,
k
);
// 如果下拉值总数大于100,则使用一个新sheet存储,避免生成的导入模板下拉值获取不到
if
(
v
.
length
>
LIMIT_NUMBER
)
{
//定义sheet的名称
//1.创建一个隐藏的sheet 名称为 hidden + k
String
sheetName
=
"hidden"
+
k
;
Workbook
workbook
=
writeWorkbookHolder
.
getWorkbook
();
Sheet
hiddenSheet
=
workbook
.
createSheet
(
sheetName
);
for
(
int
i
=
0
,
length
=
v
.
length
;
i
<
length
;
i
++)
{
// 开始的行数i,列数k
hiddenSheet
.
createRow
(
i
).
createCell
(
k
).
setCellValue
(
v
[
i
]);
}
Name
category1Name
=
workbook
.
createName
();
category1Name
.
setNameName
(
sheetName
);
String
excelLine
=
getExcelLine
(
k
);
// =hidden!$H:$1:$H$50 sheet为hidden的 H1列开始H50行数据获取下拉数组
String
refers
=
"="
+
sheetName
+
"!$"
+
excelLine
+
"$1:$"
+
excelLine
+
"$"
+
(
v
.
length
+
1
);
// 将刚才设置的sheet引用到你的下拉列表中
DataValidationConstraint
constraint
=
helper
.
createFormulaListConstraint
(
refers
);
DataValidation
dataValidation
=
helper
.
createValidation
(
constraint
,
rangeList
);
writeSheetHolder
.
getSheet
().
addValidationData
(
dataValidation
);
// 设置存储下拉列值得sheet为隐藏
int
hiddenIndex
=
workbook
.
getSheetIndex
(
sheetName
);
if
(!
workbook
.
isSheetHidden
(
hiddenIndex
))
{
workbook
.
setSheetHidden
(
hiddenIndex
,
true
);
}
}
// 下拉列表约束数据
DataValidationConstraint
constraint
=
helper
.
createExplicitListConstraint
(
v
);
// 设置约束
DataValidation
validation
=
helper
.
createValidation
(
constraint
,
rangeList
);
// 阻止输入非下拉选项的值
validation
.
setErrorStyle
(
DataValidation
.
ErrorStyle
.
STOP
);
validation
.
setShowErrorBox
(
true
);
validation
.
setSuppressDropDownArrow
(
true
);
validation
.
createErrorBox
(
"提示"
,
"此值与单元格定义格式不一致"
);
// validation.createPromptBox("填写说明:","填写内容只能为下拉数据集中的单位,其他单位将会导致无法入仓");
sheet
.
addValidationData
(
validation
);
});
}
}
}
amos-boot-module/amos-boot-module-biz/amos-boot-module-jcs-biz/src/main/java/com/yeejoin/amos/boot/module/jcs/biz/controller/ExcelController.java
View file @
383ba898
//
package com.yeejoin.amos.boot.module.jcs.biz.controller;
//
import org.springframework.web.bind.annotation.RequestMapping;
//
import io.swagger.annotations.ApiOperation;
//
import io.swagger.annotations.Api;
//
import org.springframework.web.bind.annotation.RestController;
//
import org.springframework.web.multipart.MultipartFile;
//
//
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
//
//
import org.apache.commons.beanutils.BeanUtils;
//
import org.bouncycastle.crypto.RuntimeCryptoException;
//
import org.springframework.beans.factory.annotation.Autowired;
//
//
import java.lang.reflect.InvocationTargetException;
//
import java.util.ArrayList;
//
import java.util.List;
//
//
import javax.servlet.http.HttpServletResponse;
//
import org.springframework.web.bind.annotation.*;
//
//
import com.yeejoin.amos.boot.module.jcs.api.dto.ExcelDto;
//
import com.yeejoin.amos.boot.module.jcs.api.dto.FireChemicalDto;
//
import com.yeejoin.amos.boot.module.jcs.api.entity.FireChemical;
//
import com.yeejoin.amos.boot.module.jcs.api.excel.ExcelUtil;
//
import com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper;
//
import com.yeejoin.amos.boot.module.jcs.biz.service.impl.FireChemicalServiceImpl;
//
import com.yeejoin.amos.boot.module.jcs.biz.service.impl.FireStationServiceImpl;
//
//
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
//
import org.typroject.tyboot.core.foundation.enumeration.UserType;
//
import org.typroject.tyboot.core.foundation.utils.Bean;
//
//
/
//
**
//
* 导出导入
//
*
//
* @author system_generator
//
* @date 2021-06-29
//
*/
//
@RestController
//
@Api(tags = "导出")
//
@RequestMapping(value = "/excel")
//
public class ExcelController extends BaseController {
//
//
//
@Autowired
//
DataDictionaryMapper dataDictionaryMapper;
//
@Autowired
//
FireChemicalServiceImpl fireChemicalServiceImpl;
//
private final String packageUrl="com.yeejoin.amos.boot.module.jcs.api.dto.";
//
//
@TycloudOperation(needAuth = false,ApiLevel = UserType.AGENCY)
//
@ApiOperation(value = "下载模板")
//
@GetMapping("/template")
//
public void template(HttpServletResponse response,ExcelDto excelDto ) {
//
try {
//
String url=packageUrl+excelDto.getClassUrl();
//
Class clz = Class.forName(url);
//
ExcelUtil.createTemplate(response, excelDto.getFileName(), excelDto.getSheetName(), null,clz, dataDictionaryMapper,true);
//
} catch (Exception e) {
//
e.printStackTrace();
//
throw new RuntimeException("系统异常!");
//
}
//
}
//
//
@TycloudOperation(needAuth = false,ApiLevel = UserType.AGENCY)
//
@ApiOperation(value = "导出危险品")
//
@GetMapping("/getFireStationFile")
//
public void getFireStationFile(HttpServletResponse response,ExcelDto excelDto ) {
//
try {
//
List<FireChemicalDto> date= fireChemicalServiceImpl.queryForFireChemicalList();
//
ExcelUtil.createTemplate(response, excelDto.getFileName(), excelDto.getSheetName(), date,FireChemicalDto.class, dataDictionaryMapper,false);
//
} catch (Exception e) {
//
e.printStackTrace();
//
throw new RuntimeException("系统异常!");
//
}
//
}
//
@TycloudOperation(needAuth = false,ApiLevel = UserType.AGENCY)
//
@ApiOperation(value = "上传文件数据-")
//
@PostMapping("/upload")
//
public void template2(MultipartFile multipartFile, HttpServletResponse response) throws Exception {
//
//
//
List<FireChemicalDto> demoExcelEntities = ExcelUtil.readFirstSheetExcel(multipartFile, FireChemicalDto.class, 1);
//
List<FireChemical> demoExcelEntityErrorList = new ArrayList<>();
//
demoExcelEntities.forEach(
//
demoExcelEntity -> {
//
FireChemical demoExcelEntityError = new FireChemical();
//
demoExcelEntityError = Bean.toPo(demoExcelEntity, demoExcelEntityError);
//
if(demoExcelEntityError.getType()!=null) {
//
String[] dr= demoExcelEntityError.getType().split("@");
//
demoExcelEntityError.setType(dr[0]);
//
demoExcelEntityError.setTypeCode(dr[1]);
//
}
//
demoExcelEntityErrorList.add(demoExcelEntityError);
//
}
//
);
//
fireChemicalServiceImpl.saveOrUpdateBatch(demoExcelEntityErrorList);
//
}
//
//
}
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
biz
.
controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.Api
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.yeejoin.amos.boot.biz.common.controller.BaseController
;
import
org.apache.commons.beanutils.BeanUtils
;
import
org.bouncycastle.crypto.RuntimeCryptoException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
java.lang.reflect.InvocationTargetException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.web.bind.annotation.*
;
import
com.yeejoin.amos.boot.module.jcs.api.dto.ExcelDto
;
import
com.yeejoin.amos.boot.module.jcs.api.dto.FireChemicalDto
;
import
com.yeejoin.amos.boot.module.jcs.api.entity.FireChemical
;
import
com.yeejoin.amos.boot.module.jcs.api.excel.ExcelUtil
;
import
com.yeejoin.amos.boot.module.jcs.api.mapper.DataDictionaryMapper
;
import
com.yeejoin.amos.boot.module.jcs.biz.service.impl.FireChemicalServiceImpl
;
import
com.yeejoin.amos.boot.module.jcs.biz.service.impl.FireStationServiceImpl
;
import
org.typroject.tyboot.core.restful.doc.TycloudOperation
;
import
org.typroject.tyboot.core.foundation.enumeration.UserType
;
import
org.typroject.tyboot.core.foundation.utils.Bean
;
/**
* 导出导入
*
* @author system_generator
* @date 2021-06-29
*/
@RestController
@Api
(
tags
=
"导出"
)
@RequestMapping
(
value
=
"/excel"
)
public
class
ExcelController
extends
BaseController
{
@Autowired
DataDictionaryMapper
dataDictionaryMapper
;
@Autowired
FireChemicalServiceImpl
fireChemicalServiceImpl
;
private
final
String
packageUrl
=
"com.yeejoin.amos.boot.module.jcs.api.dto."
;
@TycloudOperation
(
needAuth
=
false
,
ApiLevel
=
UserType
.
AGENCY
)
@ApiOperation
(
value
=
"下载模板"
)
@GetMapping
(
"/template"
)
public
void
template
(
HttpServletResponse
response
,
ExcelDto
excelDto
)
{
try
{
String
url
=
packageUrl
+
excelDto
.
getClassUrl
();
Class
clz
=
Class
.
forName
(
url
);
ExcelUtil
.
createTemplate
(
response
,
excelDto
.
getFileName
(),
excelDto
.
getSheetName
(),
null
,
clz
,
dataDictionaryMapper
,
true
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
RuntimeException
(
"系统异常!"
);
}
}
@TycloudOperation
(
needAuth
=
false
,
ApiLevel
=
UserType
.
AGENCY
)
@ApiOperation
(
value
=
"导出危险品"
)
@GetMapping
(
"/getFireStationFile"
)
public
void
getFireStationFile
(
HttpServletResponse
response
,
ExcelDto
excelDto
)
{
try
{
List
<
FireChemicalDto
>
date
=
fireChemicalServiceImpl
.
queryForFireChemicalList
();
ExcelUtil
.
createTemplate
(
response
,
excelDto
.
getFileName
(),
excelDto
.
getSheetName
(),
date
,
FireChemicalDto
.
class
,
dataDictionaryMapper
,
false
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
RuntimeException
(
"系统异常!"
);
}
}
@TycloudOperation
(
needAuth
=
false
,
ApiLevel
=
UserType
.
AGENCY
)
@ApiOperation
(
value
=
"上传文件数据-"
)
@PostMapping
(
"/upload"
)
public
void
template2
(
MultipartFile
multipartFile
,
HttpServletResponse
response
)
throws
Exception
{
List
<
FireChemicalDto
>
demoExcelEntities
=
ExcelUtil
.
readFirstSheetExcel
(
multipartFile
,
FireChemicalDto
.
class
,
1
);
List
<
FireChemical
>
demoExcelEntityErrorList
=
new
ArrayList
<>();
demoExcelEntities
.
forEach
(
demoExcelEntity
->
{
FireChemical
demoExcelEntityError
=
new
FireChemical
();
demoExcelEntityError
=
Bean
.
toPo
(
demoExcelEntity
,
demoExcelEntityError
);
if
(
demoExcelEntityError
.
getType
()!=
null
)
{
String
[]
dr
=
demoExcelEntityError
.
getType
().
split
(
"@"
);
demoExcelEntityError
.
setType
(
dr
[
0
]);
demoExcelEntityError
.
setTypeCode
(
dr
[
1
]);
}
demoExcelEntityErrorList
.
add
(
demoExcelEntityError
);
}
);
fireChemicalServiceImpl
.
saveOrUpdateBatch
(
demoExcelEntityErrorList
);
}
}
pom.xml
View file @
383ba898
...
...
@@ -53,11 +53,19 @@
<version>3.1.2</version>
</dependency> -->
<!--
<dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
easyexcel
</artifactId>
<version>
2.0.5
</version>
</dependency> -->
<exclusions>
<exclusion>
<groupId>
org.ow2.asm
</groupId>
<artifactId>
asm
</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加commons-lang依赖包 -->
<dependency>
...
...
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