From b0f4749bf7ed767f0df2c0cd9967c34aa812c4c5 Mon Sep 17 00:00:00 2001
From: skyouc
Date: 星期四, 20 二月 2025 14:52:28 +0800
Subject: [PATCH] #修改 1. 物料字段修改
---
rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java | 81 ++++++++++++++++++++++++++++++++++++++--
1 files changed, 76 insertions(+), 5 deletions(-)
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java b/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java
index ebffe54..bab11e6 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java
@@ -3,9 +3,10 @@
import com.vincent.rsf.framework.common.Cools;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-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.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@@ -13,8 +14,7 @@
import java.lang.reflect.Modifier;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
/**
* Created by vincent on 2/17/2024
@@ -90,6 +90,77 @@
return workbook;
}
+ /**
+ * Excel 瀵煎叆
+ * @param file 鏂囦欢
+ * @param keys 鏁版嵁椤哄簭
+ */
+ public static List<Map<String, Object>> importExcel(MultipartFile file, String[] keys) throws Exception{
+ Workbook wb = null;
+ String fileName = file.getOriginalFilename();
+ if (fileName.endsWith("xls")) {
+ POIFSFileSystem pois = new POIFSFileSystem(file.getInputStream());
+ wb = new HSSFWorkbook(pois);
+ } else if (fileName.endsWith("xlsx")) {
+ wb = new XSSFWorkbook(file.getInputStream());
+ }
+ Sheet sheet = wb.getSheetAt(0);
+ int rowCount = sheet.getPhysicalNumberOfRows();
+ if (sheet.getRow( 1).getPhysicalNumberOfCells() != keys.length){
+ throw new RuntimeException("瀵煎叆鐨凟xcel鍜屾ā鏉跨殑鍒椾笉鍖归厤");
+ }
+ List<Map<String,Object>> result = new ArrayList<>();
+ for (int i = 0; i < rowCount - 1; i++) {
+ Row row = sheet.getRow(i + 1);
+ Map<String,Object> tmp = new HashMap<>();
+ for (int j = 0;j < keys.length; j++){
+ Cell cell = row.getCell(j);
+ // 鎶婄被鍨嬭浆琛孲tring
+// cell.setCellType(CellType.STRING);
+ tmp.put(keys[j], cell.getStringCellValue());
+ }
+ result.add(tmp);
+ }
+ return result;
+ }
+ /**
+ * 琛ㄥご鏍峰紡
+ */
+ private static CellStyle HeaderStyle(Workbook wb){
+ Font font = wb.createFont();
+ font.setFontName("瀹嬩綋");
+ font.setFontHeightInPoints((short) 11);
+ CellStyle cellStyle = commonStyle(wb);
+ cellStyle.setFont(font);
+ return cellStyle;
+ }
+
+ /**
+ * 鍐呭鏍峰紡
+ */
+ private static CellStyle contentStyle(Workbook wb){
+ Font font = wb.createFont();
+ font.setFontName("瀹嬩綋");
+ font.setFontHeightInPoints((short) 10);
+ CellStyle cellStyle = commonStyle(wb);
+ cellStyle.setFont(font);
+ return cellStyle;
+ }
+
+ /**
+ * 鍏叡鏍峰紡
+ */
+ private static CellStyle commonStyle(Workbook wb){
+ CellStyle style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.CENTER);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBorderLeft(BorderStyle.THIN);
+ style.setBorderTop(BorderStyle.THIN);
+ style.setBorderRight(BorderStyle.THIN);
+ style.setWrapText(true);// 鑷姩鎹㈣
+ return style;
+ }
}
--
Gitblit v1.9.1