import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; 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; // excel文件地址 private static String excelPath ="tmp/"; // 生成txt文件地址 private static String path = "conf/server/"; 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(); System.out.println("buildDataInfo => collection name="+sheetName); // 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(); System.out.println("bulidMapInfo => collection name="+sheetName); // 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; } }