Exemplo n.º 1
0
    static void GenerateModel()
    {
        string            path = Application.dataPath + "/Test/Test4.xlsx";
        Excel             xls  = ExcelHelper.LoadExcel(path);
        ExcelDeserializer ed   = new ExcelDeserializer();

        ed.FieldNameLine  = 1;
        ed.FieldTypeLine  = 2;
        ed.FieldValueLine = 3;
        ed.IgnoreSymbol   = "#";
        ed.ModelPath      = Application.dataPath + "/Editor/Excel4Unity/DataItem.txt";
        ed.GenerateCS(xls.Tables[0]);
    }
Exemplo n.º 2
0
        internal static void Main()
        {
            // Test connection to SQL Server
            Database.SetInitializer(new MigrateDatabaseToLatestVersion <AirportSystemMsSqlDbContext, ConfigurationMSSql>());
            using (AirportSystemMsSqlDbContext db = new AirportSystemMsSqlDbContext())
            {
                db.Database.CreateIfNotExists();

                foreach (var e in db.FlightTypes)
                {
                    Console.WriteLine(e.Name);
                }
            }

            // Test Deserializers
            var xmlSer   = new XmlDeserializer();
            var jsonSer  = new JsonDeserializer();
            var excelSer = new ExcelDeserializer();

            var xmlPath   = "../../../SampleInputFiles/sample.xml";
            var jsonPath  = "../../../SampleInputFiles/sample.json";
            var excelPath = "../../../SampleInputFiles/sample.xlsx";


            // Test repository

            var msSqlData  = new AirportSystemMsSqlData(new AirportSystemMsSqlDbContext());
            var pSqlData   = new AirportSystemPSqlData(new AirportSystemPSqlDbContext());
            var sqliteData = new AirportSystemSqliteData(new AirportSystemSqliteDbContext());

            msSqlData.Airports.Add(new Airport
            {
                Code = "LBWN",
                Name = "Varna Airport"
            });

            msSqlData.Airports.Add(new Airport
            {
                Code = "LBSF",
                Name = "Sofia Airport"
            });

            msSqlData.Airlines.Add(new Airline
            {
                Name = "Bongo Air"
            });

            Console.WriteLine();
            Console.WriteLine("Airports:");
            Console.WriteLine("==========");

            foreach (var entity in msSqlData.Airports.GetAll(null))
            {
                Console.WriteLine("{0} - {1}", entity.Code, entity.Name);
            }

            Console.WriteLine("Airlines:");
            Console.WriteLine("==========");
            foreach (var entity in msSqlData.Airlines.GetAll(null))
            {
                Console.WriteLine("{0}", entity.Name);
            }

            // Test shedule updater
            var su         = new ScheduleUpdater(msSqlData, pSqlData, sqliteData);
            int countAdded = 0;

            countAdded = su.UpdateScheduleFromFile(xmlPath, xmlSer);
            Console.WriteLine("{0} FLIGHTS ADDDED!!!", countAdded);
            countAdded = su.UpdateScheduleFromFile(jsonPath, jsonSer);
            Console.WriteLine("{0} FLIGHTS ADDDED!!!", countAdded);
            countAdded = su.UpdateScheduleFromFile(excelPath, excelSer);
            Console.WriteLine("{0} FLIGHTS ADDDED!!!", countAdded);
            Console.WriteLine();

            var f          = new FlightRepository(new AirportSystemMsSqlDbContext());
            var allFlights = f.GetAll(null);

            foreach (var item in allFlights)
            {
                var fl = (Flight)item;
                Console.WriteLine("{0} - {1} - {2} - {3} - {4} - {5} - {6} - {7} - {8} - {9} - {10} - {11}",
                                  fl.DestinationAirport.Name,
                                  fl.DestinationAirport.Code,
                                  fl.FlightType.Name,
                                  fl.Plane.Manufacturers.Name,
                                  fl.Terminal.Name,
                                  fl.Plane.Airlines.Name,
                                  fl.Plane.PlanePassport.RegistrationNumber,
                                  fl.Plane.PlanePassport.YearOfRegistration,
                                  fl.Plane.PlanePassport.State,
                                  fl.SheduledTime,
                                  fl.Plane.Models.Name,
                                  fl.Plane.Models.Seats);
            }

            var filteredFlights = f.GetAll(x => x.DestinationAirportId == 3);

            Console.WriteLine(filteredFlights.Count());
        }
Exemplo n.º 3
0
    public static string ParseFile(string path, bool createCS = true, bool isMac = false)
    {
        //		UnityEngine.Debug.LogError ("path " + path);
        if (!path.EndsWith("xlsx"))
        {
            return(null);
        }

        string tableName       = "";
        string currentPropName = "";
        int    tableRow        = 0;
        int    tableColumn     = 0;
        string v     = "";
        Excel  excel = null;

        excel = ExcelHelper.LoadExcel(path);
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            JsonWriter writer            = new JsonWriter(sb);
            writer.WriteObjectStart();
            foreach (ExcelTable table in excel.Tables)
            {
                tableName = table.TableName;
                bool language = tableName.ToLower().Contains("language");
                if (table.TableName.StartsWith("#"))
                {
                    continue;
                }
                if (createCS)
                {
                    ExcelDeserializer ed = new ExcelDeserializer();
                    ed.FieldNameLine  = 1;
                    ed.FieldTypeLine  = 2;
                    ed.FieldValueLine = 3;
                    ed.IgnoreSymbol   = "#";
                    ed.ModelPath      = Application.dataPath + "/Editor/Excel4Unity/DataItem.txt";
                    ed.GenerateCS(table);
                }
                writer.WritePropertyName(table.TableName);
                writer.WriteArrayStart();
                for (int i = 4; i <= table.NumberOfRows; i++)
                {
                    tableRow = i;
                    string idStr = table.GetValue(i, 1).ToString();
                    if (idStr.Length <= 0)
                    {
                        //						UnityEngine.Debug.LogError ("ID error:" + tableName + "  (第" + i + "行)");
                        break;
                    }
                    writer.WriteObjectStart();

                    for (int j = 1; j <= table.NumberOfColumns; j++)
                    {
                        tableColumn = j;
                        string propName = table.GetValue(1, j).ToString();
                        string propType = table.GetValue(3, j).ToString();
                        propName        = propName.Replace("*", "");
                        currentPropName = propName;

                        if (propName.StartsWith("#"))
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(propName) || string.IsNullOrEmpty(propType))
                        {
                            continue;
                        }
                        writer.WritePropertyName(propName);
                        v = table.GetValue(i, j).ToString();
                        if (propType.Equals("int"))
                        {
                            int value = v.Length > 0 ? int.Parse(v) : 0;
                            writer.Write(value);
                        }
                        else if (propType.Equals("bool"))
                        {
                            int value = v.Length > 0 ? int.Parse(v) : 0;
                            writer.Write(value);
                        }
                        else if (propType.Equals("float"))
                        {
                            float value = v.Length > 0 ? float.Parse(v) : 0;
                            writer.Write(value);
                        }
                        else
                        {
                            string ss = table.GetValue(i, j).ToString();
                            if (language && ss.Contains(" "))
                            {
                                ss = ss.Replace(" ", "\u00A0");
                            }
                            writer.Write(ss);
                        }
                    }
                    writer.WriteObjectEnd();
                }
                writer.WriteArrayEnd();
            }
            writer.WriteObjectEnd();
            string outputDir  = Application.dataPath + "/Resources/DataFiles/";
            string outputPath = outputDir + Path.GetFileNameWithoutExtension(path) + ".txt";
            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }
            string str = string.Empty;
            if (File.Exists(path))
            {
                byte[]       bytes    = File.ReadAllBytes(path);
                UTF8Encoding encoding = new UTF8Encoding();
                str = encoding.GetString(bytes);
            }
            string content = sb.ToString();
            if (str != content)
            {
                File.WriteAllText(outputPath, content);
            }
            Debug.Log("convert success! path = " + path);

            return(sb.ToString());
        }
        catch (System.Exception e)
        {
            if (excel == null)
            {
                //                EditorUtility.DisplayDialog("ERROR!", "open excel failed!","ok");
                Debug.LogError("open excel failed!");
                Debug.LogError(e.StackTrace);
            }
            else
            {
                string msg = "解析错误! \n表:" + tableName + " \n字段:" + currentPropName + "  \n第" + tableRow + "行,第" + tableColumn + "列 \nvalue = " + v;
                EditorUtility.DisplayDialog("error!", msg, "ok");
                Debug.LogError(e);
                Debug.LogError(e.StackTrace);
                Debug.LogError(msg);
            }
            return(null);
        }
    }