Esempio n. 1
0
        /// <summary>
        /// excel导出sqlite
        /// 需要主动连接数据库
        /// </summary>
        /// <param name="filePath"></param>
        public static void Excel2SQLite(string filePath, DBType dbType)
        {
            filePath = IPath.FormatPathOnUnity3d(filePath); //.Replace("\\", "/").ToLower();
            //收集所有的类型
            CollectTableTypes();
            //
            var excelHash = FileHelper.GetMurmurHash3(filePath);

            //table判断
            SqliteHelper.DB.Connection.CreateTable <ImportExcelLog>();

            var table     = SqliteHelper.DB.GetTable <ImportExcelLog>();
            var importLog = table?.Where((ie) => ie.Path == filePath).FirstOrDefault();

            if (importLog == null || !importLog.Hash.Equals(excelHash))
            {
                //开始导表
                var excel = new ExcelExchangeTools(filePath);
                var json  = excel.GetJson(dbType);
                try
                {
                    Json2Sqlite(filePath, json);
                }
                catch (Exception e)
                {
                    Debug.LogError(e);
                    EditorUtility.ClearProgressBar();
                }

                //插入新版本数据
                if (importLog == null)
                {
                    importLog      = new ImportExcelLog();
                    importLog.Path = filePath;
                    importLog.Hash = excelHash;
                    importLog.Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
                    ;
                    importLog.UnityVersion = Application.unityVersion;
                    SqliteHelper.DB.Insert(importLog);
                }
                else
                {
                    importLog.Hash = excelHash;
                    importLog.Date = DateTime.Now.ToString();
                    ;
                    importLog.UnityVersion = Application.unityVersion;
                    SqliteHelper.DB.Connection.Update(importLog);
                }
            }
            else
            {
                Debug.Log($"<color=green>【Excel2Sql】内容一致,无需导入 {Path.GetFileName(filePath)} - Hash :{excelHash} </color>");
            }
        }
        /// <summary>
        /// 转换Excel文件
        /// </summary>
        private static void Convert()
        {
            foreach (string assetsPath in excelList)
            {
                //获取Excel文件的绝对路径
                string excelPath = pathRoot + "/" + assetsPath;
                //构造Excel工具类
                ExcelExchangeTools excel = new ExcelExchangeTools(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();
        }
Esempio n. 3
0
        /// <summary>
        /// excel导出sqlite
        /// 需要主动连接数据库
        /// </summary>
        /// <param name="filePath"></param>
        public static void Excel2SQLite(string filePath, DBType dbType)
        {
            //收集所有的类型
            CollectTableTypes();

            var excel = new ExcelExchangeTools(filePath);
            var json  = excel.GetJson(dbType);

            try
            {
                Json2Sqlite(filePath, json);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                EditorUtility.ClearProgressBar();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 通过excel生成class
        /// </summary>
        /// <param name="excelFilePath"></param>
        static private void GenClassByExcel(string outputDirectory, string excelFilePath, string @namespace)
        {
            Debug.LogFormat("[{0}]正在生成:" + excelFilePath, @namespace);
            var           excel         = new ExcelExchangeTools(excelFilePath);
            int           idX           = -1;
            int           idY           = -1;
            List <object> keepFieldList = new List <object>();
            string        json          = excel.GetJson(@namespace, 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, @namespace, statements, fieldTypes);

                //输出目录控制
                string outputFile = outputDirectory + "/" + @namespace;
                //获取热更config
                var config = HotfixPipelineTools.HotfixFileConfig.GetConfig("excel");
                //判断配置是否热更
                var outputHotfixFile = Path.Combine(outputFile, Path.GetFileName(excelFilePath) + "@hotfix.cs");
                outputFile = Path.Combine(outputFile, Path.GetFileName(excelFilePath) + ".cs");

                //删除旧文件
                if (File.Exists(outputHotfixFile))
                {
                    File.Delete(outputHotfixFile);
                }
                if (File.Exists(outputFile))
                {
                    File.Delete(outputFile);
                }

                //写入
                if (config != null && config.IsHotfixFile(excelFilePath))
                {
                    FileHelper.WriteAllText(outputHotfixFile, clsContent);
                    Debug.LogFormat("<color=red> [{0} 成功@hotfix] </color>:{1}", @namespace, excelFilePath);
                }
                else
                {
                    FileHelper.WriteAllText(outputFile, clsContent);
                    Debug.LogFormat("<color=green> [{0} 成功@main] </color>:{1}", @namespace, excelFilePath);
                }
            }
            else
            {
                Debug.LogError("不符合规范内容:" + excelFilePath);
            }
        }