package com.qianwen.smartman.common.utils; import java.awt.Color; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.qianwen.smartman.common.constant.ExcelConstant; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.modules.coproduction.dto.CheckResultDTO; public class ExcelUtils { /* JADX WARN: Multi-variable type inference failed */ public static ExcelDataDTO readExcel(InputStream inputStream) { XSSFWorkbook xSSFWorkbook = null; Workbook workBook = null; ExcelDataDTO excelDataDTO = buildDataDTO(); try { xSSFWorkbook = new XSSFWorkbook(inputStream); ByteArrayOutputStream bos = new ByteArrayOutputStream(); xSSFWorkbook.write(bos); excelDataDTO.setBytes(bos.toByteArray()); Sheet sheet = xSSFWorkbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); Row rowHead = sheet.getRow(1); int lastCellNum = rowHead.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { String value = rowHead.getCell(j).getStringCellValue(); excelDataDTO.addHead(value); } List heads = excelDataDTO.getHeads(); Integer index = Integer.valueOf(0); for (int i = 2; i <= lastRowNum; i++) { Row row = sheet.getRow(i); for (int k = 0; k < lastCellNum; k++) { String head = heads.get(k); Cell cell = row.getCell(k); String value = getCellValue(cell); excelDataDTO.setData(index, head, value); } Integer integer1 = index, integer2 = index = Integer.valueOf(index.intValue() + 1); } } catch (IOException e) { e.printStackTrace(); } finally { if (xSSFWorkbook != null) try { xSSFWorkbook.close(); } catch (IOException e) { e.printStackTrace(); } if (inputStream != null) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return excelDataDTO; /* Workbook workBook = null; ExcelDataDTO excelDataDTO = buildDataDTO(); try { try { workBook = new XSSFWorkbook(inputStream); ByteArrayOutputStream bos = new ByteArrayOutputStream(); workBook.write(bos); excelDataDTO.setBytes(bos.toByteArray()); Sheet sheet = workBook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); Row rowHead = sheet.getRow(1); int lastCellNum = rowHead.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { String value = rowHead.getCell(j).getStringCellValue(); excelDataDTO.addHead(value); } List heads = excelDataDTO.getHeads(); Integer index = 0; for (int i = 2; i <= lastRowNum; i++) { Row row = sheet.getRow(i); for (int j2 = 0; j2 < lastCellNum; j2++) { String head = heads.get(j2); Cell cell = row.getCell(j2); String value2 = getCellValue(cell); excelDataDTO.setData(index, head, value2); } index = Integer.valueOf(index.intValue() + 1); } if (workBook != null) { try { workBook.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { e2.printStackTrace(); } } } catch (IOException e3) { e3.printStackTrace(); if (workBook != null) { try { workBook.close(); } catch (IOException e4) { e4.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e5) { e5.printStackTrace(); } } } return excelDataDTO; } catch (Throwable th) { if (workBook != null) { try { workBook.close(); } catch (IOException e6) { e6.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e7) { e7.printStackTrace(); } } throw th; } */ } private static String getCellValue(Cell cell) { String s2; if (Func.isEmpty(cell)) { return ""; } String data = ""; CellType cellType = cell.getCellTypeEnum(); switch (cellType) { case _NONE: data = null; return data; case STRING: data = cell.getStringCellValue(); return data; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); return com.qianwen.core.tool.utils.DateUtil.DATETIME_FORMAT.format(date); } s2 = (new DecimalFormat("#.###########")).format(cell.getNumericCellValue()); data = (new BigDecimal(s2)).stripTrailingZeros().toPlainString(); return data; } data = ""; return data; /* String data; if (Func.isEmpty(cell)) { return ""; } CellType cellType = cell.getCellTypeEnum(); switch (AnonymousClass1.$SwitchMap$org$apache$poi$ss$usermodel$CellType[cellType.ordinal()]) { case 1: data = null; break; case 2: data = cell.getStringCellValue(); break; case 3: if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); return org.springblade.core.tool.utils.DateUtil.DATETIME_FORMAT.format(date); } String s2 = new DecimalFormat("#.###########").format(cell.getNumericCellValue()); data = new BigDecimal(s2).stripTrailingZeros().toPlainString(); break; default: data = ""; break; } return data; */ } public static Workbook generateFail(ExcelDataDTO excelDataDTO) { List> data = excelDataDTO.getData(); XSSFWorkbook workBook = null; byte[] bytes = excelDataDTO.getBytes(); InputStream inputStream = new ByteArrayInputStream(bytes); try { workBook = new XSSFWorkbook(inputStream); XSSFSheet sheetAt = workBook.getSheetAt(0); int lastRowNum = sheetAt.getLastRowNum(); Row rowHead = sheetAt.getRow(1); int lastCellNum = rowHead.getLastCellNum(); Cell headCell = rowHead.createCell(lastCellNum); headCell.setCellStyle(baseHeadStyle(workBook)); headCell.setCellValue(ExcelConstant.FAIL_REASON); List rowNums = new ArrayList<>(); for (int i = 2; i <= lastRowNum; i++) { Row row = sheetAt.getRow(i); Cell cell = row.createCell(lastCellNum); cell.setCellStyle(baseStyle(workBook)); Map map = data.get(i - 2); if (map.containsKey(ExcelConstant.FAIL_REASON)) { cell.setCellValue(map.get(ExcelConstant.FAIL_REASON)); } else { rowNums.add(Integer.valueOf(i + 1)); } } if (Func.isNotEmpty(rowNums)) { for (int i2 = sheetAt.getLastRowNum(); i2 <= ((Integer) Collections.max(rowNums)).intValue(); i2++) { sheetAt.createRow(i2); } rowNums.forEach(item -> { sheetAt.shiftRows(item.intValue(), sheetAt.getLastRowNum(), -1); }); } } catch (IOException e) { e.printStackTrace(); } return workBook; } public static Workbook generateExport(ExcelExportDTO excelExportDTO) { List
tableHeader = excelExportDTO.getTableHeader(); List> data = excelExportDTO.getData(); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true); XSSFSheet sheet = workbook.createSheet(); XSSFRow row = sheet.createRow(0); int cellNum = 0; Map cellNumMap = new HashMap<>(); for (Header header : tableHeader) { String text = header.getText(); sheet.setColumnWidth(cellNum, text.getBytes().length * 512); XSSFCell head = row.createCell(cellNum); head.setCellValue(text); if (header.isRequired()) { head.setCellStyle(exportHeadStyle(workbook)); } else { head.setCellStyle(exportHeadStyle(workbook)); } cellNumMap.put(text, Integer.valueOf(cellNum)); cellNum++; } for (int i = 0; i < data.size(); i++) { XSSFRow dataRow = sheet.createRow(1 + i); Map item = data.get(i); item.forEach((key, value) -> { Integer dataNum = (Integer) cellNumMap.get(key); XSSFCell cell = dataRow.createCell(dataNum.intValue()); cell.setCellValue(value); cell.setCellStyle(baseStyle(workbook)); }); } return workbook; } public static Workbook generateTemplate(ExcelTemplateDTO excelTemplateDTO) { List remind = excelTemplateDTO.getRemind(); List
tableHeader = excelTemplateDTO.getTableHeader(); int sumLine = tableHeader.size() - 1; XSSFWorkbook workbook = new XSSFWorkbook(); XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true); XSSFSheet sheet = workbook.createSheet(); XSSFRow row = sheet.createRow(0); row.setHeightInPoints((18 * remind.size()) + 30); XSSFCell cell = row.createCell(0); CellRangeAddress region = new CellRangeAddress(0, 0, 0, sumLine); sheet.addMergedRegion(region); cell.setCellStyle(cellStyle); StringBuilder sb = new StringBuilder(); for (RemindLine remindLine : remind) { sb.append(remindLine.getText()).append("\n"); } XSSFRichTextString ts = new XSSFRichTextString(sb.toString()); int count = 0; for (RemindLine remindLine2 : remind) { int temp = count + remindLine2.getText().length() + 1; if (remindLine2.isRed()) { ts.applyFont(count, temp, redBaseFont(workbook)); } else { ts.applyFont(count, temp, blackBaseFont(workbook)); } count = temp; } cell.setCellValue(ts); XSSFRow row2 = sheet.createRow(1); int cellNum = 0; for (Header header : tableHeader) { String text = header.getText(); sheet.setColumnWidth(cellNum, text.getBytes().length * 512); XSSFCell head = row2.createCell(cellNum); head.setCellValue(text); if (header.isRequired()) { head.setCellStyle(requiredHeadStyle(workbook)); } else { head.setCellStyle(baseHeadStyle(workbook)); } cellNum++; } return workbook; } public static CellStyle baseStyle(XSSFWorkbook workbook) { XSSFCellStyle createCellStyle = workbook.createCellStyle(); createCellStyle.setDataFormat(workbook.createDataFormat().getFormat("@")); createCellStyle.setFont(blackBaseFont(workbook)); return createCellStyle; } public static CellStyle requiredHeadStyle(XSSFWorkbook workbook) { XSSFCellStyle styleRequired = workbook.createCellStyle(); styleRequired.setFillPattern(FillPatternType.SOLID_FOREGROUND); styleRequired.setFillForegroundColor(new XSSFColor(new Color(192, 0, 0))); styleRequired.setDataFormat(workbook.createDataFormat().getFormat("@")); styleRequired.setFont(headFont(workbook)); return styleRequired; } public static CellStyle baseHeadStyle(XSSFWorkbook workbook) { XSSFCellStyle baseRequired = workbook.createCellStyle(); baseRequired.setFillPattern(FillPatternType.SOLID_FOREGROUND); baseRequired.setFillForegroundColor(new XSSFColor(new Color(64, 64, 64))); baseRequired.setDataFormat(workbook.createDataFormat().getFormat("@")); baseRequired.setFont(headFont(workbook)); return baseRequired; } public static CellStyle exportHeadStyle(XSSFWorkbook workbook) { XSSFCellStyle baseRequired = workbook.createCellStyle(); baseRequired.setFillPattern(FillPatternType.SOLID_FOREGROUND); baseRequired.setFillForegroundColor(new XSSFColor(new Color(192, 192, 192))); baseRequired.setDataFormat(workbook.createDataFormat().getFormat("@")); baseRequired.setFont(headBoldBaseFont(workbook)); return baseRequired; } public static Font headFont(XSSFWorkbook workbook) { XSSFFont createFont = workbook.createFont(); createFont.setColor(IndexedColors.WHITE.getIndex()); createFont.setFontHeightInPoints((short) 12); createFont.setFontName("微软雅黑"); return createFont; } public static Font redBaseFont(XSSFWorkbook workbook) { XSSFFont font = workbook.createFont(); font.setColor(new XSSFColor(new Color(255, 0, 0))); font.setFontHeightInPoints((short) 12); font.setFontName("微软雅黑"); return font; } public static Font blackBaseFont(XSSFWorkbook workbook) { XSSFFont createFont = workbook.createFont(); createFont.setColor(IndexedColors.BLACK.getIndex()); createFont.setFontHeightInPoints((short) 12); createFont.setFontName("微软雅黑"); return createFont; } public static Font headBoldBaseFont(XSSFWorkbook workbook) { XSSFFont createFont = workbook.createFont(); createFont.setColor(IndexedColors.BLACK.getIndex()); createFont.setFontHeightInPoints((short) 14); createFont.setBold(true); createFont.setFontName("微软雅黑"); return createFont; } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/common/utils/ExcelUtils$RemindLine.class */ public static class RemindLine { private String text; private boolean red; private boolean bold; public void setText(final String text) { this.text = text; } public void setRed(final boolean red) { this.red = red; } public void setBold(final boolean bold) { this.bold = bold; } public boolean equals(final Object o) { if (o == this) { return true; } if (o instanceof RemindLine) { RemindLine other = (RemindLine) o; if (other.canEqual(this) && isRed() == other.isRed() && isBold() == other.isBold()) { Object this$text = getText(); Object other$text = other.getText(); return this$text == null ? other$text == null : this$text.equals(other$text); } return false; } return false; } protected boolean canEqual(final Object other) { return other instanceof RemindLine; } public int hashCode() { int result = (1 * 59) + (isRed() ? 79 : 97); int result2 = (result * 59) + (isBold() ? 79 : 97); Object $text = getText(); return (result2 * 59) + ($text == null ? 43 : $text.hashCode()); } public String toString() { return "ExcelUtils.RemindLine(text=" + getText() + ", red=" + isRed() + ", bold=" + isBold() + ")"; } public RemindLine(final String text, final boolean red, final boolean bold) { this.text = text; this.red = red; this.bold = bold; } public String getText() { return this.text; } public boolean isRed() { return this.red; } public boolean isBold() { return this.bold; } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/common/utils/ExcelUtils$Header.class */ public static class Header { private String text; private boolean required; public void setText(final String text) { this.text = text; } public void setRequired(final boolean required) { this.required = required; } public boolean equals(final Object o) { if (o == this) { return true; } if (o instanceof Header) { Header other = (Header) o; if (other.canEqual(this) && isRequired() == other.isRequired()) { Object this$text = getText(); Object other$text = other.getText(); return this$text == null ? other$text == null : this$text.equals(other$text); } return false; } return false; } protected boolean canEqual(final Object other) { return other instanceof Header; } public int hashCode() { int result = (1 * 59) + (isRequired() ? 79 : 97); Object $text = getText(); return (result * 59) + ($text == null ? 43 : $text.hashCode()); } public String toString() { return "ExcelUtils.Header(text=" + getText() + ", required=" + isRequired() + ")"; } public Header(final String text, final boolean required) { this.text = text; this.required = required; } public String getText() { return this.text; } public boolean isRequired() { return this.required; } } /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/common/utils/ExcelUtils$ExcelExportDTO.class */ public static class ExcelExportDTO { private final List
tableHeader = new ArrayList(); private List> data = new ArrayList(); public void setData(final List> data) { this.data = data; } public boolean equals(final Object o) { if (o == this) { return true; } if (o instanceof ExcelExportDTO) { ExcelExportDTO other = (ExcelExportDTO) o; if (other.canEqual(this)) { Object this$tableHeader = getTableHeader(); Object other$tableHeader = other.getTableHeader(); if (this$tableHeader == null) { if (other$tableHeader != null) { return false; } } else if (!this$tableHeader.equals(other$tableHeader)) { return false; } Object this$data = getData(); Object other$data = other.getData(); return this$data == null ? other$data == null : this$data.equals(other$data); } return false; } return false; } protected boolean canEqual(final Object other) { return other instanceof ExcelExportDTO; } public int hashCode() { Object $tableHeader = getTableHeader(); int result = (1 * 59) + ($tableHeader == null ? 43 : $tableHeader.hashCode()); Object $data = getData(); return (result * 59) + ($data == null ? 43 : $data.hashCode()); } public String toString() { return "ExcelUtils.ExcelExportDTO(tableHeader=" + getTableHeader() + ", data=" + getData() + ")"; } public List
getTableHeader() { return this.tableHeader; } public List> getData() { return this.data; } public ExcelExportDTO addTableHead(String text, boolean required) { this.tableHeader.add(new Header(text, required)); return this; } public ExcelExportDTO addData(Map item) { this.data.add(item); return this; } } /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/common/utils/ExcelUtils$ExcelTemplateDTO.class */ public static class ExcelTemplateDTO { private final List remind = new ArrayList(); private final List
tableHeader = new ArrayList(); public List getRemind() { return this.remind; } public List
getTableHeader() { return this.tableHeader; } public ExcelTemplateDTO appendRemind(String text, boolean isRed, boolean bold) { this.remind.add(new RemindLine(text, isRed, bold)); return this; } public ExcelTemplateDTO addTableHead(String text, boolean required) { this.tableHeader.add(new Header(text, required)); return this; } } /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/common/utils/ExcelUtils$ExcelDataDTO.class */ public static class ExcelDataDTO { private List heads = new ArrayList(); private List> data = new ArrayList(); private List checkResultDTOS; private byte[] bytes; public void setHeads(final List heads) { this.heads = heads; } public void setData(final List> data) { this.data = data; } public void setCheckResultDTOS(final List checkResultDTOS) { this.checkResultDTOS = checkResultDTOS; } public void setBytes(final byte[] bytes) { this.bytes = bytes; } public boolean equals(final Object o) { if (o == this) { return true; } if (o instanceof ExcelDataDTO) { ExcelDataDTO other = (ExcelDataDTO) o; if (other.canEqual(this)) { Object this$heads = getHeads(); Object other$heads = other.getHeads(); if (this$heads == null) { if (other$heads != null) { return false; } } else if (!this$heads.equals(other$heads)) { return false; } Object this$data = getData(); Object other$data = other.getData(); if (this$data == null) { if (other$data != null) { return false; } } else if (!this$data.equals(other$data)) { return false; } Object this$checkResultDTOS = getCheckResultDTOS(); Object other$checkResultDTOS = other.getCheckResultDTOS(); if (this$checkResultDTOS == null) { if (other$checkResultDTOS != null) { return false; } } else if (!this$checkResultDTOS.equals(other$checkResultDTOS)) { return false; } return Arrays.equals(getBytes(), other.getBytes()); } return false; } return false; } protected boolean canEqual(final Object other) { return other instanceof ExcelDataDTO; } public int hashCode() { Object $heads = getHeads(); int result = (1 * 59) + ($heads == null ? 43 : $heads.hashCode()); Object $data = getData(); int result2 = (result * 59) + ($data == null ? 43 : $data.hashCode()); Object $checkResultDTOS = getCheckResultDTOS(); return (((result2 * 59) + ($checkResultDTOS == null ? 43 : $checkResultDTOS.hashCode())) * 59) + Arrays.hashCode(getBytes()); } public String toString() { return "ExcelUtils.ExcelDataDTO(heads=" + getHeads() + ", data=" + getData() + ", checkResultDTOS=" + getCheckResultDTOS() + ", bytes=" + Arrays.toString(getBytes()) + ")"; } public List getHeads() { return this.heads; } public List> getData() { return this.data; } public List getCheckResultDTOS() { return this.checkResultDTOS; } public byte[] getBytes() { return this.bytes; } public void addHead(String head) { this.heads.add(head); } private void addData(Integer index) { if (index.intValue() < this.data.size() && this.data.size() != 0) { return; } this.data.add(new HashMap()); addData(index); } public void setData(Integer index, String head, String value) { addData(index); Map map = this.data.get(this.data.size() - 1); map.put(head, value); } } public static ExcelExportDTO buildExcelExportDTO() { return new ExcelExportDTO(); } public static ExcelTemplateDTO buildExcelTemplateDTO() { return new ExcelTemplateDTO(); } public static ExcelDataDTO buildDataDTO() { return new ExcelDataDTO(); } }