|
@@ -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;
|
|
|
+ }
|
|
|
+}
|