diff --git a/serverlogic/build.gradle b/serverlogic/build.gradle index fc1fa73eb..84f4d7105 100644 --- a/serverlogic/build.gradle +++ b/serverlogic/build.gradle @@ -28,6 +28,7 @@ dependencies { compile("com.google.protobuf:protobuf-java:2.5.0") compile("com.fasterxml.jackson.core:jackson-core:2.3.1") compile("com.fasterxml.jackson.core:jackson-databind:2.3.3") + compile("org.apache.poi:poi-ooxml:3.12") compile files("${System.properties['java.home']}/../lib/tools.jar") } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/ExcelUtils.java b/serverlogic/src/main/java/com/ljsd/jieling/util/ExcelUtils.java new file mode 100644 index 000000000..89525d054 --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/ExcelUtils.java @@ -0,0 +1,240 @@ +package com.example.demo.json; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ExcelUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class); + private static boolean isWrite = true; + private static String excelPath ="tmp/"; + private static String path = "C:/ljsd/jieling_conf/"; + + public static void main(String[] args) throws IOException { + readExcelData(); + + } + public static void readExcelData() throws IOException { + File file = new File(excelPath); + if (file.exists()) { + File[] files = file.listFiles(); + for (File file1 : files) { + if (file1.isDirectory()) { + continue; + } + String extString = file1.getName().substring(file1.getName().lastIndexOf(".")); + if (!".xlsx".equals(extString)) { + continue; + } + String filePath = excelPath + file1.getName(); + Workbook wb = readExcel(filePath); + if(file1.getName().contains("1.Map_")){ + bulidMapInfo(wb); + }else{ + buildDataInfo(wb); + } + } + } + } + + private static void buildDataInfo(Workbook wb) throws IOException { + Sheet sheet; + Row row; + Object cellData; + sheet = wb.getSheetAt(0); + String sheetName = sheet.getSheetName(); + LOGGER.info("collection name={}", sheetName); + int rownum = sheet.getPhysicalNumberOfRows(); + FileWriter fw = new FileWriter(path + sheetName + ".txt"); + PrintWriter out = new PrintWriter(fw); + for (int i = 0; i < rownum; i++) { + if (i == 2 || i == 3){ + continue; + } + Row row1 = sheet.getRow(2); + row = sheet.getRow(i); + if (row != null) { + int colnum = row.getPhysicalNumberOfCells(); + StringBuilder info = new StringBuilder(); + for (int j = 0; j < colnum; j++) { + if (getCellFormatValue(row1.getCell(j)).toString().isEmpty()){ + continue; + } + int cellFormatValue = (int) getCellFormatValue(row1.getCell(j)); + if (cellFormatValue == 3){ + continue; + } + //文件名 + cellData = getCellFormatValue(row.getCell(j)); + if (info.length() == 0){ + info = info.append(cellData); + }else{ + info.append("\t").append(cellData); + } + } + out.write(info.toString()); + out.write("\r\n"); + } + } + fw.close(); + out.close(); + } + + private static void bulidMapInfo(Workbook wb) throws IOException { + Sheet sheet = wb.getSheetAt(0); + String sheetName = sheet.getSheetName(); + LOGGER.info("collection name={}", sheetName); + int rowNum = sheet.getPhysicalNumberOfRows(); + FileWriter fw = new FileWriter(path + sheetName + ".txt"); + PrintWriter out = new PrintWriter(fw); + Object[][] aa = new Object[rowNum][]; + for (int i = 0; i < rowNum; i++) { + Row row = sheet.getRow(i); + Object[] bb = new Object[rowNum]; + for (int j = 0; j < rowNum; j++) { + //文件名 + Object cellData = getCellFormatValue(row.getCell(j)); + if (!"".equals(cellData)) { + bb[j] = cellData; + } + } + aa[i] = bb; + } + Map> infoMap = getObjectListMap(aa,sheetName); + buildJson(out, infoMap); + fw.close(); + out.close(); + } + + private static Map> getObjectListMap(Object[][] aa,String sheetName) throws IOException { + Map> infoMap = new HashMap<>(); + for (int i = 1; i < aa.length; i++) { + for (int j = 1; j < aa[i].length; j++) { + if (aa[i][j] == null) { + continue; + } + if (isWrite) { +// FileWriter fw = new FileWriter("tmp/json/"+sheetName+"_size.txt"); +// PrintWriter out = new PrintWriter(fw); +// String[] split = aa[0][0].toString().split(","); +// out.write("id"+"\t"+"Length"+"\t"+"Width"); +// out.write("\r\n"); +// out.write("int\tint\tint"); +// out.write("\r\n"); +// out.write(1+"\t"+split[0] +"\t"+ split[1]); +// out.write("\r\n"); +// out.close(); + isWrite = false; + } + List objects = new ArrayList<>(); + Object cc = aa[0][j] +"#"+ aa[i][0]; + if (infoMap.containsKey(aa[i][j])) { + objects = infoMap.get(aa[i][j]); + objects.add(cc); + } else { + objects.add(cc); + } + infoMap.put(aa[i][j], objects); + } + } + isWrite = true; + return infoMap; + } + + private static void buildJson(PrintWriter out, Map> infoMap) { + int i = 0; + for (Map.Entry> entry : infoMap.entrySet()) { + String[] split = entry.getKey().toString().split("#"); + Object npc = entry.getKey(); + if (split.length >= 2){ + npc = split[1]; + } + List value = entry.getValue(); + StringBuilder groups = new StringBuilder(); + for (Object info :value){ + if (groups.length() == 0){ + groups = new StringBuilder((String) info); + }else{ + groups.append("|").append(info); + } + } + if (i == 0){ + out.write("id\tEvent\tGroups"); + out.write("\r\n"); + out.write("int"+"\t"+"int"+"\t"+"mut,int#int,2"); + out.write("\r\n"); + } + i = i +1 ; + out.write( i+"\t"+npc+"\t"+groups.toString()); + out.write("\r\n"); + } + } + + //读取excel + private static Workbook readExcel(String filePath) { + Workbook wb = null; + if (filePath == null) { + return null; + } + String extString = filePath.substring(filePath.lastIndexOf(".")); + InputStream is; + try { + is = new FileInputStream(filePath); + if (".xls".equals(extString)) { + return wb = new HSSFWorkbook(is); + } else if (".xlsx".equals(extString)) { + wb = new XSSFWorkbook(is); + return wb; + } else { + return wb = null; + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return wb; + } + + public static Object getCellFormatValue(Cell cell) { + Object cellValue = null; + if (cell != null) { + //判断cell类型 + switch (cell.getCellType()) { + case Cell.CELL_TYPE_NUMERIC: { + cellValue = (int) cell.getNumericCellValue(); + break; + } + case Cell.CELL_TYPE_FORMULA: { + //判断cell是否为日期格式 + if (DateUtil.isCellDateFormatted(cell)) { + //转换为日期格式YYYY-mm-dd + cellValue = cell.getDateCellValue(); + } else { + //数字 + cellValue = String.valueOf(cell.getNumericCellValue()); + } + break; + } + case Cell.CELL_TYPE_STRING: { + cellValue = cell.getRichStringCellValue().getString(); + break; + } + default: + cellValue = ""; + } + } else { + cellValue = ""; + } + return cellValue; + } +} \ No newline at end of file