5 changed files with 372 additions and 1 deletions
-
132src/main/java/com/gaotao/common/utils/ExcelUtil.java
-
25src/main/java/com/gaotao/modules/notify/controller/NewIssureController.java
-
56src/main/java/com/gaotao/modules/notify/entity/NotifyExcelData.java
-
14src/main/java/com/gaotao/modules/notify/service/NewIssureService.java
-
146src/main/java/com/gaotao/modules/notify/service/impl/NewIssureServiceImpl.java
@ -0,0 +1,132 @@ |
|||
package com.gaotao.common.utils; |
|||
|
|||
import org.apache.poi.ss.usermodel.*; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.*; |
|||
|
|||
/** |
|||
* Excel工具类 |
|||
* 用于读取Excel文件并转换为Map列表 |
|||
*/ |
|||
public class ExcelUtil { |
|||
|
|||
/** |
|||
* 读取Excel文件并转换为Map列表 |
|||
* @param file Excel文件 |
|||
* @return List<Map<String, Object>> 每行数据的Map列表,key为列索引(从0开始) |
|||
* @throws Exception 读取异常 |
|||
*/ |
|||
public static List<Map<String, Object>> readExcelToMapList(MultipartFile file) throws Exception { |
|||
List<Map<String, Object>> resultList = new ArrayList<>(); |
|||
|
|||
try (InputStream inputStream = file.getInputStream(); |
|||
Workbook workbook = WorkbookFactory.create(inputStream)) { |
|||
|
|||
// 获取第一个工作表 |
|||
Sheet sheet = workbook.getSheetAt(0); |
|||
|
|||
// 获取有效行数 |
|||
int lastRowNum = sheet.getLastRowNum(); |
|||
|
|||
// 遍历每一行(跳过表头,从第1行开始) |
|||
for (int rowIndex = 1; rowIndex <= lastRowNum; rowIndex++) { |
|||
Row row = sheet.getRow(rowIndex); |
|||
if (row == null) { |
|||
continue; |
|||
} |
|||
|
|||
// 检查是否为空行 |
|||
if (isEmptyRow(row)) { |
|||
continue; |
|||
} |
|||
|
|||
Map<String, Object> rowMap = new HashMap<>(); |
|||
|
|||
// 遍历每一列 |
|||
for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) { |
|||
Cell cell = row.getCell(cellIndex); |
|||
Object cellValue = getCellValue(cell); |
|||
rowMap.put(String.valueOf(cellIndex), cellValue); |
|||
} |
|||
|
|||
resultList.add(rowMap); |
|||
} |
|||
|
|||
} catch (IOException e) { |
|||
throw new Exception("读取Excel文件失败: " + e.getMessage(), e); |
|||
} |
|||
|
|||
return resultList; |
|||
} |
|||
|
|||
/** |
|||
* 获取单元格的值 |
|||
* @param cell 单元格 |
|||
* @return 单元格值 |
|||
*/ |
|||
private static Object getCellValue(Cell cell) { |
|||
if (cell == null) { |
|||
return null; |
|||
} |
|||
|
|||
switch (cell.getCellType()) { |
|||
case STRING: |
|||
return cell.getStringCellValue().trim(); |
|||
case NUMERIC: |
|||
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) { |
|||
// 日期类型 |
|||
Date date = cell.getDateCellValue(); |
|||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
|||
return sdf.format(date); |
|||
} else { |
|||
// 数字类型 |
|||
double numericValue = cell.getNumericCellValue(); |
|||
// 如果是整数,返回整数格式 |
|||
if (numericValue == Math.floor(numericValue)) { |
|||
return String.valueOf((long) numericValue); |
|||
} else { |
|||
return String.valueOf(numericValue); |
|||
} |
|||
} |
|||
case BOOLEAN: |
|||
return String.valueOf(cell.getBooleanCellValue()); |
|||
case FORMULA: |
|||
// 公式类型,获取计算后的值 |
|||
try { |
|||
return String.valueOf(cell.getNumericCellValue()); |
|||
} catch (Exception e) { |
|||
return cell.getStringCellValue().trim(); |
|||
} |
|||
case BLANK: |
|||
case ERROR: |
|||
default: |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 检查行是否为空 |
|||
* @param row 行对象 |
|||
* @return true表示空行 |
|||
*/ |
|||
private static boolean isEmptyRow(Row row) { |
|||
if (row == null) { |
|||
return true; |
|||
} |
|||
|
|||
for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) { |
|||
Cell cell = row.getCell(cellIndex); |
|||
if (cell != null && cell.getCellType() != CellType.BLANK) { |
|||
Object cellValue = getCellValue(cell); |
|||
if (cellValue != null && !cellValue.toString().trim().isEmpty()) { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
package com.gaotao.modules.notify.entity; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonFormat; |
|||
import lombok.Data; |
|||
import org.apache.ibatis.type.Alias; |
|||
import org.springframework.format.annotation.DateTimeFormat; |
|||
|
|||
import java.math.BigDecimal; |
|||
import java.util.Date; |
|||
|
|||
@Data |
|||
@Alias("NotifyExcelData") |
|||
public class NotifyExcelData { |
|||
|
|||
/** |
|||
* 工厂编码 |
|||
*/ |
|||
private String site; |
|||
|
|||
/** |
|||
* 订单号 |
|||
*/ |
|||
private String orderNo; |
|||
|
|||
/** |
|||
* 发布号 |
|||
*/ |
|||
private String releaseNo; |
|||
|
|||
/** |
|||
* 序列号 |
|||
*/ |
|||
private String sequenceNo; |
|||
|
|||
/** |
|||
* BOM行号 |
|||
*/ |
|||
private Integer bomLineNo; |
|||
|
|||
/** |
|||
* 物料编码 |
|||
*/ |
|||
private String materialPartNo; |
|||
|
|||
/** |
|||
* 需求日期 |
|||
*/ |
|||
@DateTimeFormat(pattern = "yyyy-MM-dd") |
|||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") |
|||
private Date needDate; |
|||
|
|||
/** |
|||
* 数量 |
|||
*/ |
|||
private BigDecimal qty; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue