429 lines
16 KiB
Java
429 lines
16 KiB
Java
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.ss.usermodel.*;
|
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public class ExcelUtils {
|
|
private static boolean isWrite = true;
|
|
private static String excelPath ="F:/work2/data_execl/base_data/"; //excel 文件
|
|
// private static String excelPath ="D:/excel/0.94/data_execl/Map_data/"; //excel 文件
|
|
private static String path = "conf/server/";
|
|
private static Set<String> oldFileNames = new HashSet<>();
|
|
private static String javaFilePath = "tablemanager/src/main/java/config/";
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
File oldfile = new File(path);
|
|
String[] list = oldfile.list();
|
|
oldFileNames.addAll(Arrays.asList(list));
|
|
readExcelData();
|
|
//只加载新文件
|
|
Set<String> newFileNames = new HashSet<>();
|
|
File newfile = new File(path);
|
|
String[] newList = newfile.list();
|
|
newFileNames.addAll(Arrays.asList(newList));
|
|
newFileNames.removeAll(oldFileNames);
|
|
if(!newFileNames.isEmpty()) {
|
|
for (String newFileName : newFileNames) {
|
|
String finalNewFileName = newFileName.replaceAll(".txt", "");
|
|
genJavaFile(finalNewFileName);
|
|
}
|
|
}
|
|
|
|
}
|
|
public static void genJavaFile(String fileName){
|
|
|
|
Map<String,String> fieldInfo = new HashMap<>();
|
|
File file = new File("conf/server/"+fileName +".txt");
|
|
StringBuffer stringBuffer = new StringBuffer("package config;\n" +
|
|
"\n" +
|
|
"import manager.STableManager;\n" +
|
|
"import manager.Table;\n" +
|
|
"\n" +
|
|
"import java.util.Map;\n" +
|
|
"\n" +
|
|
"@Table(name =\"").append(fileName).append("\")\r\n");
|
|
stringBuffer.append("public class S").append(fileName).append(" implements BaseConfig {\n\n");
|
|
StringBuffer getInfos = new StringBuffer();
|
|
if(file.exists()){
|
|
try {
|
|
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
|
String firstLine = bufferedReader.readLine();
|
|
String secondLine = bufferedReader.readLine();
|
|
String[] s = firstLine.split("\t");
|
|
String[] s1 = secondLine.split("\t");
|
|
|
|
for(int i=0;i<s.length;i++){
|
|
String fieldType = typeInfo(s1[i]);
|
|
String fieldName = s[i];
|
|
String firstChart = fieldName.substring(0, 1).toLowerCase();
|
|
StringBuffer finalFieldName = new StringBuffer().append(firstChart).append(fieldName.substring(1));
|
|
getInfos.append("\tpublic ").append(fieldType).append(" ").append("get").append(fieldName).append("() {\n" +
|
|
" return " ).append(finalFieldName).append(";\r\n").append(
|
|
" }\r\n\n");
|
|
stringBuffer.append("\tprivate ").append(fieldType).append(" ").append(finalFieldName).append(";").
|
|
append("\r\n\n");
|
|
}
|
|
System.out.println(firstLine);
|
|
System.out.println(secondLine);
|
|
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
getInfos.append("\r\n}");
|
|
stringBuffer.append(" \n\t@Override\n" +
|
|
" public void init() throws Exception {\n" +
|
|
" \n" +
|
|
" }\n\n\n");
|
|
File targetJavaFile = new File(javaFilePath + "S" + fileName + ".java");
|
|
try(PrintWriter printWriter = new PrintWriter(new FileOutputStream(targetJavaFile));) {
|
|
printWriter.print(stringBuffer);
|
|
printWriter.print(getInfos);
|
|
printWriter.flush();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println(stringBuffer);
|
|
System.out.println(getInfos);
|
|
}
|
|
|
|
public static void readExcelData() throws Exception {
|
|
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("Map_")){
|
|
bulidMapInfo(wb);
|
|
}else{
|
|
buildDataInfo(wb);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static String typeInfo(String fieldInfo){
|
|
//mut,int#int,2
|
|
if(fieldInfo.contains("mut")){
|
|
String[] split = fieldInfo.split("#");
|
|
String[] typeInfos = split[1].split(",");
|
|
int nums = Integer.parseInt(typeInfos[1]);
|
|
StringBuffer sb = new StringBuffer();
|
|
if("string".equals(typeInfos[0])){
|
|
sb.append("String");
|
|
}else{
|
|
sb.append(typeInfos[0]);
|
|
}
|
|
|
|
|
|
for(int i=0;i<nums;i++){
|
|
sb.append("[]");
|
|
}
|
|
return sb.toString();
|
|
}
|
|
if("string".equals(fieldInfo)){
|
|
return "String";
|
|
}
|
|
return fieldInfo;
|
|
}
|
|
|
|
private static void buildDataInfo(Workbook wb) throws Exception {
|
|
Sheet sheet;
|
|
Row row;
|
|
Object cellData;
|
|
sheet = wb.getSheetAt(0);
|
|
String sheetName = sheet.getSheetName();
|
|
System.out.println("bulidMapInfo => collection name="+sheetName);
|
|
int rownum = sheet.getLastRowNum();
|
|
FileWriter fw = new FileWriter(path + sheetName + ".txt");
|
|
PrintWriter out = new PrintWriter(fw);
|
|
Set<Integer> indexCheck = new HashSet<>();
|
|
for (int i = 0; i <= rownum; i++) {
|
|
if (i == 2 || i == 3 || i == 4 ||i == 5||i == 6){
|
|
continue;
|
|
}
|
|
Row row1 = sheet.getRow(2);
|
|
Row row2 = sheet.getRow(1);
|
|
Row row4 = sheet.getRow(4);
|
|
row = sheet.getRow(i);
|
|
// for (Cell c :row){
|
|
// System.out.println(c.getColumnIndex() +"==="+row.getCell(c.getColumnIndex()));
|
|
// }
|
|
if (row != null) {
|
|
StringBuilder info = new StringBuilder();
|
|
int colnum = row1.getLastCellNum();
|
|
for (int j = 1; j < colnum; j++) {
|
|
int cellFormatValue = (int) getCellFormatValue(row1.getCell(j),null);
|
|
if (cellFormatValue == 3 || cellFormatValue == 0){
|
|
continue;
|
|
}
|
|
Cell cell = row.getCell(j);
|
|
Cell cell1 = row4.getCell(j);
|
|
if (cell == null){
|
|
info = getStringBuilder(row2, info, j, cell1);
|
|
}else{
|
|
//添加默认值
|
|
if (cell.toString().isEmpty()&&(cell1!=null)&&(!cell1.toString().isEmpty())){
|
|
info = getStringBuilder(row2, info, j, cell1);
|
|
continue;
|
|
}
|
|
if (getCellFormatValue(row1.getCell(j),null).toString().isEmpty()){
|
|
continue;
|
|
}
|
|
//文件名
|
|
cellData = getCellFormatValue(cell,row2.getCell(j));
|
|
if ("".equals(cellData.toString())){
|
|
cellData = getIn(row2.getCell(j).toString());
|
|
}
|
|
if (info.length() == 0){
|
|
info = info.append(cellData);
|
|
}else{
|
|
info.append("\t").append(cellData);
|
|
}
|
|
if(i>2&&j==1){
|
|
try {
|
|
if(indexCheck.contains(cellData)){
|
|
throw new Exception("\r\ncollection:="+sheetName+" 重复\r\n :index: "+cellData+"\r\n :row:"+(i+1));
|
|
}
|
|
indexCheck.add(Integer.valueOf(cellData.toString()));
|
|
}catch (ClassCastException e){
|
|
System.out.println("wb = [" + e + "]");
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
out.write(info.toString());
|
|
out.write("\r\n");
|
|
}
|
|
}
|
|
fw.close();
|
|
out.close();
|
|
}
|
|
|
|
private static StringBuilder getStringBuilder(Row row2, StringBuilder info, int j, Cell cell1) {
|
|
Object cellData;
|
|
if (cell1==null){
|
|
String in = getIn(row2.getCell(j).toString());
|
|
if (info.length() == 0){
|
|
info = info.append(in);
|
|
}else{
|
|
info.append("\t").append(in);
|
|
}
|
|
}else{
|
|
cellData = getCellFormatValue(cell1,row2.getCell(j));
|
|
if (info.length() == 0){
|
|
info = info.append(cellData);
|
|
}else{
|
|
info.append("\t").append(cellData);
|
|
}
|
|
}
|
|
return info;
|
|
}
|
|
|
|
private static String getIn(String row2) {
|
|
String str = "";
|
|
switch (row2) {
|
|
case "bool":
|
|
str = "false";
|
|
break;
|
|
case "int":
|
|
case "float":
|
|
str = "0";
|
|
break;
|
|
case "string":
|
|
break;
|
|
default:
|
|
str = null;
|
|
}
|
|
return str;
|
|
}
|
|
|
|
private static void bulidMapInfo(Workbook wb) throws IOException {
|
|
Sheet sheet = wb.getSheetAt(0);
|
|
String sheetName = sheet.getSheetName();
|
|
System.out.println("bulidMapInfo => collection name="+sheetName);
|
|
int rowNum = sheet.getPhysicalNumberOfRows();
|
|
FileWriter fw = new FileWriter(path + sheetName + ".txt");
|
|
PrintWriter out = new PrintWriter(fw);
|
|
Object[][] aa = new Object[rowNum][];
|
|
int physicalNumberOfCells=sheet.getRow(0).getPhysicalNumberOfCells();
|
|
for (int i = 0; i < rowNum; i++) {
|
|
Row row = sheet.getRow(i);
|
|
Object[] bb = new Object[physicalNumberOfCells];
|
|
for (int j = 0; j < physicalNumberOfCells; j++) {
|
|
//文件名
|
|
Object cellData = getCellFormatValue(row.getCell(j),null);
|
|
if (!"".equals(cellData)) {
|
|
bb[j] = cellData;
|
|
}
|
|
}
|
|
aa[i] = bb;
|
|
}
|
|
Map<Object, List<Object>> infoMap = getObjectListMap(aa,sheetName);
|
|
buildJson(out, infoMap);
|
|
fw.close();
|
|
out.close();
|
|
}
|
|
|
|
private static Map<Object, List<Object>> getObjectListMap(Object[][] aa,String sheetName) throws IOException {
|
|
Map<Object, List<Object>> 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<Object> 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<Object, List<Object>> infoMap) {
|
|
int i = 0;
|
|
for (Map.Entry<Object, List<Object>> entry : infoMap.entrySet()) {
|
|
String[] split = entry.getKey().toString().split("#");
|
|
Object npc = entry.getKey();
|
|
Object isMustAppear = 0;
|
|
if (split.length >= 3){
|
|
npc = split[1];
|
|
isMustAppear = split[2];
|
|
}
|
|
List<Object> 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\tisMustAppear");
|
|
out.write("\r\n");
|
|
out.write("int"+"\t"+"int"+"\t"+"mut,int#int,2"+"\t"+"int");
|
|
out.write("\r\n");
|
|
}
|
|
i = i +1 ;
|
|
out.write( i+"\t"+npc+"\t"+groups.toString()+"\t" + isMustAppear);
|
|
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,Cell cellType) {
|
|
Object cellValue = null;
|
|
if (cell != null ) {
|
|
//判断cell类型
|
|
switch (cell.getCellType()) {
|
|
case Cell.CELL_TYPE_NUMERIC: {
|
|
if (cellType != null && cellType.toString().contains("float")){
|
|
cellValue = cell.getNumericCellValue();
|
|
}else{
|
|
cellValue = (int) cell.getNumericCellValue();
|
|
}
|
|
break;
|
|
}
|
|
case Cell.CELL_TYPE_FORMULA: {
|
|
//判断cell是否为日期格式
|
|
cellValue = ((XSSFCell) cell).getRawValue();
|
|
// if (DateUtil.isCellDateFormatted(cell)) {
|
|
// //转换为日期格式YYYY-mm-dd
|
|
// cellValue = cell.getDateCellValue();
|
|
// } else {
|
|
// //数字
|
|
// cellValue = String.valueOf(cell.getNumericCellValue());
|
|
// if (cellType != null && cellType.toString().equals("float")){
|
|
//
|
|
// }else{
|
|
// if (cellType.toString().equals("string")){
|
|
// cellValue = "";
|
|
// }else{
|
|
// cellValue = (int) cell.getNumericCellValue();
|
|
// }
|
|
// }
|
|
// }
|
|
break;
|
|
}
|
|
case Cell.CELL_TYPE_BOOLEAN:{
|
|
cellValue =cell.getBooleanCellValue();
|
|
break;
|
|
}
|
|
case Cell.CELL_TYPE_STRING: {
|
|
cellValue = cell.getRichStringCellValue().getString();
|
|
break;
|
|
}
|
|
default:
|
|
cellValue = "";
|
|
}
|
|
} else {
|
|
cellValue = "";
|
|
}
|
|
return cellValue;
|
|
}
|
|
} |