Example #1
0
        /// <summary>
        /// excel导出sqlite
        /// 需要主动连接数据库
        /// </summary>
        /// <param name="filePath"></param>
        public static void Excel2SQLite(string filePath, DBType dbType)
        {
            var excel = new ExcelUtility(filePath);
            var json  = excel.GetJson(dbType);

            Json2Sqlite(filePath, json);
        }
        public static void GenSQLite(string outPath)
        {
            var tablePath = IPath.Combine(Application.dataPath, "Resource/Table/");
            var xlslFiles = Directory.GetFiles(tablePath, "*.xlsx", SearchOption.AllDirectories);

            //
            if (Directory.Exists(outPath) == false)
            {
                Directory.CreateDirectory(outPath);
            }

            var _path = IPath.Combine(outPath, "Local.db");

            //
            sql = new SQLiteService(SqliteLoder.CreateConnetion(_path));
            foreach (var f in xlslFiles)
            {
                var excel = new ExcelUtility(f);
                var json  = excel.GetJson();
                Json2Sqlite(f, json);
            }

            sql.Close();
            EditorUtility.ClearProgressBar();
            Debug.Log("导出Sqlite完成!");
            AssetDatabase.Refresh();
        }
Example #3
0
        /// <summary>
        /// 通过excel生成class
        /// </summary>
        /// <param name="filename"></param>
        static private void GenClassByExcel(string filename)
        {
            var           excel      = new ExcelUtility(filename);
            string        json       = excel.GetJson();
            List <object> statements = excel.GetLine(0);
            //这里将前三列进行判断
            //如果第二列第一行是"Id",则用自动推测字段模式
            //如果第三列第一行是"Id",则用自定义字段类型模式
            var list  = excel.GetLine(1);
            var list2 = excel.GetLine(2);

            if (list[0].Equals("Id"))
            {
                Json2Class(filename, json, statements, null);
                Debug.Log("[自动分析]导出:" + filename);
            }
            else if (list2[0].Equals("Id"))
            {
                List <object> fieldTypes = excel.GetLine(1);
                Json2Class(filename, json, statements, fieldTypes);
                Debug.Log("[自定义字段]导出:" + filename);
            }
            else
            {
                Debug.LogError("不符合规范内容:" + filename);
            }
        }
Example #4
0
        /// <summary>
        /// 通过excel生成class
        /// </summary>
        /// <param name="excelFilePath"></param>
        static private void GenClassByExcel(string outputDirectory, string excelFilePath, string localOrServer)
        {
            Debug.LogFormat("[{0}]正在生成:" + excelFilePath, localOrServer);
            var           excel         = new ExcelUtility(excelFilePath);
            int           idX           = -1;
            int           idY           = -1;
            List <object> keepFieldList = new List <object>();
            string        json          = excel.GetJson(localOrServer, ref idX, ref idY, ref keepFieldList);

            if (idX != -1 && idY != -1)
            {
                if (idY < 2)
                {
                    Debug.LogErrorFormat("【生成失败】 {0} ,请检查表头预留3行:备注,类型,字段名!", Path.GetFileName(excelFilePath));
                    return;
                }

                //这里将前三列进行判断
                var statements = excel.GetRowDatas(idY - 2);
                var fieldTypes = excel.GetRowDatas(idY - 1);
                if (idX > 0)
                {
                    statements.RemoveRange(0, idX);
                    fieldTypes.RemoveRange(0, idX);
                    if (keepFieldList.Count > 0)
                    {
                        keepFieldList.RemoveRange(0, idX);
                    }
                }

                if (keepFieldList.Count > 0)
                {
                    for (int i = keepFieldList.Count - 1; i >= 0; i--)
                    {
                        if (!keepFieldList[i].Equals("*"))
                        {
                            statements.RemoveAt(i);
                            fieldTypes.RemoveAt(i);
                        }
                    }
                }

                var clsContent = Json2Class(excelFilePath, json, localOrServer, statements, fieldTypes);

                //输出目录控制
                string outputFile = outputDirectory + "/" + localOrServer;
                outputFile = Path.Combine(outputFile, Path.GetFileName(excelFilePath) + ".cs");
                FileHelper.WriteAllText(outputFile, clsContent);


                Debug.LogFormat("<color=red> [{0} 成功] </color>:{1}", localOrServer, excelFilePath);
            }
            else
            {
                Debug.LogError("不符合规范内容:" + excelFilePath);
            }
        }
Example #5
0
        /// <summary>
        /// 转换Excel文件
        /// </summary>
        private static void Convert()
        {
            foreach (string assetsPath in excelList)
            {
                //获取Excel文件的绝对路径
                string excelPath = pathRoot + "/" + assetsPath;
                //构造Excel工具类
                ExcelUtility excel = new ExcelUtility(excelPath);

                //判断编码类型
                Encoding encoding = null;
                if (indexOfEncoding == 0)
                {
                    encoding = Encoding.GetEncoding("utf-8");
                }
                else if (indexOfEncoding == 1)
                {
                    encoding = Encoding.GetEncoding("gb2312");
                }

                //判断输出类型
                string output = "";
                if (indexOfFormat == 0)
                {
                    output = excelPath.Replace(".xlsx", ".json");
                    excel.ConvertToJson(output, encoding);
                }
                else if (indexOfFormat == 1)
                {
                    output = excelPath.Replace(".xlsx", ".csv");
                    excel.ConvertToCSV(output, encoding);
                }
                else if (indexOfFormat == 2)
                {
                    output = excelPath.Replace(".xlsx", ".xml");
                    excel.ConvertToXml(output);
                }

                //判断是否保留源文件
                if (!keepSource)
                {
                    FileUtil.DeleteFileOrDirectory(excelPath);
                }

                //刷新本地资源
                AssetDatabase.Refresh();
            }

            //转换完后关闭插件
            //这样做是为了解决窗口
            //再次点击时路径错误的Bug
            instance.Close();
        }
        /// <summary>
        /// 通过excel生成class
        /// </summary>
        /// <param name="filename"></param>
        static private void GenClassByExcel(string filename, string dbType)
        {
            Debug.LogFormat("[{0}]正在生成:" + filename, dbType);
            var           excel         = new ExcelUtility(filename);
            int           idX           = -1;
            int           idY           = -1;
            List <object> keepFieldList = new List <object>();
            string        json          = excel.GetJson(dbType, ref idX, ref idY, ref keepFieldList);

            if (idX != -1 && idY != -1)
            {
                if (idY < 2)
                {
                    Debug.LogErrorFormat("【生成失败】 {0} ,请检查表头预留3行:备注,类型,字段名!", Path.GetFileName(filename));
                    return;
                }

                //这里将前三列进行判断
                var statements = excel.GetRowDatas(idY - 2);
                var fieldTypes = excel.GetRowDatas(idY - 1);
                if (idX > 0)
                {
                    statements.RemoveRange(0, idX);
                    fieldTypes.RemoveRange(0, idX);
                    if (keepFieldList.Count > 0)
                    {
                        keepFieldList.RemoveRange(0, idX);
                    }
                }

                if (keepFieldList.Count > 0)
                {
                    for (int i = keepFieldList.Count - 1; i >= 0; i--)
                    {
                        if (!keepFieldList[i].Equals("*"))
                        {
                            statements.RemoveAt(i);
                            fieldTypes.RemoveAt(i);
                        }
                    }
                }

                Json2Class(filename, json, dbType, statements, fieldTypes);
                Debug.LogFormat("<color=red> [{0} 成功] </color>:{1}", dbType, filename);
            }
            else
            {
                Debug.LogError("不符合规范内容:" + filename);
            }
        }
        /// <summary>
        /// excel导出sqlite
        /// 需要主动连接数据库
        /// </summary>
        /// <param name="filePath"></param>
        public static void Excel2SQLite(string filePath, DBType dbType)
        {
            var excel = new ExcelUtility(filePath);
            var json  = excel.GetJson(dbType);

            try
            {
                Json2Sqlite(filePath, json);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                EditorUtility.ClearProgressBar();
            }
        }
Example #8
0
        public static void GenCode()
        {
            var           tablePath = Path.Combine(Application.dataPath, "Resource/Table");
            DirectoryInfo info      = new DirectoryInfo(tablePath);

            foreach (var file in info.GetFiles())
            {
                if (!file.FullName.ToLower().EndsWith("xlsx") && !file.FullName.ToLower().EndsWith("xls"))
                {
                    continue;
                }
                string fname = Path.GetFileNameWithoutExtension(file.FullName).ToLower();
                fname = UpperFirst(fname);
                string destPath = Path.GetDirectoryName(file.FullName) + "\\" + fname +
                                  Path.GetExtension(file.FullName);
//                //判断是否重名
                string oldPath = "Assets" + file.FullName.Replace('\\', '/').Replace(Application.dataPath, "");
                string newPath = "Assets" + destPath.Replace('\\', '/').Replace(Application.dataPath, "");
                if (!oldPath.Equals(newPath))
                {
                    AssetDatabase.CopyAsset(oldPath, newPath);
                }
            }


            AssetDatabase.Refresh();

            var tableDir  = Path.GetDirectoryName(tablePath);
            var xlslFiles = Directory.GetFiles(tablePath, "*.xlsx", SearchOption.AllDirectories);

            if (xlslFiles.Length == 0)
            {
                EditorUtility.DisplayDialog("提示", "未发现xlsx文件,请注意不是xls", "确定");
                return;
            }

            foreach (var f in xlslFiles)
            {
                var excel      = new ExcelUtility(f);
                var json       = excel.GetJson();
                var statements = excel.GetLine(0);
                Json2Class(f, json, statements);
                Debug.Log("导出:" + f);
            }

            EditorUtility.DisplayDialog("提示", "生成完成!", "确定");
            AssetDatabase.Refresh();
        }
Example #9
0
        /// <summary>
        /// 生成sqlite
        /// 默认导出到当前平台目录下
        /// </summary>
        /// <param name="root">自定义路径</param>
        public static void GenExcel2SQLite(string root, RuntimePlatform platform)
        {
            var xlslFiles = GetAllConfigFiles();

            //
            SqliteLoder.LoadOnEditor(root, platform);
            {
                foreach (var f in xlslFiles)
                {
                    var excel = new ExcelUtility(f);
                    var json  = excel.GetJson();
                    JsonContent2Sqlite(f, json);
                }
            }
            SqliteLoder.Close();
            //
            EditorUtility.ClearProgressBar();
            Debug.Log("导出Sqlite完成!");
        }