|
@@ -0,0 +1,136 @@
|
|
|
|
+package com.galaxis.manatee.Test.zzx;
|
|
|
|
+
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
|
+import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
|
+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.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@SpringBootTest
|
|
|
|
+/**
|
|
|
|
+ * 读取excel文件
|
|
|
|
+ */
|
|
|
|
+public class ProjectTest2 {
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+// for (int i = 0; i < list.get(0).size(); i++) {
|
|
|
|
+// List<Object> objects = list.get(0).get(i);
|
|
|
|
+// if (i > 1) {
|
|
|
|
+// //System.err.println(objects.toString());
|
|
|
|
+// String objectsSub = objects.toString().substring(0, objects.toString().length() - 1);
|
|
|
|
+// String[] splitArr = objectsSub.split(",");
|
|
|
|
+// //System.err.println(splitArr.length);
|
|
|
|
+// System.err.println(splitArr[9]);
|
|
|
|
+// //splitArr数组:9-一级领导,10-二级领导,11-三级领导,12-四级领导,13-分公司总经理,14-集团分管领导
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ List<List<Object>> excel1 = getExcel("C:\\Users\\86186\\Desktop\\分权手册2022_09_20_16_31_58.xlsx");
|
|
|
|
+ List<List<Object>> excel2 = getExcel("C:\\Users\\86186\\Desktop\\凯捷--2022年度分级授权手册20220920更新了花名册.xls");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<List<Object>> getExcel(String filePath){
|
|
|
|
+ Workbook wb = null;
|
|
|
|
+ Sheet sheet = null;
|
|
|
|
+ Row row = null;
|
|
|
|
+
|
|
|
|
+ // String filePath1 = "C:\\Users\\86186\\Desktop\\分权手册2022_09_20_16_31_58.xlsx";
|
|
|
|
+ wb = readExcel(filePath);
|
|
|
|
+
|
|
|
|
+ List<List<List<Object>>> list = new ArrayList<>();
|
|
|
|
+ if (wb != null) {
|
|
|
|
+ try {
|
|
|
|
+ // 循环页签
|
|
|
|
+ for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
|
|
|
|
+ // 指定页签的值
|
|
|
|
+ sheet = wb.getSheetAt(sheetNum);
|
|
|
|
+ // 定义存放一个页签中所有数据的List
|
|
|
|
+ List<List<Object>> sheetList = new ArrayList<>();
|
|
|
|
+ // 循环行
|
|
|
|
+ for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
|
|
|
|
+ // 指定行的值
|
|
|
|
+ row = sheet.getRow(rowNum);
|
|
|
|
+ // 定义存放一行数据的List
|
|
|
|
+ List<Object> rowList = new ArrayList<>();
|
|
|
|
+ // 循环列
|
|
|
|
+ for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
|
|
|
|
+ Cell cell = sheet.getRow(rowNum).getCell(cellNum);
|
|
|
|
+ rowList.add(getStringCellValue(cell));
|
|
|
|
+ }
|
|
|
|
+ sheetList.add(rowList);
|
|
|
|
+ }
|
|
|
|
+ list.add(sheetList);
|
|
|
|
+ }
|
|
|
|
+ System.err.println(list.get(0).size());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list.get(0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //判断文件格式
|
|
|
|
+ public static Workbook readExcel(String filePath) {
|
|
|
|
+ if (filePath == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ String extString = filePath.substring(filePath.lastIndexOf("."));
|
|
|
|
+ try {
|
|
|
|
+ InputStream is = new FileInputStream(filePath);
|
|
|
|
+ if (".xls".equals(extString)) {
|
|
|
|
+ return new HSSFWorkbook(is);
|
|
|
|
+ } else if (".xlsx".equals(extString)) {
|
|
|
|
+ return new XSSFWorkbook(is);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getStringCellValue(Cell cell) {
|
|
|
|
+ String cellvalue = "";
|
|
|
|
+ if (cell == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ switch (cell.getCellType()) {
|
|
|
|
+ case Cell.CELL_TYPE_STRING:
|
|
|
|
+ cellvalue = cell.getStringCellValue();
|
|
|
|
+ break;
|
|
|
|
+ case Cell.CELL_TYPE_NUMERIC:
|
|
|
|
+ if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ Date date = cell.getDateCellValue();
|
|
|
|
+ cellvalue = sdf.format(date);
|
|
|
|
+ } else {
|
|
|
|
+ cellvalue = String.valueOf(cell.getNumericCellValue());
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case Cell.CELL_TYPE_BOOLEAN:
|
|
|
|
+ cellvalue = String.valueOf(cell.getBooleanCellValue());
|
|
|
|
+ break;
|
|
|
|
+ case Cell.CELL_TYPE_BLANK:
|
|
|
|
+ cellvalue = "";
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ cellvalue = "";
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ if (cellvalue == "") {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ return cellvalue;
|
|
|
|
+ }
|
|
|
|
+}
|