Browse Source

update

master
YUJT 3 years ago
parent
commit
c0f1c78ecd
  1. 2
      epdc-commons-tools/pom.xml
  2. 28
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/dto/ExcelSelectionDto.java
  3. 5
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/enums/UserSexEnum.java
  4. 24
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java
  5. 182
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java
  6. 102
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/FileUtils.java
  7. 56
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/StreamUtils.java

2
epdc-commons-tools/pom.xml

@ -17,7 +17,7 @@
<commons.lang.version>3.7</commons.lang.version>
<commons.fileupload.version>1.3.3</commons.fileupload.version>
<hutool.version>4.1.8</hutool.version>
<easypoi.version>3.1.0</easypoi.version>
<easypoi.version>4.3.0</easypoi.version>
<joda.time.version>2.9.9</joda.time.version>
<fastjson.version>1.2.59</fastjson.version>
<jsoup.version>1.11.3</jsoup.version>

28
epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/dto/ExcelSelectionDto.java

@ -0,0 +1,28 @@
package com.elink.esua.epdc.commons.tools.dto;
import lombok.Data;
/**
* @author songyunpeng
* @Description excel导出模板的下拉列表实体
* @create 2020-09-03
*/
@Data
public class ExcelSelectionDto {
/**
* sheet页索引 0开始
*/
private Integer sheetIndex;
/**
* 区域中第一个单元格的列号 (下标0开始)
*/
private Integer firstCol;
/**
* 区域中最后一个单元格的列号
*/
private Integer lastCol;
/**
* 下拉数据
*/
private String[] excelSelections;
}

5
epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/enums/UserSexEnum.java

@ -26,6 +26,11 @@ public enum UserSexEnum {
*/
FEMALE("0"),
/**
* 未知
*/
UNKNOWN_SEX("2"),
/**
*
*/

24
epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java

@ -280,4 +280,28 @@ public class RedisKeys {
public static String getWorkUserListKey(String partyFlag, String authenticatedFlag, Long userId,Integer pageIndex,Integer pageSize) {
return rootPrefix.concat("work:user:List:").concat(partyFlag + ":").concat(authenticatedFlag + ":").concat(pageIndex + ":").concat(pageSize + ":").concat(userId.toString());
}
/**
* 疫苗接种权限
*
* @param userId
* @return java.lang.String
* @author zhy
* @date 2021/6/3 17:19
*/
public static String getVaccinationAuthParamKey(String userId) {
return rootPrefix.concat("vaccination:auth:param:").concat(userId);
}
/**
* 疫苗接种权限
*
* @param userId
* @return java.lang.String
* @author zhy
* @date 2021/6/3 17:19
*/
public static String getVaccinationAuthKey(String userId) {
return rootPrefix.concat("vaccination:auth:role:").concat(userId);
}
}

182
epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java

@ -10,23 +10,33 @@ package com.elink.esua.epdc.commons.tools.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.export.ExcelBatchExportService;
import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import com.elink.esua.epdc.commons.tools.dto.ExcelSelectionDto;
import com.elink.esua.epdc.commons.tools.exception.RenException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -67,6 +77,46 @@ public class ExcelUtils {
out.close();
}
/**
* 导出大量数据(超百万行的数据建议用CSV导出)
*
* @param fileName 文件名
* @param queryParams 查询条件
* @param server 查询服务
* @param response 浏览器请求返回
* @param params Excel属性
* @param pojoClass Excel对象Class
* @return void
* @author work@yujt.net.cn
* @date 2021/6/17 13:55
*/
public static <T> void exportBigExcel(String fileName, Object queryParams, IExcelExportServer server,
HttpServletResponse response, ExportParams params, Class<T> pojoClass) throws IOException {
ExcelBatchExportService batchService = new ExcelBatchExportService();
batchService.init(params, pojoClass);
Workbook workbook = batchService.exportBigExcel(server, queryParams);
if (StringUtils.isBlank(fileName)) {
//当前日期
fileName = DateUtils.format(new Date());
}
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream);
response.setHeader("Content-Length", String.valueOf(byteArrayOutputStream.size()));
ServletOutputStream out = response.getOutputStream();
out.write(byteArrayOutputStream.toByteArray());
out.flush();
out.close();
}
/**
* Excel导出先sourceList转换成List<targetClass>再导出
*
@ -87,6 +137,92 @@ public class ExcelUtils {
exportExcel(response, fileName, targetList, targetClass);
}
/**
* Excel导出先sourceList转换成List<targetClass>再导出
*
* @param response response
* @param fileName 文件名
* @param sourceList 原数据List
* @param sheetNames sheet名字列表
*/
public static void exportExcelToTargetWithSheets(HttpServletResponse response, String fileName, List<String> sheetNames,
List<Class<?>> targetClassList, List<ExcelSelectionDto> excelSelectionDtos, Collection<?>... sourceList) throws Exception {
if (sheetNames.size() != targetClassList.size() && targetClassList.size() != sourceList.length) {
throw new RenException("参数传递出错");
}
// 将sheets使用得map进行包装
List<Map<String, Object>> sheetsList = new ArrayList<>();
for (int i = 0; i < sheetNames.size(); i++) {
Map<String, Object> deptDataMap = new HashMap<>(4);
ExportParams exportParams = new ExportParams();
exportParams.setSheetName(sheetNames.get(i));
deptDataMap.put("title", exportParams);
// 模版导出对应得实体类型
deptDataMap.put("entity", targetClassList.get(i));
// sheet中要填充得数据
deptDataMap.put("data", sourceList[i]);
sheetsList.add(deptDataMap);
}
exportExcelWithSheets(response, fileName, sheetsList, excelSelectionDtos);
}
/**
* 多sheetExcel导出
*
* @param response response
* @param fileName 文件名
* @param sheetsList 多sheet组装
*/
public static void exportExcelWithSheets(HttpServletResponse response, String fileName, List<Map<String, Object>> sheetsList, List<ExcelSelectionDto> excelSelectionDtos) throws IOException {
if (StringUtils.isBlank(fileName)) {
//当前日期
fileName = DateUtils.format(new Date());
}
Workbook workbook = null;
try {
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
} catch (Exception ex) {
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.XSSF);
}
//插入下拉列表
if (excelSelectionDtos != null && excelSelectionDtos.size() > 0) {
for (ExcelSelectionDto excelSelectionDto : excelSelectionDtos) {
selectList(workbook, excelSelectionDto.getSheetIndex(), excelSelectionDto.getFirstCol(), excelSelectionDto.getLastCol(), excelSelectionDto.getExcelSelections());
}
}
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}
/**
* firstRow 開始行號 根据此项目默认为2(下标0开始)
* lastRow 根据此项目默认为最大65535
* firstCol 区域中第一个单元格的列号 (下标0开始)
* lastCol 区域中最后一个单元格的列号
* strings 下拉内容
*/
public static void selectList(Workbook workbook, Integer sheetIndex, int firstCol, int lastCol, String[] strings) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
// 生成下拉列表
// 只对(x,x)单元格有效
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, firstCol, lastCol);
// 生成下拉框内容
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(strings);
HSSFDataValidation dataValidation = new HSSFDataValidation(cellRangeAddressList, dvConstraint);
// 对sheet页生效
sheet.addValidationData(dataValidation);
}
/**
* 获取单元格内容
* @param cell
@ -94,7 +230,7 @@ public class ExcelUtils {
*/
public static String getCellContent(Cell cell){
String value = "";
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (cell.getCellType() == CellType.NUMERIC) {
value = String.valueOf(cell.getNumericCellValue());
} else {
value = cell.getStringCellValue();
@ -201,30 +337,22 @@ public class ExcelUtils {
}
/**
* 删除文件夹强制删除
* 得到Workbook对象
*
* @param path
* @param file
* @return
* @throws IOException
*/
public static void deleteAllFilesOfDir(File path) {
if (null != path) {
if (!path.exists()) {
return;
}
if (path.isFile()) {
boolean result = path.delete();
int tryCount = 0;
while (!result && tryCount++ < 10) {
System.gc(); // 回收资源
result = path.delete();
}
}
File[] files = path.listFiles();
if (null != files) {
for (int i = 0; i < files.length; i++) {
deleteAllFilesOfDir(files[i]);
}
}
path.delete();
public static Workbook getWorkBook(MultipartFile file) throws IOException {
//这样写 excel 能兼容03和07
InputStream is = file.getInputStream();
Workbook hssfWorkbook = null;
try {
hssfWorkbook = new HSSFWorkbook(is);
} catch (Exception ex) {
is = file.getInputStream();
hssfWorkbook = new XSSFWorkbook(is);
}
return hssfWorkbook;
}
}

102
epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/FileUtils.java

@ -0,0 +1,102 @@
package com.elink.esua.epdc.commons.tools.utils;
import com.elink.esua.epdc.commons.tools.constant.NumConstant;
import org.apache.commons.lang3.StringUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 文件操作工具
*
* @author work@yujt.net.cn
* @date 2020/5/18 15:27
*/
public class FileUtils {
/**
* File文件转为byte[]
*
* @param file
* @return byte[]
* @author work@yujt.net.cn
* @date 2019/9/19 15:56
*/
public static byte[] fileToByteArray(File file) {
try {
//获取输入流
FileInputStream fis = new FileInputStream(file);
//新的 byte 数组输出流,缓冲区容量1024byte
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
//缓存
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != NumConstant.ONE_NEG) {
bos.write(b, NumConstant.ZERO, n);
}
fis.close();
//改变为byte[]
byte[] data = bos.toByteArray();
bos.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 删除文件夹强制删除
*
* @param path
*/
public static void deleteAllFilesOfDir(File path) {
if (null != path) {
if (!path.exists()) {
return;
}
if (path.isFile()) {
boolean result = path.delete();
int tryCount = 0;
while (!result && tryCount++ < 10) {
System.gc(); // 回收资源
result = path.delete();
}
}
File[] files = path.listFiles();
if (null != files) {
for (int i = 0; i < files.length; i++) {
deleteAllFilesOfDir(files[i]);
}
}
path.delete();
}
}
/**
* BufferedImage文件转为byte[]
*
* @param bImage 图片
* @param formatName 格式 e.g. png
* @return byte[]
* @author work@yujt.net.cn
* @date 2020/8/7 13:30
*/
public static byte[] imageToBytes(BufferedImage bImage, String formatName) {
if (null == bImage || StringUtils.isBlank(formatName)) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, formatName, out);
} catch (IOException e) {
//log.error(e.getMessage());
}
return out.toByteArray();
}
}

56
epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/StreamUtils.java

@ -0,0 +1,56 @@
package com.elink.esua.epdc.commons.tools.utils;
import com.elink.esua.epdc.commons.tools.exception.RenException;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* 接收文件转化File
* Created by liuhongwei on 2019/6/21.
*/
public class StreamUtils {
public static File conversionFile(MultipartFile file){
File toFile =null;
InputStream ins = null;
try {
// 转化字节流
ins = file.getInputStream();
// 获取文件名字
toFile = new File(file.getOriginalFilename());
// 字节转化文件
inputStreamToFile(ins, toFile);
ins.close();
} catch (IOException e) {
new RenException(500,"文件转化失败");
}
return toFile;
}
/**
* 流转化
* @param ins file
* @return
* @author liuhongwei
* @date 2019/6/14 14:07
*/
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Loading…
Cancel
Save