yaoll преди 4 години
родител
ревизия
7f814e86ee

+ 10 - 0
dmp-common/pom.xml

@@ -48,6 +48,16 @@
             <artifactId>spring-boot-starter-data-jpa</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
 </project>

+ 33 - 0
dmp-common/src/main/java/com/persagy/common/security/MD5Utils.java

@@ -0,0 +1,33 @@
+package com.persagy.common.security;
+
+import java.security.MessageDigest;
+
+public class MD5Utils {
+
+	public static String getSign(String str) {
+		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+		try {
+			byte[] btInput = str.getBytes();
+			// 获得MD5摘要算法的 MessageDigest 对象
+			MessageDigest mdInst = MessageDigest.getInstance("MD5");
+			// 使用指定的字节更新摘要
+			mdInst.update(btInput);
+			// 获得密文
+			byte[] md = mdInst.digest();
+			// 把密文转换成十六进制的字符串形式
+			int j = md.length;
+			char arr[] = new char[j * 2];
+			int k = 0;
+			for (int i = 0; i < j; i++) {
+				byte byte0 = md[i];
+				arr[k++] = hexDigits[byte0 >>> 4 & 0xf];
+				arr[k++] = hexDigits[byte0 & 0xf];
+			}
+			return new String(arr);
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		}
+	}
+
+}

+ 7 - 0
dmp-common/src/main/java/com/persagy/dmp/common/excel/ExcelDataHandler.java

@@ -0,0 +1,7 @@
+package com.persagy.dmp.common.excel;
+
+public interface ExcelDataHandler<T> {
+
+	Object get(T data, String prop);
+
+}

+ 219 - 0
dmp-common/src/main/java/com/persagy/dmp/common/excel/ExcelUtils.java

@@ -0,0 +1,219 @@
+package com.persagy.dmp.common.excel;
+
+
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.InputStream;
+import java.util.*;
+
+public class ExcelUtils {
+
+	public static Object parseCell(Cell cell, String type) {
+		CellType cellType = cell.getCellTypeEnum();
+		if ("string".equalsIgnoreCase(type)) {
+			if (cellType == CellType.STRING) {
+				return cell.getStringCellValue();
+			} else if (cellType == CellType.NUMERIC) {
+				return cell.getNumericCellValue();
+			}
+		} else if ("integer".equalsIgnoreCase(type)) {
+			if (cellType == CellType.STRING) {
+				String stringCellValue = cell.getStringCellValue();
+				return stringCellValue == null ? null : Double.valueOf(stringCellValue).intValue();
+			} else if (cellType == CellType.NUMERIC) {
+				return Double.valueOf(cell.getNumericCellValue()).intValue();
+			}
+		} else if ("double".equalsIgnoreCase(type)) {
+			if (cellType == CellType.STRING) {
+				String stringCellValue = cell.getStringCellValue();
+				return stringCellValue == null ? null : Double.valueOf(stringCellValue);
+			} else if (cellType == CellType.NUMERIC) {
+				return cell.getNumericCellValue();
+			}
+		} else if ("date".equalsIgnoreCase(type)) {
+			if (cellType == CellType.NUMERIC) {
+				Date dateCellValue = cell.getDateCellValue();
+				return dateCellValue == null ? null : dateCellValue;
+			} else if (cellType == CellType.NUMERIC) {
+				return cell.getNumericCellValue();
+			}
+		}
+		return null;
+	}
+
+	public static List<List<Map<String, Object>>> read(InputStream inputStream, List<SheetReadInfo> infos) {
+		List<List<Map<String, Object>>> answer = new ArrayList<>(infos.size());
+
+		try {
+			Workbook workbook = new XSSFWorkbook(inputStream);
+
+			for (int infoIndex = 0; infoIndex < infos.size(); infoIndex++) {
+				List<Map<String, Object>> result = new ArrayList<>();
+				answer.add(result);
+
+				Sheet sheet = workbook.getSheetAt(infoIndex);
+				SheetReadInfo info = infos.get(infoIndex);
+				Integer startRow = info.getStartRow();
+				List<Integer> columnIndexs = info.getColumnIndexs();
+				List<String> keys = info.getColumnKeys();
+				List<String> types = info.getColumnTypes();
+
+				int lastRowNum = sheet.getLastRowNum();
+				for (int rowIndex = startRow; rowIndex < lastRowNum + 1; rowIndex++) {
+					Row row = sheet.getRow(rowIndex);
+					if (null == row) {
+						System.err.println("==========rowIndex:" + rowIndex);
+						continue;
+					}
+					Map<String, Object> data = new HashMap<>();
+					for (int columnDefIndex = 0; columnDefIndex < columnIndexs.size(); columnDefIndex++) {
+						Integer columnIndex = columnIndexs.get(columnDefIndex);
+						String key = keys.get(columnDefIndex);
+						String type = types.get(columnDefIndex);
+						Cell cell = row.getCell(columnIndex);
+						if (null == cell) {
+							System.err.println("==========columnIndex:" + columnIndex);
+							continue;
+						}
+						Object value = parseCell(cell, type);
+						if (null != value) {
+							data.put(key, value);
+						}
+					}
+					if (0 < data.size()) {
+						data.put(info.getSeqKey(), rowIndex);
+						result.add(data);
+					}
+				}
+			}
+			workbook.close();
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+		}
+		return answer;
+
+	}
+
+	public static List<Map<String, Object>> read(InputStream inputStream, SheetReadInfo info) {
+		List<List<Map<String, Object>>> read = read(inputStream, Arrays.asList(info));
+		return read.get(0);
+	}
+
+	public static Workbook createExcel(List<SheetWriteInfo> sheets) {
+		Workbook wb = new XSSFWorkbook();
+		CreationHelper createHelper = wb.getCreationHelper();  //创建帮助工具
+		for (SheetWriteInfo sheetInfo : sheets) {
+			Sheet sheet = wb.createSheet(sheetInfo.getSheetName());
+			List<String> headList = sheetInfo.getHeadList();
+			List<HorizontalAlignment> aligns = sheetInfo.getHorizontalAlignments();
+			List<String> props = sheetInfo.getProps();
+			List<String> types = sheetInfo.getDataTypes();
+			List<Integer> widths = sheetInfo.getWidths();
+			List data = sheetInfo.getData();
+			ExcelDataHandler handler = sheetInfo.getDataHandler();
+			setColumnWidth(sheet, widths);
+			CellStyle headerStyle = getHeaderStyle(wb);
+			Row headRow = sheet.createRow(0); //第一行为头
+			int columnSize = headList.size();
+			for (int i = 0; i < columnSize; i++) {  //遍历表头数据
+				Cell cell = headRow.createCell(i);  //创建单元格
+				cell.setCellValue(createHelper.createRichTextString(headList.get(i)));  //设置值
+				cell.setCellStyle(headerStyle);  //设置样式
+			}
+
+			List<CellStyle> dataStyles = new ArrayList<>(columnSize);
+			for (int i = 0; i < aligns.size(); i++) {
+				dataStyles.add(getDataStyle(wb, aligns.get(i)));
+			}
+
+			int rowIndex = 1;
+			if (data != null) {
+				for (Object datum : data) {
+					Row row = sheet.createRow(rowIndex++); //第一行为头
+					for (int j = 0; j < columnSize; j++) {  //编译每一行
+						Cell cell = row.createCell(j);
+						cell.setCellStyle(dataStyles.get(j));
+						String prop = props.get(j);
+						String type = types.get(j);
+						Object o = handler.get(datum, prop);
+						if (o != null) {
+							if (type.equalsIgnoreCase("string")) {
+								if (o == null) {
+									cell.setCellValue("");
+								} else {
+									String str = o.toString();
+									cell.setCellValue(str.length() > 3000 ? str.substring(0, 3000) : str);
+								}
+							} else if (type.equalsIgnoreCase("byte")) {
+								cell.setCellValue((byte) o);
+							} else if (type.equalsIgnoreCase("short")) {
+								cell.setCellValue((short) o);
+							} else if (type.equalsIgnoreCase("int")) {
+								cell.setCellValue((int) o);
+							} else if (type.equalsIgnoreCase("long")) {
+								cell.setCellValue((long) o);
+							} else if (type.equalsIgnoreCase("double")) {
+								cell.setCellValue((Double) o);
+							} else if (type.equalsIgnoreCase("date")) {
+								CellStyle cellStyle = dataStyles.get(j);
+								DataFormat df = wb.createDataFormat();
+								cellStyle.setDataFormat(df.getFormat("yyyy/m/d"));
+								cell.setCellValue((Date) o);
+								cell.setCellStyle(cellStyle);
+							} else {
+								cell.setCellValue(o.toString());
+							}
+						}
+					}
+				}
+			}
+		}
+		return wb;
+	}
+
+	private static void setColumnWidth(Sheet sheet, List<Integer> widths) {
+		for (int i = 0; i < widths.size(); i++) {
+			sheet.setColumnWidth(i, widths.get(i) * 256);
+		}
+	}
+
+	private static CellStyle getHeaderStyle(Workbook wb) {
+		//设置字体
+		Font headFont = wb.createFont();
+		headFont.setFontHeightInPoints((short) 14);
+		headFont.setFontName("Courier New");
+		headFont.setItalic(false);
+		headFont.setStrikeout(false);
+		//设置头部单元格样式
+		CellStyle headStyle = wb.createCellStyle();
+		headStyle.setBorderBottom(BorderStyle.THIN);  //设置单元格线条
+		headStyle.setBorderLeft(BorderStyle.THIN);
+		headStyle.setBorderRight(BorderStyle.THIN);
+		headStyle.setBorderTop(BorderStyle.THIN);
+		headStyle.setAlignment(HorizontalAlignment.CENTER);    //设置水平对齐方式
+		headStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐方式
+		headStyle.setFont(headFont);  //设置字体
+		return headStyle;
+	}
+
+	private static CellStyle getDataStyle(Workbook wb, HorizontalAlignment horizontalAlignment) {
+		//设置字体
+		Font headFont = wb.createFont();
+		headFont.setFontHeightInPoints((short) 14);
+		headFont.setFontName("Courier New");
+		headFont.setItalic(false);
+		headFont.setStrikeout(false);
+		//设置头部单元格样式
+		CellStyle dataStyle = wb.createCellStyle();
+		dataStyle.setBorderBottom(BorderStyle.THIN);  //设置单元格线条
+		dataStyle.setBorderLeft(BorderStyle.THIN);
+		dataStyle.setBorderRight(BorderStyle.THIN);
+		dataStyle.setBorderTop(BorderStyle.THIN);
+		dataStyle.setAlignment(horizontalAlignment);    //设置水平对齐方式
+		dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐方式
+		dataStyle.setFont(headFont);  //设置字体
+		return dataStyle;
+	}
+}

+ 28 - 0
dmp-common/src/main/java/com/persagy/dmp/common/excel/SheetReadInfo.java

@@ -0,0 +1,28 @@
+package com.persagy.dmp.common.excel;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.LinkedList;
+import java.util.List;
+
+@Getter
+@Setter
+public class SheetReadInfo {
+
+	private Integer startRow = 0;
+
+	private String seqKey = "seq";
+
+	private List<Integer> columnIndexs = new LinkedList<>();
+
+	private List<String> columnKeys = new LinkedList<>();
+
+	private List<String> columnTypes = new LinkedList<>();
+
+	public void add(Integer index, String key, String type) {
+		this.columnIndexs.add(index);
+		this.columnKeys.add(key);
+		this.columnTypes.add(type);
+	}
+}

+ 37 - 0
dmp-common/src/main/java/com/persagy/dmp/common/excel/SheetWriteInfo.java

@@ -0,0 +1,37 @@
+package com.persagy.dmp.common.excel;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+
+import java.util.LinkedList;
+import java.util.List;
+
+@Getter
+@Setter
+public class SheetWriteInfo<T> {
+
+	private String sheetName;
+
+	private List<String> headList = new LinkedList<>();
+
+	private List<Integer> widths = new LinkedList<>();
+
+	private List<HorizontalAlignment> horizontalAlignments = new LinkedList<>();
+
+	private List<String> dataTypes = new LinkedList<>();
+
+	private List<String> props = new LinkedList<>();
+
+	private ExcelDataHandler<T> dataHandler;
+
+	private List<T> data;
+
+	public void addColumn(String header, String porp, String dataType, Integer width, HorizontalAlignment horizontalAlignment){
+		this.headList.add(header);
+		this.props.add(porp);
+		this.dataTypes.add(dataType);
+		this.widths.add(width);
+		this.horizontalAlignments.add(horizontalAlignment);
+	}
+}