public override bool DealRow(ExcelData excelData, int rowIndex) { if (rowIndex > 2) { string JsonLine = "{"; //一行的json数据 List <string> rowList = excelData.GetRowData(rowIndex); int rowListCount = rowList.Count; int arrayMark = 0; List <TableArrayElementInfo> arrayList = new List <TableArrayElementInfo>(); for (int i = 0; i < rowListCount; i++) { string wordName = excelData.GetWordName(i); string wordType = excelData.GetTypeName(i); string cellStr = rowList[i]; //string wordArrayName = null; if (wordName.Contains(":")) { wordName = wordName.Split(':')[0]; if (arrayMark == 0) { arrayMark = 1; arrayList.Clear(); } } if (i + 1 < rowListCount) //预判下一个值(如果不是最后一个) { string nextCellWordName = excelData.GetWordName(i + 1); //字段名称 if (nextCellWordName.Contains(":")) { string nextWordArrayName = nextCellWordName.Split(':')[0]; if (!nextWordArrayName.Equals(wordName)) //如果下一个单元格和当前单元格不是一个数组里的 { if (arrayMark == 1) { arrayMark = 2; } } } else { if (arrayMark == 1) { arrayMark = 2; } } } else { //如果已经是最后一个了 if (arrayMark == 1) { arrayMark = 2; } } if (arrayMark == 0) //普通单元格 { cellStr = GetSingleCellString(wordType, cellStr); string jsonCellStr = string.Format(Template_Server_Java_Json.Template_JsonCell, wordName, cellStr); JsonLine += jsonCellStr; if (i + 1 < rowListCount) { JsonLine += ","; } } else if (arrayMark == 1) //同一数组添加 { arrayList.Add(new TableArrayElementInfo(wordName, wordType, cellStr, excelData.Name)); } else if (arrayMark == 2) //整个数组解析成代码 { arrayList.Add(new TableArrayElementInfo(wordName, wordType, cellStr, excelData.Name)); string cellParam = GetCellArrayString(arrayList); string jsonCellStr = string.Format(Template_Server_Java_Json.Template_JsonCell, wordName, cellParam); JsonLine = JsonLine + jsonCellStr; if (i + 1 < rowListCount) { JsonLine += ","; } arrayMark = 0; } } //Console.WriteLine(excelData.GetAllRowCount().ToString() + " " + (rowIndex + 1).ToString()); if (excelData.GetAllRowCount() == rowIndex + 1) { JsonLine += "}"; jsonContentStr += JsonLine; } else { JsonLine += "},"; jsonContentStr += JsonLine + "\n"; } } return(true); }
public override void Execute(ExcelData excelData, System.Action <ExcelData, bool> onFinished) { string singleCode1 = ""; string singleCode2 = ""; List <string> rowList = excelData.GetRowData(0); List <string> desList = excelData.GetRowData(2); string lastWordName = ""; for (int i = 0; i < rowList.Count; i++) { string typeName = excelData.GetTypeName(i); string wordName = excelData.GetWordName(i); string desStr = desList[i]; if (string.IsNullOrEmpty(desStr)) { desStr = "该字段未添加注释"; } bool isArray = false; if (wordName.Contains(":")) { wordName = wordName.Split(':')[0]; isArray = true; } if (!lastWordName.Equals(wordName)) { if (typeName == "string") { typeName = "String"; } if (typeName == "bool") { typeName = "boolean"; } if (isArray) { if (typeName == "int") { typeName = "List<Integer>"; } else { typeName = string.Format(@"List<{0}>", typeName); } } singleCode1 += string.Format(Template_Server_Java_Json.Template_SingleTableCodeWord, wordName, typeName, wordName); singleCode2 += string.Format(Template_Server_Java_Json.Template_SingleTableCodeWordGet, desStr, typeName, wordName, wordName); lastWordName = wordName; } } string codeFileContent = string.Format(Template_Server_Java_Json.Template_SingleTableCode, excelData.Name, singleCode1, singleCode2); string JsonFilePath = System.IO.Path.Combine(Setting.GenJavaServerJsonCodePath, "Table" + excelData.Name + ".java"); if (Tools.GenerateFile(codeFileContent, JsonFilePath)) { Debug.Log("[Generate Sucess]:" + JsonFilePath); if (onFinished != null) { onFinished(excelData, true); } } if (onFinished != null) { onFinished(excelData, true); } }