Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
shop-Mall
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
licc
shop-Mall
Commits
208f9517
Commit
208f9517
authored
Apr 27, 2021
by
licc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
二维码导出
parent
c5bd5c0e
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
991 additions
and
875 deletions
+991
-875
pom.xml
wisenergy-common/pom.xml
+15
-15
ExcelUtils.java
...n/src/main/java/cn/wisenergy/common/utils/ExcelUtils.java
+276
-276
ExcelView.java
.../cn/wisenergy/common/utils/exportView/view/ExcelView.java
+562
-563
AntiFakeMapper.xml
...nergy-mapper/src/main/resources/mapper/AntiFakeMapper.xml
+6
-5
AntiFake.java
...-model/src/main/java/cn/wisenergy/model/app/AntiFake.java
+2
-0
AntiFakeService.java
...c/main/java/cn/wisenergy/service/app/AntiFakeService.java
+2
-0
AntiFakeServiceImpl.java
...va/cn/wisenergy/service/app/impl/AntiFakeServiceImpl.java
+35
-11
UserLevelTaskServiceImpl.java
.../wisenergy/service/app/impl/UserLevelTaskServiceImpl.java
+0
-3
ExcelUtils.java
...e/src/main/java/cn/wisenergy/service/util/ExcelUtils.java
+79
-0
AntiFakeController.java
...isenergy/web/admin/controller/app/AntiFakeController.java
+14
-2
No files found.
wisenergy-common/pom.xml
View file @
208f9517
...
@@ -140,21 +140,21 @@
...
@@ -140,21 +140,21 @@
<artifactId>
shiro-spring
</artifactId>
<artifactId>
shiro-spring
</artifactId>
</dependency>
</dependency>
<!-- POI -->
<!-- POI -->
<dependency
>
<!-- <dependency>--
>
<groupId>
org.apache.poi
</groupId
>
<!-- <groupId>org.apache.poi</groupId>--
>
<artifactId>
poi
</artifactId
>
<!-- <artifactId>poi</artifactId>--
>
<version>
3.9
</version
>
<!-- <version>3.9</version>--
>
</dependency
>
<!-- </dependency>--
>
<dependency
>
<!-- <dependency>--
>
<groupId>
org.apache.poi
</groupId
>
<!-- <groupId>org.apache.poi</groupId>--
>
<artifactId>
poi-ooxml
</artifactId
>
<!-- <artifactId>poi-ooxml</artifactId>--
>
<version>
3.9
</version
>
<!-- <version>3.9</version>--
>
</dependency
>
<!-- </dependency>--
>
<dependency
>
<!-- <dependency>--
>
<groupId>
org.apache.poi
</groupId
>
<!-- <groupId>org.apache.poi</groupId>--
>
<artifactId>
poi-ooxml-schemas
</artifactId
>
<!-- <artifactId>poi-ooxml-schemas</artifactId>--
>
<version>
3.9
</version
>
<!-- <version>3.9</version>--
>
</dependency
>
<!-- </dependency>--
>
<dependency>
<dependency>
<groupId>
cn.hutool
</groupId>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<artifactId>
hutool-all
</artifactId>
...
...
wisenergy-common/src/main/java/cn/wisenergy/common/utils/ExcelUtils.java
View file @
208f9517
package
cn
.
wisenergy
.
common
.
utils
;
//
package cn.wisenergy.common.utils;
//
//
import
org.apache.commons.lang3.StringUtils
;
//
import org.apache.commons.lang3.StringUtils;
import
org.apache.poi.hssf.usermodel.HSSFDataFormat
;
//
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil
;
//
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
//
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import
org.apache.poi.ss.usermodel.*
;
//
import org.apache.poi.ss.usermodel.*;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
//
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import
org.apache.xmlbeans.impl.piccolo.io.FileFormatException
;
//
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import
org.springframework.web.multipart.MultipartFile
;
//
import org.springframework.web.multipart.MultipartFile;
//
import
java.io.FileInputStream
;
//
import java.io.FileInputStream;
import
java.io.FileNotFoundException
;
//
import java.io.FileNotFoundException;
import
java.io.IOException
;
//
import java.io.IOException;
import
java.io.InputStream
;
//
import java.io.InputStream;
import
java.text.DecimalFormat
;
//
import java.text.DecimalFormat;
import
java.text.SimpleDateFormat
;
//
import java.text.SimpleDateFormat;
import
java.util.ArrayList
;
//
import java.util.ArrayList;
import
java.util.Date
;
//
import java.util.Date;
import
java.util.List
;
//
import java.util.List;
//
public
class
ExcelUtils
{
//
public class ExcelUtils {
//
//
private
static
final
String
EXTENSION_XLS
=
"xls"
;
//
private static final String EXTENSION_XLS = "xls";
private
static
final
String
EXTENSION_XLSX
=
"xlsx"
;
//
private static final String EXTENSION_XLSX = "xlsx";
//
//
/**
//
/**
* 判断EXCEL版本
//
* 判断EXCEL版本
*
//
*
* @param in
//
* @param in
* @param filename
//
* @param filename
* @return
//
* @return
* @throws IOException
//
* @throws IOException
*/
//
*/
public
static
Workbook
getWorkbook
(
InputStream
in
,
String
filename
)
throws
IOException
{
//
public static Workbook getWorkbook(InputStream in, String filename) throws IOException {
Workbook
wb
=
null
;
//
Workbook wb = null;
if
(
filename
.
endsWith
(
EXTENSION_XLS
))
{
//
if (filename.endsWith(EXTENSION_XLS)) {
wb
=
new
HSSFWorkbook
(
in
);
//Excel 2003
//
wb = new HSSFWorkbook(in);//Excel 2003
}
else
if
(
filename
.
endsWith
(
EXTENSION_XLSX
))
{
//
} else if (filename.endsWith(EXTENSION_XLSX)) {
wb
=
new
XSSFWorkbook
(
in
);
//Excel 2007
//
wb = new XSSFWorkbook(in);//Excel 2007
}
//
}
return
wb
;
//
return wb;
}
//
}
//
//
/**
//
/**
* 文件校验是否是excel
//
* 文件校验是否是excel
*
//
*
* @param filePath
//
* @param filePath
* @throws FileNotFoundException
//
* @throws FileNotFoundException
* @throws FileFormatException
//
* @throws FileFormatException
*/
//
*/
public
static
void
preReadCheck
(
String
fileName
)
throws
FileNotFoundException
,
//
public static void preReadCheck(String fileName) throws FileNotFoundException,
FileFormatException
{
//
FileFormatException {
// 常规检查
//
// 常规检查
if
(
StringUtils
.
isBlank
(
fileName
))
{
//
if (StringUtils.isBlank(fileName)) {
throw
new
FileNotFoundException
(
"传入的文件不存在:"
+
fileName
);
//
throw new FileNotFoundException("传入的文件不存在:" + fileName);
}
//
}
//
if
(!
fileName
.
endsWith
(
EXTENSION_XLS
)
&&
!
fileName
.
endsWith
(
EXTENSION_XLSX
))
{
//
if (!fileName.endsWith(EXTENSION_XLS) && !fileName.endsWith(EXTENSION_XLSX)) {
throw
new
FileFormatException
(
"传入的文件不是excel"
);
//
throw new FileFormatException("传入的文件不是excel");
}
//
}
}
//
}
//
//
/**
//
/**
* 读取EXCEL
//
* 读取EXCEL
*
//
*
* @param filePath
//
* @param filePath
* @throws FileNotFoundException
//
* @throws FileNotFoundException
* @throws FileFormatException
//
* @throws FileFormatException
*/
//
*/
public
static
List
<
List
<
String
>>
readExcel
(
MultipartFile
file
)
throws
FileNotFoundException
,
FileFormatException
{
//
public static List<List<String>> readExcel(MultipartFile file) throws FileNotFoundException, FileFormatException {
// 检查
//
// 检查
preReadCheck
(
file
.
getOriginalFilename
());
//
preReadCheck(file.getOriginalFilename());
// 获取workbook对象
//
// 获取workbook对象
Workbook
workbook
=
null
;
//
Workbook workbook = null;
/*InputStream is = new FileInputStream(filePath);*/
//
/*InputStream is = new FileInputStream(filePath);*/
List
<
List
<
String
>>
result
=
new
ArrayList
<
List
<
String
>>();
//
List<List<String>> result = new ArrayList<List<String>>();
try
{
//
try {
workbook
=
getWorkbook
(
file
.
getInputStream
(),
file
.
getOriginalFilename
());
//
workbook = getWorkbook(file.getInputStream(), file.getOriginalFilename());
// workbook = WorkbookFactory.create(is);
//
// workbook = WorkbookFactory.create(is);
//
int
sheetCount
=
workbook
.
getNumberOfSheets
();
//Sheet的数量
//
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
// 读文件 一个sheet一个sheet地读取
//
// 读文件 一个sheet一个sheet地读取
for
(
int
numSheet
=
0
;
numSheet
<
sheetCount
;
numSheet
++)
{
//
for (int numSheet = 0; numSheet < sheetCount; numSheet++) {
Sheet
sheet
=
workbook
.
getSheetAt
(
numSheet
);
//
Sheet sheet = workbook.getSheetAt(numSheet);
if
(
sheet
==
null
)
{
//
if (sheet == null) {
continue
;
//
continue;
}
//
}
//
int
firstRowIndex
=
sheet
.
getFirstRowNum
();
//
int firstRowIndex = sheet.getFirstRowNum();
int
lastRowIndex
=
sheet
.
getLastRowNum
();
//
int lastRowIndex = sheet.getLastRowNum();
//
if
(
firstRowIndex
!=
lastRowIndex
&&
lastRowIndex
!=
0
)
{
//
if (firstRowIndex != lastRowIndex && lastRowIndex != 0) {
//
// 读取数据行
//
// 读取数据行
for
(
int
rowIndex
=
firstRowIndex
+
1
;
rowIndex
<=
lastRowIndex
;
rowIndex
++)
{
//
for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {
Row
currentRow
=
sheet
.
getRow
(
rowIndex
);
// 当前行
//
Row currentRow = sheet.getRow(rowIndex);// 当前行
int
firstColumnIndex
=
currentRow
.
getFirstCellNum
();
// 首列
//
int firstColumnIndex = currentRow.getFirstCellNum(); // 首列
int
lastColumnIndex
=
currentRow
.
getLastCellNum
();
// 最后一列
//
int lastColumnIndex = currentRow.getLastCellNum();// 最后一列
List
<
String
>
rowList
=
new
ArrayList
<
String
>();
//
List<String> rowList = new ArrayList<String>();
for
(
int
columnIndex
=
firstColumnIndex
;
columnIndex
<
lastColumnIndex
;
columnIndex
++)
{
//
for (int columnIndex = firstColumnIndex; columnIndex < lastColumnIndex; columnIndex++) {
Cell
currentCell
=
currentRow
.
getCell
(
columnIndex
);
// 当前单元格
//
Cell currentCell = currentRow.getCell(columnIndex);// 当前单元格
//
String
currentCellValue
=
getCellValue
(
currentCell
,
true
);
// 当前单元格的值
//
String currentCellValue = getCellValue(currentCell, true);// 当前单元格的值
rowList
.
add
(
currentCellValue
);
//
rowList.add(currentCellValue);
}
//
}
//
//行为空的不读
//
//行为空的不读
Boolean
flag
=
false
;
//
Boolean flag = false;
for
(
String
str
:
rowList
)
{
//
for (String str : rowList) {
if
(!
StringUtils
.
isBlank
(
str
))
{
//
if (!StringUtils.isBlank(str)) {
flag
=
true
;
//
flag = true;
break
;
//
break;
}
//
}
}
//
}
if
(
flag
)
{
//
if (flag) {
result
.
add
(
rowList
);
//
result.add(rowList);
}
//
}
}
//
}
}
//
}
//
}
//
}
}
catch
(
Exception
e
)
{
//
} catch (Exception e) {
e
.
printStackTrace
();
//
e.printStackTrace();
}
finally
{
//
} finally {
}
//
}
return
result
;
//
return result;
}
//
}
//
public
static
List
<
List
<
String
>>
readInlandCompare
(
String
filePath
,
Integer
startSheet
,
Integer
endSheet
)
throws
FileNotFoundException
,
FileFormatException
{
//
public static List<List<String>> readInlandCompare(String filePath, Integer startSheet, Integer endSheet) throws FileNotFoundException, FileFormatException {
// 检查
//
// 检查
preReadCheck
(
filePath
);
//
preReadCheck(filePath);
// 获取workbook对象
//
// 获取workbook对象
Workbook
workbook
=
null
;
//
Workbook workbook = null;
InputStream
is
=
new
FileInputStream
(
filePath
);
//
InputStream is = new FileInputStream(filePath);
List
<
List
<
String
>>
result
=
new
ArrayList
<
List
<
String
>>();
//
List<List<String>> result = new ArrayList<List<String>>();
try
{
//
try {
workbook
=
getWorkbook
(
is
,
filePath
);
//
workbook = getWorkbook(is, filePath);
// workbook = WorkbookFactory.create(is);
//
// workbook = WorkbookFactory.create(is);
//
int
sheetCount
=
workbook
.
getNumberOfSheets
();
//Sheet的数量
//
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
// 读文件 一个sheet一个sheet地读取
//
// 读文件 一个sheet一个sheet地读取
for
(
int
numSheet
=
startSheet
;
numSheet
<
endSheet
;
numSheet
++)
{
//
for (int numSheet = startSheet; numSheet < endSheet; numSheet++) {
Sheet
sheet
=
workbook
.
getSheetAt
(
numSheet
);
//
Sheet sheet = workbook.getSheetAt(numSheet);
if
(
sheet
==
null
)
{
//
if (sheet == null) {
continue
;
//
continue;
}
//
}
//
int
firstRowIndex
=
sheet
.
getFirstRowNum
();
//
int firstRowIndex = sheet.getFirstRowNum();
int
lastRowIndex
=
sheet
.
getLastRowNum
();
//
int lastRowIndex = sheet.getLastRowNum();
//
if
(
firstRowIndex
!=
lastRowIndex
&&
lastRowIndex
!=
0
)
{
//
if (firstRowIndex != lastRowIndex && lastRowIndex != 0) {
//
// 读取数据行
//
// 读取数据行
for
(
int
rowIndex
=
firstRowIndex
+
1
;
rowIndex
<=
lastRowIndex
;
rowIndex
++)
{
//
for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {
Row
currentRow
=
sheet
.
getRow
(
rowIndex
);
// 当前行
//
Row currentRow = sheet.getRow(rowIndex);// 当前行
int
firstColumnIndex
=
currentRow
.
getFirstCellNum
();
// 首列
//
int firstColumnIndex = currentRow.getFirstCellNum(); // 首列
int
lastColumnIndex
=
currentRow
.
getLastCellNum
();
// 最后一列
//
int lastColumnIndex = currentRow.getLastCellNum();// 最后一列
List
<
String
>
rowList
=
new
ArrayList
<
String
>();
//
List<String> rowList = new ArrayList<String>();
for
(
int
columnIndex
=
firstColumnIndex
;
columnIndex
<
lastColumnIndex
;
columnIndex
++)
{
//
for (int columnIndex = firstColumnIndex; columnIndex < lastColumnIndex; columnIndex++) {
Cell
currentCell
=
currentRow
.
getCell
(
columnIndex
);
// 当前单元格
//
Cell currentCell = currentRow.getCell(columnIndex);// 当前单元格
//
String
currentCellValue
=
getCellValue
(
currentCell
,
true
);
// 当前单元格的值
//
String currentCellValue = getCellValue(currentCell, true);// 当前单元格的值
rowList
.
add
(
currentCellValue
);
//
rowList.add(currentCellValue);
}
//
}
//行为空的不读
//
//行为空的不读
Boolean
flag
=
false
;
//
Boolean flag = false;
for
(
String
str
:
rowList
)
{
//
for (String str : rowList) {
if
(!
StringUtils
.
isBlank
(
str
))
{
//
if (!StringUtils.isBlank(str)) {
flag
=
true
;
//
flag = true;
break
;
//
break;
}
//
}
}
//
}
if
(
flag
)
{
//
if (flag) {
result
.
add
(
rowList
);
//
result.add(rowList);
}
//
}
}
//
}
}
//
}
//
}
//
}
}
catch
(
Exception
e
)
{
//
} catch (Exception e) {
e
.
printStackTrace
();
//
e.printStackTrace();
}
finally
{
//
} finally {
}
//
}
return
result
;
//
return result;
}
//
}
//
/**
//
/**
* 取单元格的值
//
* 取单元格的值
*
//
*
* @param cell 单元格对象
//
* @param cell 单元格对象
* @param treatAsStr 为true时,当做文本来取值 (取到的是文本,不会把“1”取成“1.0”)
//
* @param treatAsStr 为true时,当做文本来取值 (取到的是文本,不会把“1”取成“1.0”)
* @return
//
* @return
*/
//
*/
public
static
String
getCellValue
(
Cell
cell
,
boolean
treatAsStr
)
{
//
public static String getCellValue(Cell cell, boolean treatAsStr) {
if
(
cell
==
null
)
{
//
if (cell == null) {
return
""
;
//
return "";
}
//
}
//
/* if (treatAsStr) {
//
/* if (treatAsStr) {
// 虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0”
//
// 虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0”
// 加上下面这句,临时把它当做文本来读取
//
// 加上下面这句,临时把它当做文本来读取
cell.setCellType(Cell.CELL_TYPE_STRING);
//
cell.setCellType(Cell.CELL_TYPE_STRING);
}*/
//
}*/
//SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
//
//SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
//
String
cellValue
=
null
;
//
String cellValue = null;
int
cellType
=
cell
.
getCellType
();
//
int cellType = cell.getCellType();
switch
(
cellType
)
{
//
switch (cellType) {
case
Cell
.
CELL_TYPE_STRING
:
// 文本
//
case Cell.CELL_TYPE_STRING: // 文本
cellValue
=
cell
.
getStringCellValue
();
//
cellValue = cell.getStringCellValue();
break
;
//
break;
case
Cell
.
CELL_TYPE_NUMERIC
:
// 数字、日期
//
case Cell.CELL_TYPE_NUMERIC: // 数字、日期
if
(
HSSFDateUtil
.
isCellDateFormatted
(
cell
))
{
// 处理日期格式、时间格式
//
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat
sdf
=
null
;
//
SimpleDateFormat sdf = null;
if
(
cell
.
getCellStyle
().
getDataFormat
()
==
HSSFDataFormat
//
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.
getBuiltinFormat
(
"h:mm"
))
{
//
.getBuiltinFormat("h:mm")) {
sdf
=
new
SimpleDateFormat
(
"HH:mm:ss"
);
//
sdf = new SimpleDateFormat("HH:mm:ss");
}
else
{
// 日期
//
} else {// 日期
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
//
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
//
}
Date
date
=
cell
.
getDateCellValue
();
//
Date date = cell.getDateCellValue();
return
sdf
.
format
(
date
);
//
return sdf.format(date);
}
else
if
(
cell
.
getCellStyle
().
getDataFormat
()
==
58
)
{
//
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
//
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
//
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double
value
=
cell
.
getNumericCellValue
();
//
double value = cell.getNumericCellValue();
Date
date
=
org
.
apache
.
poi
.
ss
.
usermodel
.
DateUtil
//
Date date = org.apache.poi.ss.usermodel.DateUtil
.
getJavaDate
(
value
);
//
.getJavaDate(value);
return
sdf
.
format
(
date
);
//
return sdf.format(date);
}
else
{
//
} else {
double
value
=
cell
.
getNumericCellValue
();
//
double value = cell.getNumericCellValue();
CellStyle
style
=
cell
.
getCellStyle
();
//
CellStyle style = cell.getCellStyle();
DecimalFormat
format
=
new
DecimalFormat
();
//
DecimalFormat format = new DecimalFormat();
String
temp
=
style
.
getDataFormatString
();
//
String temp = style.getDataFormatString();
// 单元格设置成常规
//
// 单元格设置成常规
if
(
temp
.
equals
(
"General"
))
{
//
if (temp.equals("General")) {
format
.
applyPattern
(
"#"
);
//
format.applyPattern("#");
}
//
}
return
format
.
format
(
value
);
//
return format.format(value);
}
//
}
//
case
Cell
.
CELL_TYPE_BOOLEAN
:
// 布尔型
//
case Cell.CELL_TYPE_BOOLEAN: // 布尔型
cellValue
=
String
.
valueOf
(
cell
.
getBooleanCellValue
());
//
cellValue = String.valueOf(cell.getBooleanCellValue());
break
;
//
break;
case
Cell
.
CELL_TYPE_BLANK
:
// 空白
//
case Cell.CELL_TYPE_BLANK: // 空白
cellValue
=
cell
.
getStringCellValue
();
//
cellValue = cell.getStringCellValue();
break
;
//
break;
case
Cell
.
CELL_TYPE_ERROR
:
// 错误
//
case Cell.CELL_TYPE_ERROR: // 错误
cellValue
=
"错误"
;
//
cellValue = "错误";
break
;
//
break;
case
Cell
.
CELL_TYPE_FORMULA
:
// 公式
//
case Cell.CELL_TYPE_FORMULA: // 公式
/* try {
//
/* try {
cellValue = cell.getStringCellValue();
//
cellValue = cell.getStringCellValue();
} catch (IllegalStateException e) {
//
} catch (IllegalStateException e) {
cellValue = String.valueOf(cell.getNumericCellValue());
//
cellValue = String.valueOf(cell.getNumericCellValue());
}*/
//
}*/
try
{
//
try {
cellValue
=
String
.
valueOf
(
cell
.
getNumericCellValue
());
//
cellValue = String.valueOf(cell.getNumericCellValue());
}
catch
(
IllegalStateException
e
)
{
//
} catch (IllegalStateException e) {
cellValue
=
String
.
valueOf
(
cell
.
getRichStringCellValue
());
//
cellValue = String.valueOf(cell.getRichStringCellValue());
}
//
}
break
;
//
break;
default
:
//
default:
cellValue
=
"错误"
;
//
cellValue = "错误";
}
//
}
return
cellValue
;
//
return cellValue;
}
//
}
//
//
}
//
}
wisenergy-common/src/main/java/cn/wisenergy/common/utils/exportView/view/ExcelView.java
View file @
208f9517
package
cn
.
wisenergy
.
common
.
utils
.
exportView
.
view
;
//package cn.wisenergy.common.utils.exportView.view;
//
import
cn.wisenergy.common.utils.FileUtil
;
//import cn.wisenergy.common.utils.FileUtil;
import
cn.wisenergy.common.utils.exportView.convert.Convert
;
//import cn.wisenergy.common.utils.exportView.convert.Convert;
import
org.apache.commons.beanutils.BeanUtils
;
//import org.apache.commons.beanutils.BeanUtils;
import
org.apache.commons.collections.MapUtils
;
//import org.apache.commons.collections.MapUtils;
import
org.apache.commons.io.IOUtils
;
//import org.apache.commons.io.IOUtils;
import
org.apache.commons.lang3.StringUtils
;
//import org.apache.commons.lang3.StringUtils;
import
org.apache.commons.lang3.exception.ExceptionUtils
;
//import org.apache.commons.lang3.exception.ExceptionUtils;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle
;
//import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import
org.apache.poi.hssf.usermodel.HSSFFont
;
//import org.apache.poi.hssf.usermodel.HSSFFont;
import
org.apache.poi.hssf.util.HSSFColor
;
//import org.apache.poi.hssf.util.HSSFColor;
import
org.apache.poi.ss.usermodel.*
;
//import org.apache.poi.ss.usermodel.*;
import
org.apache.poi.xssf.usermodel.XSSFClientAnchor
;
//import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import
org.apache.poi.xssf.usermodel.XSSFRichTextString
;
//import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import
org.slf4j.Logger
;
//import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
//import org.slf4j.LoggerFactory;
import
org.springframework.util.Assert
;
//import org.springframework.util.Assert;
import
org.springframework.web.servlet.view.document.AbstractXlsxView
;
//import org.springframework.web.servlet.view.document.AbstractXlsxView;
//
import
javax.servlet.http.HttpServletRequest
;
//import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse
;
//import javax.servlet.http.HttpServletResponse;
import
java.io.InputStream
;
//import java.io.InputStream;
import
java.net.URLEncoder
;
//import java.net.URLEncoder;
import
java.util.Collection
;
//import java.util.Collection;
import
java.util.HashMap
;
//import java.util.HashMap;
import
java.util.Map
;
//import java.util.Map;
//
/**
///**
* excel视图
// * excel视图
*/
// */
public
class
ExcelView
extends
AbstractXlsxView
{
//public class ExcelView extends AbstractXlsxView {
//
/**
// /**
* 日志类
// * 日志类
*/
// */
private
Logger
log
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
// private Logger log = LoggerFactory.getLogger(this.getClass());
//
/**
// /**
* 文件名称
// * 文件名称
*/
// */
private
String
filename
;
// private String filename;
//
/**
// /**
* 表名称
// * 表名称
*/
// */
private
String
sheetName
;
// private String sheetName;
//
/**
// /**
* 属性
// * 属性
*/
// */
private
String
[]
properties
;
// private String[] properties;
//
/**
// /**
* 标题
// * 标题
*/
// */
private
String
[]
titles
;
// private String[] titles;
//
/**
// /**
* 图片属性集合
// * 图片属性集合
*/
// */
private
String
[]
pictures
;
// private String[] pictures;
//
/**
// /**
* 列宽
// * 列宽
*/
// */
private
Integer
[]
widths
;
// private Integer[] widths;
//
/**
// /**
* 表体高度
// * 表体高度
*/
// */
private
Float
bodyHeight
;
// private Float bodyHeight;
//
/**
// /**
* 数据
// * 数据
*/
// */
private
Collection
<?>
data
;
// private Collection<?> data;
//
/**
// /**
* 附加内容
// * 附加内容
*/
// */
private
String
[]
contents
;
// private String[] contents;
//
/**
// /**
* 数据转换map
// * 数据转换map
*/
// */
private
Map
<
String
,
Convert
>
convertMap
=
new
HashMap
<
String
,
Convert
>();
// private Map<String, Convert> convertMap = new HashMap<String, Convert>();
//
/**
// /**
* @param filename
// * @param filename
* 文件名称
// * 文件名称
* @param sheetName
// * @param sheetName
* 表名称
// * 表名称
* @param properties
// * @param properties
* 属性
// * 属性
* @param titles
// * @param titles
* 标题
// * 标题
* @param widths
// * @param widths
* 列宽
// * 列宽
* @param data
// * @param data
* 数据
// * 数据
* @param contents
// * @param contents
* 附加内容
// * 附加内容
* @param convertMap
// * @param convertMap
* 数据转化map
// * 数据转化map
*/
// */
public
ExcelView
(
String
filename
,
String
sheetName
,
String
[]
properties
,
// public ExcelView(String filename, String sheetName, String[] properties,
String
[]
titles
,
Integer
[]
widths
,
Collection
<?>
data
,
// String[] titles, Integer[] widths, Collection<?> data,
String
[]
contents
,
Map
<
String
,
Convert
>
convertMap
)
{
// String[] contents, Map<String, Convert> convertMap) {
this
.
filename
=
filename
;
// this.filename = filename;
this
.
sheetName
=
sheetName
;
// this.sheetName = sheetName;
this
.
properties
=
properties
;
// this.properties = properties;
this
.
titles
=
titles
;
// this.titles = titles;
this
.
widths
=
widths
;
// this.widths = widths;
this
.
data
=
data
;
// this.data = data;
this
.
contents
=
contents
;
// this.contents = contents;
this
.
convertMap
=
MapUtils
.
isEmpty
(
convertMap
)
?
this
.
convertMap
// this.convertMap = MapUtils.isEmpty(convertMap) ? this.convertMap
:
convertMap
;
// : convertMap;
}
// }
//
/**
// /**
* @param filename
// * @param filename
* 文件名称
// * 文件名称
* @param sheetName
// * @param sheetName
* 表名称
// * 表名称
* @param properties
// * @param properties
* 属性
// * 属性
* @param titles
// * @param titles
* 标题
// * 标题
* @param pictures
// * @param pictures
* 图片属性
// * 图片属性
* @param widths
// * @param widths
* 列宽
// * 列宽
* @param bodyHeight
// * @param bodyHeight
* 表体高度
// * 表体高度
* @param data
// * @param data
* 数据
// * 数据
* @param contents
// * @param contents
* 附加内容
// * 附加内容
* @param convertMap
// * @param convertMap
* 数据转化map
// * 数据转化map
*/
// */
public
ExcelView
(
String
filename
,
String
sheetName
,
String
[]
properties
,
// public ExcelView(String filename, String sheetName, String[] properties,
String
[]
titles
,
String
[]
pictures
,
Integer
[]
widths
,
// String[] titles, String[] pictures, Integer[] widths,
Float
bodyHeight
,
Collection
<?>
data
,
String
[]
contents
,
// Float bodyHeight, Collection<?> data, String[] contents,
Map
<
String
,
Convert
>
convertMap
)
{
// Map<String, Convert> convertMap) {
this
.
filename
=
filename
;
// this.filename = filename;
this
.
sheetName
=
sheetName
;
// this.sheetName = sheetName;
this
.
properties
=
properties
;
// this.properties = properties;
this
.
titles
=
titles
;
// this.titles = titles;
this
.
pictures
=
pictures
;
// this.pictures = pictures;
this
.
widths
=
widths
;
// this.widths = widths;
this
.
bodyHeight
=
bodyHeight
;
// this.bodyHeight = bodyHeight;
this
.
data
=
data
;
// this.data = data;
this
.
contents
=
contents
;
// this.contents = contents;
this
.
convertMap
=
MapUtils
.
isEmpty
(
convertMap
)
?
this
.
convertMap
// this.convertMap = MapUtils.isEmpty(convertMap) ? this.convertMap
:
convertMap
;
// : convertMap;
}
// }
//
/**
// /**
* @param properties
// * @param properties
* 属性
// * 属性
* @param titles
// * @param titles
* 标题
// * 标题
* @param data
// * @param data
* 数据
// * 数据
* @param contents
// * @param contents
* 附加内容
// * 附加内容
*/
// */
public
ExcelView
(
String
filename
,
String
[]
properties
,
String
[]
titles
,
// public ExcelView(String filename, String[] properties, String[] titles,
Collection
<?>
data
,
String
[]
contents
,
// Collection<?> data, String[] contents,
Map
<
String
,
Convert
>
convertMap
)
{
// Map<String, Convert> convertMap) {
this
.
filename
=
filename
;
// this.filename = filename;
this
.
properties
=
properties
;
// this.properties = properties;
this
.
titles
=
titles
;
// this.titles = titles;
this
.
data
=
data
;
// this.data = data;
this
.
contents
=
contents
;
// this.contents = contents;
this
.
convertMap
=
MapUtils
.
isEmpty
(
convertMap
)
?
this
.
convertMap
// this.convertMap = MapUtils.isEmpty(convertMap) ? this.convertMap
:
convertMap
;
// : convertMap;
}
// }
//
/**
// /**
* @param properties
// * @param properties
* 属性
// * 属性
* @param titles
// * @param titles
* 标题
// * 标题
* @param data
// * @param data
* 数据
// * 数据
*/
// */
public
ExcelView
(
String
filename
,
String
[]
properties
,
String
[]
titles
,
// public ExcelView(String filename, String[] properties, String[] titles,
Collection
<?>
data
,
Map
<
String
,
Convert
>
convertMap
)
{
// Collection<?> data, Map<String, Convert> convertMap) {
this
.
filename
=
filename
;
// this.filename = filename;
this
.
properties
=
properties
;
// this.properties = properties;
this
.
titles
=
titles
;
// this.titles = titles;
this
.
data
=
data
;
// this.data = data;
this
.
convertMap
=
MapUtils
.
isEmpty
(
convertMap
)
?
this
.
convertMap
// this.convertMap = MapUtils.isEmpty(convertMap) ? this.convertMap
:
convertMap
;
// : convertMap;
}
// }
//
/**
// /**
* @param properties
// * @param properties
* 属性
// * 属性
* @param data
// * @param data
* 数据
// * 数据
*/
// */
public
ExcelView
(
String
[]
properties
,
Collection
<?>
data
)
{
// public ExcelView(String[] properties, Collection<?> data) {
this
.
properties
=
properties
;
// this.properties = properties;
this
.
data
=
data
;
// this.data = data;
}
// }
//
@SuppressWarnings
(
"rawtypes"
)
// @SuppressWarnings("rawtypes")
@Override
// @Override
protected
void
buildExcelDocument
(
Map
<
String
,
Object
>
map
,
// protected void buildExcelDocument(Map<String, Object> map,
Workbook
workbook
,
HttpServletRequest
httpServletRequest
,
// Workbook workbook, HttpServletRequest httpServletRequest,
HttpServletResponse
httpServletResponse
)
throws
Exception
{
// HttpServletResponse httpServletResponse) throws Exception {
try
{
// try {
//
Assert
.
notEmpty
(
properties
,
"表格导出中属性为空"
);
// Assert.notEmpty(properties, "表格导出中属性为空");
Sheet
sheet
;
// Sheet sheet;
if
(
StringUtils
.
isNotEmpty
(
sheetName
))
{
// if (StringUtils.isNotEmpty(sheetName)) {
sheet
=
workbook
.
createSheet
(
sheetName
);
// sheet = workbook.createSheet(sheetName);
}
else
{
// } else {
sheet
=
workbook
.
createSheet
();
// sheet = workbook.createSheet();
}
// }
int
rowNumber
=
0
;
// int rowNumber = 0;
//
// 创建表头
// // 创建表头
if
(
titles
!=
null
&&
titles
.
length
>
0
)
{
// if (titles != null && titles.length > 0) {
// HSSFRow header = sheet.createRow(rowNumber);
// // HSSFRow header = sheet.createRow(rowNumber);
Row
header
=
sheet
.
createRow
(
rowNumber
);
// Row header = sheet.createRow(rowNumber);
header
.
setHeight
((
short
)
400
);
// header.setHeight((short) 400);
for
(
int
i
=
0
;
i
<
properties
.
length
;
i
++)
{
// for (int i = 0; i < properties.length; i++) {
Cell
cell
=
header
.
createCell
(
i
);
// Cell cell = header.createCell(i);
CellStyle
cellStyle
=
workbook
.
createCellStyle
();
// CellStyle cellStyle = workbook.createCellStyle();
cellStyle
// cellStyle
.
setFillForegroundColor
(
HSSFColor
.
LIGHT_CORNFLOWER_BLUE
.
index
);
// .setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
cellStyle
.
setFillPattern
(
HSSFCellStyle
.
SOLID_FOREGROUND
);
//// cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle
.
setAlignment
(
HSSFCellStyle
.
ALIGN_CENTER
);
//// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle
// //cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
.
setVerticalAlignment
(
HSSFCellStyle
.
VERTICAL_CENTER
);
// Font font = workbook.createFont();
Font
font
=
workbook
.
createFont
();
// font.setFontHeightInPoints((short) 11);
font
.
setFontHeightInPoints
((
short
)
11
);
// //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font
.
setBoldweight
(
HSSFFont
.
BOLDWEIGHT_BOLD
);
// cellStyle.setFont(font);
cellStyle
.
setFont
(
font
);
// cell.setCellStyle(cellStyle);
cell
.
setCellStyle
(
cellStyle
);
//
// if (i == 0) {
if
(
i
==
0
)
{
// Drawing patriarch = sheet.createDrawingPatriarch();
Drawing
patriarch
=
sheet
.
createDrawingPatriarch
();
// Comment comment = patriarch
Comment
comment
=
patriarch
// .createCellComment(new XSSFClientAnchor(0, 0,
.
createCellComment
(
new
XSSFClientAnchor
(
0
,
0
,
// 0, 0, (short) 1, 1, (short) 4, 4));
0
,
0
,
(
short
)
1
,
1
,
(
short
)
4
,
4
));
// comment.setString(new XSSFRichTextString("亿函科技公司版权所有"));
comment
.
setString
(
new
XSSFRichTextString
(
"亿函科技公司版权所有"
));
// cell.setCellComment(comment);
cell
.
setCellComment
(
comment
);
// }
}
// if (titles.length > i && titles[i] != null) {
if
(
titles
.
length
>
i
&&
titles
[
i
]
!=
null
)
{
// cell.setCellValue(titles[i]);
cell
.
setCellValue
(
titles
[
i
]);
// } else {
}
else
{
// cell.setCellValue(properties[i]);
cell
.
setCellValue
(
properties
[
i
]);
// }
}
// if (widths != null && widths.length > i
if
(
widths
!=
null
&&
widths
.
length
>
i
// && widths[i] != null) {
&&
widths
[
i
]
!=
null
)
{
// sheet.setColumnWidth(i, widths[i]);
sheet
.
setColumnWidth
(
i
,
widths
[
i
]);
// } else {
}
else
{
// sheet.autoSizeColumn(i);
sheet
.
autoSizeColumn
(
i
);
// }
}
// }
}
// rowNumber++;
rowNumber
++;
// }
}
//
// // 写入数据
// 写入数据
// if (data != null) {
if
(
data
!=
null
)
{
// String property;
String
property
;
// Convert convert;
Convert
convert
;
// Drawing drawing = sheet.createDrawingPatriarch();
Drawing
drawing
=
sheet
.
createDrawingPatriarch
();
// for (Object item : data) {
for
(
Object
item
:
data
)
{
// Row row = sheet.createRow(rowNumber);
Row
row
=
sheet
.
createRow
(
rowNumber
);
// // 设置Excel表格高度(除标题以外)
// 设置Excel表格高度(除标题以外)
// if (this.bodyHeight != null) {
if
(
this
.
bodyHeight
!=
null
)
{
// row.setHeightInPoints(this.bodyHeight);
row
.
setHeightInPoints
(
this
.
bodyHeight
);
// }
}
//
// for (int i = 0; i < properties.length; i++) {
for
(
int
i
=
0
;
i
<
properties
.
length
;
i
++)
{
// Cell cell = row.createCell(i);
Cell
cell
=
row
.
createCell
(
i
);
// CellStyle cs = workbook.createCellStyle();
CellStyle
cs
=
workbook
.
createCellStyle
();
// //cs.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
cs
.
setAlignment
(
CellStyle
.
ALIGN_CENTER
);
// 水平居中
// //cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 垂直居中
cs
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_CENTER
);
// 垂直居中
// cs.setWrapText(true); // 设置自动换行
cs
.
setWrapText
(
true
);
// 设置自动换行
// cell.setCellStyle(cs);
cell
.
setCellStyle
(
cs
);
//
// property = properties[i];
property
=
properties
[
i
];
// convert = convertMap.get(property);
convert
=
convertMap
.
get
(
property
);
//
// String value = null;
String
value
=
null
;
// if (item instanceof Map) {
if
(
item
instanceof
Map
)
{
// Object val = ((Map) item).get(property);
Object
val
=
((
Map
)
item
).
get
(
property
);
// value = val == null ? null : val.toString();
value
=
val
==
null
?
null
:
val
.
toString
();
// } else {
}
else
{
// value = BeanUtils.getProperty(item, property);
value
=
BeanUtils
.
getProperty
(
item
,
property
);
// }
}
// if (convert != null) {
if
(
convert
!=
null
)
{
// value = convert.convert(value);
value
=
convert
.
convert
(
value
);
// }
}
// cell.setCellValue(value);
cell
.
setCellValue
(
value
);
// if (convert == null) {
if
(
convert
==
null
)
{
// cell.setCellValue(BeanUtils.getProperty(item,
cell
.
setCellValue
(
BeanUtils
.
getProperty
(
item
,
// property));
property
));
// } else {
}
else
{
// cell.setCellValue(convert.convert(BeanUtils
cell
.
setCellValue
(
convert
.
convert
(
BeanUtils
// .getProperty(item, property)));
.
getProperty
(
item
,
property
)));
// }
}
//
// if (rowNumber == 0 || rowNumber == 1) {
if
(
rowNumber
==
0
||
rowNumber
==
1
)
{
// if (widths != null && widths.length > i
if
(
widths
!=
null
&&
widths
.
length
>
i
// && widths[i] != null) {
&&
widths
[
i
]
!=
null
)
{
// sheet.setColumnWidth(i, widths[i]);
sheet
.
setColumnWidth
(
i
,
widths
[
i
]);
// } else {
}
else
{
// sheet.autoSizeColumn(i);
sheet
.
autoSizeColumn
(
i
);
// }
}
// }
}
// }
}
//
// // 处理图片
// 处理图片
// if (pictures != null) {
if
(
pictures
!=
null
)
{
// for (int j = 0; j < pictures.length; j++) {
for
(
int
j
=
0
;
j
<
pictures
.
length
;
j
++)
{
// property = pictures[j];
property
=
pictures
[
j
];
// String value = null;
String
value
=
null
;
// if (item instanceof Map) {
if
(
item
instanceof
Map
)
{
// Object val = ((Map) item).get(property);
Object
val
=
((
Map
)
item
).
get
(
property
);
// value = val == null ? null : val.toString();
value
=
val
==
null
?
null
:
val
.
toString
();
// } else {
}
else
{
// value = BeanUtils.getProperty(item, property);
value
=
BeanUtils
.
getProperty
(
item
,
property
);
// }
}
// if (StringUtils.isNotBlank(value)) {
if
(
StringUtils
.
isNotBlank
(
value
))
{
//
// // 清空图片地址
// 清空图片地址
// row.getCell(properties.length - 1 + j)
row
.
getCell
(
properties
.
length
-
1
+
j
)
// .setCellValue("");
.
setCellValue
(
""
);
//
// // 将图片服务器上的图片转成byte数组
// 将图片服务器上的图片转成byte数组
// InputStream is = FileUtil
InputStream
is
=
FileUtil
// .remotePathToStream(value);
.
remotePathToStream
(
value
);
// if (is != null) {
if
(
is
!=
null
)
{
// byte[] data = IOUtils.toByteArray(is);
byte
[]
data
=
IOUtils
.
toByteArray
(
is
);
// // 写图片
// 写图片
// XSSFClientAnchor anchor = new XSSFClientAnchor(
XSSFClientAnchor
anchor
=
new
XSSFClientAnchor
(
// 0,
0
,
// 0,
0
,
// 0,
0
,
// 0,
0
,
// (short) (properties.length - 1 + j),
(
short
)
(
properties
.
length
-
1
+
j
),
// row.getRowNum(),
row
.
getRowNum
(),
// (short) (properties.length + j),
(
short
)
(
properties
.
length
+
j
),
// row.getRowNum() + 1);
row
.
getRowNum
()
+
1
);
// drawing.createPicture(
drawing
.
createPicture
(
// anchor,
anchor
,
// workbook.addPicture(
workbook
.
addPicture
(
// data,
data
,
// XSSFWorkbook.PICTURE_TYPE_JPEG));
XSSFWorkbook
.
PICTURE_TYPE_JPEG
));
// }
}
// }
}
// }
}
// }
}
//
// rowNumber++;
rowNumber
++;
// }
}
// }
}
//
// // 写入汇总信息
// 写入汇总信息
// if (contents != null && contents.length > 0) {
if
(
contents
!=
null
&&
contents
.
length
>
0
)
{
// rowNumber++;
rowNumber
++;
// for (String content : contents) {
for
(
String
content
:
contents
)
{
// Row row = sheet.createRow(rowNumber);
Row
row
=
sheet
.
createRow
(
rowNumber
);
// Cell cell = row.createCell(0);
Cell
cell
=
row
.
createCell
(
0
);
// CellStyle cellStyle = workbook.createCellStyle();
CellStyle
cellStyle
=
workbook
.
createCellStyle
();
// Font font = workbook.createFont();
Font
font
=
workbook
.
createFont
();
// font.setColor(HSSFColor.GREY_50_PERCENT.index);
font
.
setColor
(
HSSFColor
.
GREY_50_PERCENT
.
index
);
// cellStyle.setFont(font);
cellStyle
.
setFont
(
font
);
// cell.setCellStyle(cellStyle);
cell
.
setCellStyle
(
cellStyle
);
// cell.setCellValue(content);
cell
.
setCellValue
(
content
);
// rowNumber++;
rowNumber
++;
// }
}
// }
}
//
// httpServletResponse.setContentType("application/force-download");
httpServletResponse
.
setContentType
(
"application/force-download"
);
// if (StringUtils.isNotEmpty(filename)) {
if
(
StringUtils
.
isNotEmpty
(
filename
))
{
// httpServletResponse.setHeader(
httpServletResponse
.
setHeader
(
// "Content-disposition",
"Content-disposition"
,
// "attachment; filename="
"attachment; filename="
// + URLEncoder.encode(filename, "UTF-8"));
+
URLEncoder
.
encode
(
filename
,
"UTF-8"
));
// } else {
}
else
{
// httpServletResponse.setHeader("Content-disposition",
httpServletResponse
.
setHeader
(
"Content-disposition"
,
// "attachment");
"attachment"
);
// }
}
// } catch (Exception e) {
}
catch
(
Exception
e
)
{
// log.error(
log
.
error
(
// "\r\n ************ Excle导出工具类方法【buildExcelDocument】报错:{}",
"\r\n ************ Excle导出工具类方法【buildExcelDocument】报错:{}"
,
// ExceptionUtils.getStackTrace(e));
ExceptionUtils
.
getStackTrace
(
e
));
// }
}
// }
}
//
// /**
/**
// * 获取文件名称
* 获取文件名称
// *
*
// * @return 文件名称
* @return 文件名称
// */
*/
// public String getFileName() {
public
String
getFileName
()
{
// return filename;
return
filename
;
// }
}
//
// /**
/**
// * 设置文件名称
* 设置文件名称
// *
*
// * @param filename
* @param filename
// * 文件名称
* 文件名称
// */
*/
// public void setFileName(String filename) {
public
void
setFileName
(
String
filename
)
{
// this.filename = filename;
this
.
filename
=
filename
;
// }
}
//
// /**
/**
// * 获取表名称
* 获取表名称
// *
*
// * @return 表名称
* @return 表名称
// */
*/
// public String getSheetName() {
public
String
getSheetName
()
{
// return sheetName;
return
sheetName
;
// }
}
//
// /**
/**
// * 设置表名称
* 设置表名称
// *
*
// * @param sheetName
* @param sheetName
// * 表名称
* 表名称
// */
*/
// public void setSheetName(String sheetName) {
public
void
setSheetName
(
String
sheetName
)
{
// this.sheetName = sheetName;
this
.
sheetName
=
sheetName
;
// }
}
//
// /**
/**
// * 获取属性
* 获取属性
// *
*
// * @return 属性
* @return 属性
// */
*/
// public String[] getProperties() {
public
String
[]
getProperties
()
{
// return properties;
return
properties
;
// }
}
//
// /**
/**
// * 设置属性
* 设置属性
// *
*
// * @param properties
* @param properties
// * 属性
* 属性
// */
*/
// public void setProperties(String[] properties) {
public
void
setProperties
(
String
[]
properties
)
{
// this.properties = properties;
this
.
properties
=
properties
;
// }
}
//
// /**
/**
// * 获取标题
* 获取标题
// *
*
// * @return 标题
* @return 标题
// */
*/
// public String[] getTitles() {
public
String
[]
getTitles
()
{
// return titles;
return
titles
;
// }
}
//
// /**
/**
// * 设置标题
* 设置标题
// *
*
// * @param titles
* @param titles
// * 标题
* 标题
// */
*/
// public void setTitles(String[] titles) {
public
void
setTitles
(
String
[]
titles
)
{
// this.titles = titles;
this
.
titles
=
titles
;
// }
}
//
// /**
/**
// * 获取图片属性集合
* 获取图片属性集合
// */
*/
// public String[] getPictures() {
public
String
[]
getPictures
()
{
// return pictures;
return
pictures
;
// }
}
//
// /**
/**
// * 设置图片属性集合
* 设置图片属性集合
// */
*/
// public void setPictures(String[] pictures) {
public
void
setPictures
(
String
[]
pictures
)
{
// this.pictures = pictures;
this
.
pictures
=
pictures
;
// }
}
//
// /**
/**
// * 获取列宽
* 获取列宽
// *
*
// * @return 列宽
* @return 列宽
// */
*/
// public Integer[] getWidths() {
public
Integer
[]
getWidths
()
{
// return widths;
return
widths
;
// }
}
//
// /**
/**
// * 设置列宽
* 设置列宽
// *
*
// * @param widths
* @param widths
// * 列宽
* 列宽
// */
*/
// public void setWidths(Integer[] widths) {
public
void
setWidths
(
Integer
[]
widths
)
{
// this.widths = widths;
this
.
widths
=
widths
;
// }
}
//
// /**
/**
// * 获取表体高度
* 获取表体高度
// */
*/
// public Float getBodyHeight() {
public
Float
getBodyHeight
()
{
// return bodyHeight;
return
bodyHeight
;
// }
}
//
// /**
/**
// * 设置表体高度
* 设置表体高度
// */
*/
// public void setBodyHeight(Float bodyHeight) {
public
void
setBodyHeight
(
Float
bodyHeight
)
{
// this.bodyHeight = bodyHeight;
this
.
bodyHeight
=
bodyHeight
;
// }
}
//
// /**
/**
// * 获取数据
* 获取数据
// *
*
// * @return 数据
* @return 数据
// */
*/
// public Collection<?> getData() {
public
Collection
<?>
getData
()
{
// return data;
return
data
;
// }
}
//
// /**
/**
// * 设置数据
* 设置数据
// *
*
// * @param data
* @param data
// * 数据
* 数据
// */
*/
// public void setData(Collection<?> data) {
public
void
setData
(
Collection
<?>
data
)
{
// this.data = data;
this
.
data
=
data
;
// }
}
//
// /**
/**
// * 获取附加内容
* 获取附加内容
// *
*
// * @return 附加内容
* @return 附加内容
// */
*/
// public String[] getContents() {
public
String
[]
getContents
()
{
// return contents;
return
contents
;
// }
}
//
// /**
/**
// * 设置附加内容
* 设置附加内容
// *
*
// * @param contents
* @param contents
// * 附加内容
* 附加内容
// */
*/
// public void setContents(String[] contents) {
public
void
setContents
(
String
[]
contents
)
{
// this.contents = contents;
this
.
contents
=
contents
;
// }
}
//
//}
}
wisenergy-mapper/src/main/resources/mapper/AntiFakeMapper.xml
View file @
208f9517
...
@@ -118,7 +118,7 @@
...
@@ -118,7 +118,7 @@
</foreach>
</foreach>
</insert>
</insert>
<select
id=
"getByProductNo"
result
Type=
"cn.wisenergy.model.app.AntiFake
"
>
<select
id=
"getByProductNo"
result
Map=
"antiMap
"
>
select
select
<include
refid=
"cols_all"
/>
<include
refid=
"cols_all"
/>
from
from
...
@@ -137,7 +137,7 @@
...
@@ -137,7 +137,7 @@
</where>
</where>
</select>
</select>
<select
id=
"getList"
result
Type=
"cn.wisenergy.model.app.AntiFake
"
>
<select
id=
"getList"
result
Map=
"antiMap
"
>
select
select
<include
refid=
"cols_all"
/>
<include
refid=
"cols_all"
/>
from
from
...
@@ -149,8 +149,9 @@
...
@@ -149,8 +149,9 @@
</where>
</where>
</select>
</select>
<select
id=
"getProductNos"
resultType=
"cn.wisenergy.model.app.AntiFake"
>
<select
id=
"getProductNos"
resultMap=
"antiMap"
>
select product_no
select
<include
refid=
"cols_all"
/>
from
from
<include
refid=
"table"
/>
<include
refid=
"table"
/>
<where>
<where>
...
@@ -162,7 +163,7 @@
...
@@ -162,7 +163,7 @@
</where>
</where>
</select>
</select>
<select
id=
"getByShaValue"
result
Type=
"cn.wisenergy.model.app.AntiFake
"
>
<select
id=
"getByShaValue"
result
Map=
"antiMap
"
>
select
select
<include
refid=
"cols_all"
/>
<include
refid=
"cols_all"
/>
from
from
...
...
wisenergy-model/src/main/java/cn/wisenergy/model/app/AntiFake.java
View file @
208f9517
package
cn
.
wisenergy
.
model
.
app
;
package
cn
.
wisenergy
.
model
.
app
;
import
com.alibaba.excel.annotation.ExcelIgnoreUnannotated
;
import
com.alibaba.excel.annotation.ExcelIgnoreUnannotated
;
import
com.alibaba.excel.annotation.ExcelProperty
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.AllArgsConstructor
;
import
lombok.AllArgsConstructor
;
...
@@ -50,6 +51,7 @@ public class AntiFake implements Serializable {
...
@@ -50,6 +51,7 @@ public class AntiFake implements Serializable {
* 防伪码
* 防伪码
*/
*/
@ApiModelProperty
(
value
=
"防伪码"
,
name
=
"shaValue"
)
@ApiModelProperty
(
value
=
"防伪码"
,
name
=
"shaValue"
)
@ExcelProperty
(
value
=
"防伪码"
)
private
String
shaValue
;
private
String
shaValue
;
/**
/**
...
...
wisenergy-service/src/main/java/cn/wisenergy/service/app/AntiFakeService.java
View file @
208f9517
...
@@ -63,4 +63,6 @@ public interface AntiFakeService {
...
@@ -63,4 +63,6 @@ public interface AntiFakeService {
* @return
* @return
*/
*/
R
<
Boolean
>
report
(
String
str
,
HttpServletResponse
response
);
R
<
Boolean
>
report
(
String
str
,
HttpServletResponse
response
);
void
exportReceivable
(
String
str
,
HttpServletResponse
response
);
}
}
wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/AntiFakeServiceImpl.java
View file @
208f9517
...
@@ -13,6 +13,7 @@ import cn.wisenergy.model.vo.ReportCodeVo;
...
@@ -13,6 +13,7 @@ import cn.wisenergy.model.vo.ReportCodeVo;
import
cn.wisenergy.service.Manager.AntiFakeManger
;
import
cn.wisenergy.service.Manager.AntiFakeManger
;
import
cn.wisenergy.service.app.AntiFakeService
;
import
cn.wisenergy.service.app.AntiFakeService
;
import
cn.wisenergy.service.util.CodeUtils
;
import
cn.wisenergy.service.util.CodeUtils
;
import
cn.wisenergy.service.util.ExcelUtils
;
import
cn.wisenergy.service.util.QRCodeUtils
;
import
cn.wisenergy.service.util.QRCodeUtils
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.excel.EasyExcel
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
...
@@ -203,15 +204,18 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
...
@@ -203,15 +204,18 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
return
R
.
ok
(
false
);
return
R
.
ok
(
false
);
}
}
//获取防伪
二维码生产
码
//获取防伪码
List
<
AntiFake
>
result
=
antiFakeMapper
.
getProductNos
(
array
);
List
<
AntiFake
>
result
=
antiFakeMapper
.
getProductNos
(
array
);
if
(
CollectionUtils
.
isEmpty
(
result
))
{
if
(
CollectionUtils
.
isEmpty
(
result
))
{
return
R
.
ok
(
0
,
true
);
return
R
.
ok
(
0
,
true
);
}
}
List
<
ReportCodeVo
>
resultBo
=
new
ArrayList
<>();
for
(
AntiFake
anti
:
result
)
{
for
(
AntiFake
anti
:
result
)
{
String
url
=
REQUEST_URL
+
anti
.
getShaValue
();
String
url
=
REQUEST_URL
+
anti
.
getShaValue
();
anti
.
setShaValue
(
url
);
ReportCodeVo
reportCodeVo
=
new
ReportCodeVo
();
reportCodeVo
.
setShaValue
(
url
);
resultBo
.
add
(
reportCodeVo
);
}
}
//生成Excel
//生成Excel
...
@@ -220,21 +224,13 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
...
@@ -220,21 +224,13 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
response
.
setContentType
(
"application/vnd.ms-excel"
);
response
.
setContentType
(
"application/vnd.ms-excel"
);
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
setCharacterEncoding
(
"utf-8"
);
// 使用java8新特性的stream流去处理数据,把空的数据过滤掉
List
<
ReportCodeVo
>
resultBo
=
new
ArrayList
<>();
for
(
AntiFake
antiFake
:
result
)
{
ReportCodeVo
reportCodeVo
=
new
ReportCodeVo
();
reportCodeVo
.
setShaValue
(
antiFake
.
getShaValue
());
resultBo
.
add
(
reportCodeVo
);
}
//创建文件名称
//创建文件名称
long
lon
=
System
.
currentTimeMillis
();
long
lon
=
System
.
currentTimeMillis
();
response
.
setHeader
(
"Content-disposition"
,
"attachment;filename="
+
lon
+
".xlsx"
);
response
.
setHeader
(
"Content-disposition"
,
"attachment;filename="
+
lon
+
".xlsx"
);
// sheet名称
// sheet名称
EasyExcel
.
write
(
response
.
getOutputStream
(),
ReportCodeVo
.
class
).
sheet
(
Long
.
toString
(
lon
)).
doWrite
(
resultBo
);
EasyExcel
.
write
(
response
.
getOutputStream
(),
AntiFake
.
class
).
sheet
(
Long
.
toString
(
lon
)).
doWrite
(
resultBo
);
//修改二维码使用状态
//修改二维码使用状态
boolean
bool
=
antiFakeManger
.
updateAntiFake
(
array
);
boolean
bool
=
antiFakeManger
.
updateAntiFake
(
array
);
if
(!
bool
)
{
if
(!
bool
)
{
...
@@ -246,6 +242,34 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
...
@@ -246,6 +242,34 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
}
}
}
}
@Override
public
void
exportReceivable
(
String
str
,
HttpServletResponse
response
)
{
if
(
StringUtils
.
isBlank
(
str
)){
return
;
}
//把字符转化为数字
List
<
Long
>
array
=
StringUtil
.
strToLongArray
(
str
);
if
(
CollectionUtils
.
isEmpty
(
array
))
{
return
;
}
//获取防伪码
List
<
AntiFake
>
result
=
antiFakeMapper
.
getProductNos
(
array
);
if
(
CollectionUtils
.
isEmpty
(
result
))
{
return
;
}
List
<
ReportCodeVo
>
resultBo
=
new
ArrayList
<>();
for
(
AntiFake
anti
:
result
)
{
String
url
=
REQUEST_URL
+
anti
.
getShaValue
();
ReportCodeVo
reportCodeVo
=
new
ReportCodeVo
();
reportCodeVo
.
setShaValue
(
url
);
resultBo
.
add
(
reportCodeVo
);
}
ExcelUtils
.
export
(
response
,
resultBo
,
"aadfgh"
,
ReportCodeVo
.
class
);
}
/**
/**
* 分页处理方法
* 分页处理方法
*
*
...
...
wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/UserLevelTaskServiceImpl.java
View file @
208f9517
...
@@ -6,14 +6,11 @@ import cn.wisenergy.service.app.LastMonthUserInfoService;
...
@@ -6,14 +6,11 @@ import cn.wisenergy.service.app.LastMonthUserInfoService;
import
cn.wisenergy.service.app.MonthUserLevelService
;
import
cn.wisenergy.service.app.MonthUserLevelService
;
import
cn.wisenergy.service.app.UserLevelService
;
import
cn.wisenergy.service.app.UserLevelService
;
import
cn.wisenergy.service.app.UserLevelTaskService
;
import
cn.wisenergy.service.app.UserLevelTaskService
;
import
com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH
;
import
com.xxl.job.core.handler.annotation.XxlJob
;
import
com.xxl.job.core.handler.annotation.XxlJob
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.poi.xssf.model.IndexedUDFFinder
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
@Service
@Service
...
...
wisenergy-service/src/main/java/cn/wisenergy/service/util/ExcelUtils.java
0 → 100644
View file @
208f9517
package
cn
.
wisenergy
.
service
.
util
;
import
com.alibaba.excel.EasyExcel
;
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.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy
;
import
io.undertow.util.Headers
;
import
org.apache.poi.ss.usermodel.BorderStyle
;
import
org.apache.poi.ss.usermodel.HorizontalAlignment
;
import
org.apache.poi.ss.usermodel.VerticalAlignment
;
import
org.springframework.http.MediaType
;
import
javax.servlet.http.HttpServletResponse
;
import
java.net.URLEncoder
;
import
java.nio.charset.StandardCharsets
;
import
java.util.List
;
/**
* Excel工具类
*
* @author: ZHY
* @date: 2020-08-24 17:31
* @version:
**/
public
class
ExcelUtils
{
private
final
static
HorizontalCellStyleStrategy
HORIZONTAL_CELL_STYLE_STRATEGY
;
static
{
// 头的策略
WriteCellStyle
headWriteCellStyle
=
new
WriteCellStyle
();
WriteFont
headWriteFont
=
new
WriteFont
();
headWriteFont
.
setFontHeightInPoints
((
short
)
12
);
headWriteCellStyle
.
setWriteFont
(
headWriteFont
);
// 内容的策略
WriteCellStyle
contentWriteCellStyle
=
new
WriteCellStyle
();
contentWriteCellStyle
.
setBorderBottom
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setBorderLeft
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setBorderRight
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setBorderTop
(
BorderStyle
.
THIN
);
contentWriteCellStyle
.
setHorizontalAlignment
(
HorizontalAlignment
.
CENTER
);
contentWriteCellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
// 内容字体
WriteFont
contentWriteFont
=
new
WriteFont
();
// 字体大小
contentWriteFont
.
setFontHeightInPoints
((
short
)
11
);
contentWriteCellStyle
.
setWriteFont
(
contentWriteFont
);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HORIZONTAL_CELL_STYLE_STRATEGY
=
new
HorizontalCellStyleStrategy
(
headWriteCellStyle
,
contentWriteCellStyle
);
}
/**
* 导出
*
* @param response
* @param data 数据
* @param fileName 文件名
* @param t 导出对象
**/
public
static
<
T
>
void
export
(
HttpServletResponse
response
,
List
<
T
>
data
,
String
fileName
,
Class
<
T
>
t
)
{
try
{
response
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
);
response
.
setCharacterEncoding
(
StandardCharsets
.
UTF_8
.
name
());
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
response
.
setHeader
(
Headers
.
CONTENT_DISPOSITION_STRING
,
"attachment;filename="
+
URLEncoder
.
encode
(
fileName
,
"UTF-8"
)
+
".xlsx"
);
EasyExcel
.
write
(
response
.
getOutputStream
(),
t
)
.
registerWriteHandler
(
HORIZONTAL_CELL_STYLE_STRATEGY
)
.
registerWriteHandler
(
new
LongestMatchColumnWidthStyleStrategy
())
.
sheet
(
fileName
).
doWrite
(
data
);
}
catch
(
Exception
e
)
{
// 重置response
response
.
reset
();
throw
new
RuntimeException
(
"下载失败"
);
}
}
}
\ No newline at end of file
wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/AntiFakeController.java
View file @
208f9517
...
@@ -36,13 +36,13 @@ public class AntiFakeController {
...
@@ -36,13 +36,13 @@ public class AntiFakeController {
return
antiFakeService
.
createCode
(
codeVo
);
return
antiFakeService
.
createCode
(
codeVo
);
}
}
@ApiOperation
(
value
=
"扫描防伪二维码"
,
notes
=
"扫描防伪二维码"
,
httpMethod
=
"
POS
T"
)
@ApiOperation
(
value
=
"扫描防伪二维码"
,
notes
=
"扫描防伪二维码"
,
httpMethod
=
"
GE
T"
)
@ApiImplicitParams
({
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"shaValue"
,
value
=
"防伪码"
,
dataType
=
"String"
,
required
=
true
),
@ApiImplicitParam
(
name
=
"shaValue"
,
value
=
"防伪码"
,
dataType
=
"String"
,
required
=
true
),
@ApiImplicitParam
(
name
=
"securityCode"
,
value
=
"安全码"
,
dataType
=
"String"
,
required
=
true
)
@ApiImplicitParam
(
name
=
"securityCode"
,
value
=
"安全码"
,
dataType
=
"String"
,
required
=
true
)
})
})
@ApiImplicitParam
(
name
=
"shaValue"
,
value
=
"防伪码"
,
dataType
=
"String"
)
@ApiImplicitParam
(
name
=
"shaValue"
,
value
=
"防伪码"
,
dataType
=
"String"
)
@
Pos
tMapping
(
"/admin/scanCode"
)
@
Ge
tMapping
(
"/admin/scanCode"
)
public
R
<
String
>
scanCode
(
String
shaValue
,
String
securityCode
)
{
public
R
<
String
>
scanCode
(
String
shaValue
,
String
securityCode
)
{
log
.
info
(
"shop-mall[]AntiFakeController[]scanCode[]input.param.shaValue:"
+
shaValue
,
securityCode
);
log
.
info
(
"shop-mall[]AntiFakeController[]scanCode[]input.param.shaValue:"
+
shaValue
,
securityCode
);
if
(
StringUtils
.
isBlank
(
shaValue
))
{
if
(
StringUtils
.
isBlank
(
shaValue
))
{
...
@@ -77,4 +77,16 @@ public class AntiFakeController {
...
@@ -77,4 +77,16 @@ public class AntiFakeController {
return
antiFakeService
.
report
(
ids
,
response
);
return
antiFakeService
.
report
(
ids
,
response
);
}
}
// @ApiOperation(value = "导出二维码", notes = "获取防伪二维码分页列表", httpMethod = "GET")
// @ApiImplicitParam(name = "ids", value = " 二维码主键id :\"1,2,3,4,5,6,7,8,9,\"", dataType = "String")
// @GetMapping("admin/antiFakeService")
// public void antiFakeService(String ids, HttpServletResponse response) {
// log.info("shop-mall[]AntiFakeController[]antiFakeService[]input.param.ids:" + ids);
// if (StringUtils.isBlank(ids)) {
// return ;
// }
//
// antiFakeService.exportReceivable(ids, response);
// }
}
}
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