Exemplo n.º 1
0
        private bool Read_Single_Excel(string path)
        {
            try
            {
                ExcelData    excelData   = new ExcelData(path);
                FileInfo     fileInfo    = new System.IO.FileInfo(path);
                XSSFWorkbook workbook    = new XSSFWorkbook(fileInfo);
                ISheet       sheet       = workbook.GetSheetAt(0);
                int          columnCount = 0; //列数
                for (int i = 0; i < 10000; i++)
                {
                    IRow row = sheet.GetRow(i);
                    if (row != null)
                    {
                        List <string> rowList = new List <string>();
                        for (int j = 0; j < 100; j++)
                        {
                            ICell  cell    = row.GetCell(j);
                            string cellStr = "";
                            if (cell == null)
                            {
                                if (columnCount == 0)
                                {
                                    columnCount = rowList.Count;
                                    break;
                                }
                                else if (j < columnCount)
                                {
                                    if (i > 2)//策划没有配置,给预一个默认值
                                    {
                                        string cellType = excelData.GetTypeName(j);
                                        if (cellType == "string")
                                        {
                                            cellStr = "";
                                        }
                                        else if (cellType == "bool")
                                        {
                                            cellStr = "FLASE";
                                        }
                                        else
                                        {
                                            cellStr = "0";
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (string.IsNullOrEmpty(cellStr) && cell != null)
                            {
                                if (cell.CellType != CellType.Formula)  //如果不是公式
                                {
                                    cellStr = cell.ToString().Trim();
                                }
                                else
                                {
                                    if (cell.CachedFormulaResultType == CellType.Numeric)
                                    {
                                        cellStr = cell.NumericCellValue.ToString();
                                    }
                                    else if (cell.CachedFormulaResultType == CellType.String)
                                    {
                                        cellStr = cell.StringCellValue.Trim();
                                    }
                                    else if (cell.CachedFormulaResultType == CellType.Boolean)
                                    {
                                        cellStr = cell.BooleanCellValue.ToString();
                                    }
                                }
                            }

                            if (i == 0)
                            {
                                cellStr = cellStr[0].ToString().ToUpper() + cellStr.Substring(1); //字段首字母必须大写,应该flat会解析成首字母大写
                            }
                            if (i > 2 && j == 0 && cellStr.StartsWith("#"))                       //如果从第三行开始第一个单元格里的是#,则本行不读
                            {
                                break;
                            }
                            else
                            {
                                rowList.Add(cellStr);
                            }
                        }
                        if (rowList.Count > 0)
                        {
                            excelData.AddRowData(rowList);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                AddDelayRun(() =>
                {
                    SchemeType scheme = Setting.GetCurSchemeType();
                    if (scheme == SchemeType.Client_CSharp_To_FlatBuffer)
                    {
                        Proceduree_GenerateFbs(excelData);
                    }
                    else if (scheme == SchemeType.Server_Java_To_Json)
                    {
                        Procedure_OnDealSingleExcel(excelData);
                    }
                });
            }
            catch (Exception error)
            {
                Debug.Log(error);
                return(true);
            }
            return(true);
        }
        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);
            }
        }