static void ExportExcelToAssetbundle()
    {
        foreach (Object obj in Selection.objects)
        {
            var window = ScriptableObject.CreateInstance <ExcelProcesser>();
            window.filePath = AssetDatabase.GetAssetPath(obj);
            window.fileName = Path.GetFileNameWithoutExtension(window.filePath);
            using (FileStream stream = File.Open(window.filePath, FileMode.Open, FileAccess.Read))
            {
                StreamReader sr       = new StreamReader(stream);
                string       line1    = sr.ReadLine();
                string[]     comments = line1.Split('\t'); //comment
                string       line2    = sr.ReadLine();
                string[]     types    = line2.Split('\t'); //type
                foreach (var item in types)
                {
                    item.Trim('\n');
                }
                string   line3 = sr.ReadLine();
                string[] names = line3.Split('\t');//name

                string   data      = sr.ReadLine();
                string[] dataSplit = data.Split('\t');
                for (int i = 0; i < names.Length; i++)
                {
                    ExcelRowParameter parser = new ExcelRowParameter();
                    parser.comment = comments[i];
                    parser.type    = types[i];
                    //bool isArray = dataSplit[i].Split('|').Length > 1;
                    //bool isFloat = dataSplit[i].Contains('.');
                    //switch (parser.type)
                    //{
                    //    case "int"://策划有可能填错
                    //        if (isArray && isFloat)
                    //            parser.type = "float[]";
                    //        else if (isArray)
                    //            parser.type = "int[]";
                    //        else if (isFloat)
                    //            parser.type = "float";
                    //        break;
                    //    case "float":
                    //        if (isArray)
                    //            parser.type = "float[]";
                    //        break;
                    //    case "string":
                    //        if (isArray)
                    //            parser.type = "string[]";
                    //        break;
                    //}
                    parser.name = names[i];

                    window.typeList.Add(parser);
                }
            }

            window.Show();
        }
    }
    static void ExportExcelToAssetbundle()
    {
        foreach (Object obj in Selection.objects)
        {
            var window = ScriptableObject.CreateInstance <ExcelImporterMaker>();
            window.filePath = AssetDatabase.GetAssetPath(obj);
            window.fileName = Path.GetFileNameWithoutExtension(window.filePath);


            using (FileStream stream = File.Open(window.filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                IWorkbook book = null;
                if (Path.GetExtension(window.filePath) == ".xls")
                {
                    book = new HSSFWorkbook(stream);
                }
                else
                {
                    book = new XSSFWorkbook(stream);
                }

                for (int i = 0; i < book.NumberOfSheets; ++i)
                {
                    ISheet s = book.GetSheetAt(i);
                    ExcelSheetParameter sht = new ExcelSheetParameter();
                    sht.sheetName = s.SheetName;
                    sht.isEnable  = EditorPrefs.GetBool(s_key_prefix + window.fileName + ".sheet." + sht.sheetName, true);
                    window.sheetList.Add(sht);
                }

                ISheet sheet = book.GetSheetAt(0);

                window.className = EditorPrefs.GetString(s_key_prefix + window.fileName + ".className", "Entity_" + sheet.SheetName);

                window.sepalateSheet = EditorPrefs.GetBool(s_key_prefix + window.fileName + ".separateSheet");

                IRow titleRow = sheet.GetRow(0);
                IRow dataRow  = sheet.GetRow(1);
                for (int i = 0; i < titleRow.LastCellNum; i++)
                {
                    ExcelRowParameter lastParser = null;
                    ExcelRowParameter parser     = new ExcelRowParameter();
                    parser.name    = titleRow.GetCell(i).StringCellValue;
                    parser.isArray = parser.name.Contains("[]");
                    if (parser.isArray)
                    {
                        parser.name = parser.name.Remove(parser.name.LastIndexOf("[]"));
                    }

                    ICell cell = dataRow.GetCell(i);

                    // array support
                    if (window.typeList.Count > 0)
                    {
                        lastParser = window.typeList [window.typeList.Count - 1];
                        if (lastParser.isArray && parser.isArray && lastParser.name.Equals(parser.name))
                        {
                            // trailing array items must be the same as the top type
                            parser.isEnable          = lastParser.isEnable;
                            parser.type              = lastParser.type;
                            lastParser.nextArrayItem = parser;
                            window.typeList.Add(parser);
                            continue;
                        }
                    }

                    if (cell.CellType != CellType.Unknown && cell.CellType != CellType.Blank)
                    {
                        parser.isEnable = true;

                        try
                        {
                            if (EditorPrefs.HasKey(s_key_prefix + window.fileName + ".type." + parser.name))
                            {
                                parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + window.fileName + ".type." + parser.name);
                            }
                            else
                            {
                                string sampling = cell.StringCellValue;
                                parser.type = ValueType.STRING;
                            }
                        } catch
                        {
                        }
                        try
                        {
                            if (EditorPrefs.HasKey(s_key_prefix + window.fileName + ".type." + parser.name))
                            {
                                parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + window.fileName + ".type." + parser.name);
                            }
                            else
                            {
                                double sampling = cell.NumericCellValue;
                                parser.type = ValueType.DOUBLE;
                            }
                        } catch
                        {
                        }
                        try
                        {
                            if (EditorPrefs.HasKey(s_key_prefix + window.fileName + ".type." + parser.name))
                            {
                                parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + window.fileName + ".type." + parser.name);
                            }
                            else
                            {
                                bool sampling = cell.BooleanCellValue;
                                parser.type = ValueType.BOOL;
                            }
                        } catch
                        {
                        }
                    }

                    window.typeList.Add(parser);
                }

                window.Show();
            }
        }
    }
    void ExportImporter()
    {
        string templateFilePath = (sepalateSheet) ? "Assets/Terasurware/Editor/ExportTemplate2.txt" : "Assets/Terasurware/Editor/ExportTemplate.txt";

        string importerTemplate = File.ReadAllText(templateFilePath);

        StringBuilder builder          = new StringBuilder();
        StringBuilder sheetListbuilder = new StringBuilder();
        int           rowCount         = 0;
        string        tab = "					";
        bool          isInbetweenArray = false;

        //public string[] sheetNames = {"hoge", "fuga"};
        //$SheetList$
        foreach (ExcelSheetParameter sht in sheetList)
        {
            if (sht.isEnable)
            {
                sheetListbuilder.Append("\"" + sht.sheetName + "\",");
            }

            /*
             * if (sht != sheetList [sheetList.Count - 1])
             * {
             *  sheetListbuilder.Append(",");
             * }
             */
        }

        foreach (ExcelRowParameter row in typeList)
        {
            if (row.isEnable)
            {
                if (!row.isArray)
                {
                    builder.AppendLine();
                    switch (row.type)
                    {
                    case ValueType.BOOL:
                        builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0} = (cell == null ? false : cell.BooleanCellValue);", row.name, rowCount);
                        break;

                    case ValueType.DOUBLE:
                        builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0} = (cell == null ? 0.0 : cell.NumericCellValue);", row.name, rowCount);
                        break;

                    case ValueType.INT:
                        builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0} = (int)(cell == null ? 0 : cell.NumericCellValue);", row.name, rowCount);
                        break;

                    case ValueType.FLOAT:
                        builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0} = (float)(cell == null ? 0 : cell.NumericCellValue);", row.name, rowCount);
                        break;

                    case ValueType.STRING:
                        builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0} = (cell == null ? \"\" : cell.StringCellValue);", row.name, rowCount);
                        break;
                    }
                }
                else
                {
                    // only the head of array should generate code

                    if (!isInbetweenArray)
                    {
                        int arrayLength = 0;
                        for (ExcelRowParameter r = row; r != null; r = r.nextArrayItem, ++arrayLength)
                        {
                        }

                        builder.AppendLine();
                        switch (row.type)
                        {
                        case ValueType.BOOL:
                            builder.AppendFormat(tab + "p.{0} = new bool[{1}];", row.name, arrayLength);
                            break;

                        case ValueType.DOUBLE:
                            builder.AppendFormat(tab + "p.{0} = new double[{1}];", row.name, arrayLength);
                            break;

                        case ValueType.INT:
                            builder.AppendFormat(tab + "p.{0} = new int[{1}];", row.name, arrayLength);
                            break;

                        case ValueType.FLOAT:
                            builder.AppendFormat(tab + "p.{0} = new float[{1}];", row.name, arrayLength);
                            break;

                        case ValueType.STRING:
                            builder.AppendFormat(tab + "p.{0} = new string[{1}];", row.name, arrayLength);
                            break;
                        }

                        for (int i = 0; i < arrayLength; ++i)
                        {
                            builder.AppendLine();
                            switch (row.type)
                            {
                            case ValueType.BOOL:
                                builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0}[{2}] = (cell == null ? false : cell.BooleanCellValue);", row.name, rowCount + i, i);
                                break;

                            case ValueType.DOUBLE:
                                builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0}[{2}] = (cell == null ? 0.0 : cell.NumericCellValue);", row.name, rowCount + i, i);
                                break;

                            case ValueType.INT:
                                builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0}[{2}] = (int)(cell == null ? 0 : cell.NumericCellValue);", row.name, rowCount + i, i);
                                break;

                            case ValueType.FLOAT:
                                builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0}[{2}] = (float)(cell == null ? 0.0 : cell.NumericCellValue);", row.name, rowCount + i, i);
                                break;

                            case ValueType.STRING:
                                builder.AppendFormat(tab + "cell = row.GetCell({1}); p.{0}[{2}] = (cell == null ? \"\" : cell.StringCellValue);", row.name, rowCount + i, i);
                                break;
                            }
                        }
                    }
                    isInbetweenArray = (row.nextArrayItem != null);
                }
            }
            rowCount += 1;
        }

        importerTemplate = importerTemplate.Replace("$IMPORT_PATH$", filePath);
        importerTemplate = importerTemplate.Replace("$ExportAssetDirectry$", Path.GetDirectoryName(filePath));
        importerTemplate = importerTemplate.Replace("$EXPORT_PATH$", Path.ChangeExtension(filePath, ".asset"));
        importerTemplate = importerTemplate.Replace("$ExcelData$", className);
        importerTemplate = importerTemplate.Replace("$SheetList$", sheetListbuilder.ToString());
        importerTemplate = importerTemplate.Replace("$EXPORT_DATA$", builder.ToString());
        importerTemplate = importerTemplate.Replace("$ExportTemplate$", fileName + "_importer");

        Directory.CreateDirectory("Assets/Terasurware/Classes/Editor/");
        File.WriteAllText("Assets/Terasurware/Classes/Editor/" + fileName + "_importer.cs", importerTemplate);
    }
	static void ExportExcelToAssetbundle ()
	{
		foreach (Object obj in Selection.objects) {
			
		
			var window = ScriptableObject.CreateInstance<ExcelImporterMaker> ();
			window.filePath = AssetDatabase.GetAssetPath (obj);
			window.fileName = Path.GetFileNameWithoutExtension (window.filePath);
		
		
			using (FileStream stream = File.Open (window.filePath, FileMode.Open, FileAccess.Read)) {
			
				IWorkbook book = new HSSFWorkbook (stream);

				for(int i = 0; i < book.NumberOfSheets; ++i) {
					ISheet s = book.GetSheetAt (i);
					ExcelSheetParameter sht = new ExcelSheetParameter ();
					sht.sheetName = s.SheetName;
					sht.isEnable  = EditorPrefs.GetBool(s_key_prefix + window.fileName + ".sheet."+ sht.sheetName, true);
					window.sheetList.Add( sht );
				}
			
				ISheet sheet = book.GetSheetAt (0);

				window.className = EditorPrefs.GetString (s_key_prefix + window.fileName + ".className", "Entity_" + sheet.SheetName);

				IRow titleRow = sheet.GetRow (0);
				IRow dataRow = sheet.GetRow (1);
				for (int i=0; i < titleRow.LastCellNum; i++) {
					ExcelRowParameter lastParser = null;
					ExcelRowParameter parser = new ExcelRowParameter ();
					parser.name = titleRow.GetCell (i).StringCellValue;
					parser.isArray = parser.name.Contains("[]");
					if( parser.isArray ) {
						parser.name = parser.name.Remove(parser.name.LastIndexOf("[]"));
					}

					ICell cell = dataRow.GetCell (i);

//					if(cell == null) {
//						continue;
//					}

					// array support
					if( window.typeList.Count > 0 ) {
						lastParser = window.typeList[window.typeList.Count-1];
						if( lastParser.isArray && parser.isArray && lastParser.name.Equals( parser.name ) ) {
							// trailing array items must be the same as the top type
							parser.isEnable = lastParser.isEnable;
							parser.type = lastParser.type;
							lastParser.nextArrayItem = parser;
							window.typeList.Add (parser);
							continue;
						}
					}
				
					if (cell.CellType != CellType.Unknown && cell.CellType != CellType.BLANK) {
						parser.isEnable = true;

						try {
							if(EditorPrefs.HasKey(s_key_prefix + window.fileName + ".type."+ parser.name)) {
								parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + window.fileName + ".type."+ parser.name);
							} else {
								string sampling = cell.StringCellValue;
								parser.type = ValueType.STRING;
							}
						} catch {
						}
						try {
							if(EditorPrefs.HasKey(s_key_prefix + window.fileName + ".type."+ parser.name)) {
								parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + window.fileName + ".type."+ parser.name);
							} else {
								double sampling = cell.NumericCellValue;
								parser.type = ValueType.DOUBLE;
							}
						} catch {
						}
						try {
							if(EditorPrefs.HasKey(s_key_prefix + window.fileName + ".type."+ parser.name)) {
								parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + window.fileName + ".type."+ parser.name);
							} else {
								bool sampling = cell.BooleanCellValue;
								parser.type = ValueType.BOOL;
							}
						} catch {
						}
					}
				
					window.typeList.Add (parser);
				}
			
				window.Show ();
			}
		}
	}
Exemplo n.º 5
0
    /// <summary>
    /// Setups the typelist.
    /// </summary>
    /// <param name="sheetCount">Sheet count.</param>
    /// <param name="book">Book.</param>
    void SetupTypelist(int sheetCount)
    {
        typeList.Clear();
        ISheet sheet = book.GetSheetAt(sheetCount);

        className = EditorPrefs.GetString(s_key_prefix + fileName + sheet.SheetName + ".className", "Entity_" + sheet.SheetName);

        sepalateSheet = EditorPrefs.GetBool(s_key_prefix + fileName + ".separateSheet");

        IRow titleRow = sheet.GetRow(0);
        IRow dataRow  = sheet.GetRow(1);

        for (int i = 0; i < titleRow.LastCellNum; i++)
        {
            ExcelRowParameter lastParser = null;
            ExcelRowParameter parser     = new ExcelRowParameter();
            parser.name    = titleRow.GetCell(i).StringCellValue;
            parser.isArray = parser.name.Contains("[]");
            if (parser.isArray)
            {
                parser.name = parser.name.Remove(parser.name.LastIndexOf("[]"));
            }

            ICell cell = dataRow.GetCell(i);

            // array support
            if (typeList.Count > 0)
            {
                lastParser = typeList [typeList.Count - 1];
                if (lastParser.isArray && parser.isArray && lastParser.name.Equals(parser.name))
                {
                    // trailing array items must be the same as the top type
                    parser.isEnable          = lastParser.isEnable;
                    parser.type              = lastParser.type;
                    lastParser.nextArrayItem = parser;
                    typeList.Add(parser);
                    continue;
                }
            }

            if (cell.CellType != CellType.Unknown && cell.CellType != CellType.BLANK)
            {
                parser.isEnable = true;

                try {
                    if (EditorPrefs.HasKey(s_key_prefix + fileName + ".type." + parser.name))
                    {
                        parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + fileName + ".type." + parser.name);
                    }
                    else
                    {
                        string sampling = cell.StringCellValue;
                        parser.type = ValueType.STRING;
                    }
                } catch {}
                try {
                    if (EditorPrefs.HasKey(s_key_prefix + fileName + ".type." + parser.name))
                    {
                        parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + fileName + ".type." + parser.name);
                    }
                    else
                    {
                        double sampling = cell.NumericCellValue;
                        parser.type = ValueType.DOUBLE;
                    }
                } catch {}
                try {
                    if (EditorPrefs.HasKey(s_key_prefix + fileName + ".type." + parser.name))
                    {
                        parser.type = (ValueType)EditorPrefs.GetInt(s_key_prefix + fileName + ".type." + parser.name);
                    }
                    else
                    {
                        bool sampling = cell.BooleanCellValue;
                        parser.type = ValueType.BOOL;
                    }
                } catch {
                }
            }

            typeList.Add(parser);
        }
    }