static void Main(string[] args) { var inputDir = args[0]; if (!Directory.Exists(inputDir)) { throw new Exception("[ETU] directory: " + inputDir + " not exsists"); } Console.WriteLine("[ETU] generate json from directory: " + inputDir); // create workspace var workspace = inputDir + "/etu"; if (Directory.Exists(workspace)) { Directory.Delete(workspace, true); } Directory.CreateDirectory(workspace); // File.Delete($"{inputDir}/etu.json"); // build var result = new EtuBuildResult(); DataMaker.Instance.Build(inputDir, false, result); // write to file foreach (var kv in result.fileNameToJsonObject) { var name = kv.Key; var jd = kv.Value; var json = jd.ToJson(); File.WriteAllText($"{workspace}/{name}.json", json); } // package json var package = new JsonData(); foreach (var kv in result.fileNameToJsonObject) { var name = kv.Key; var jd = kv.Value; package[name] = jd; } var packageJson = package.ToJson(); File.WriteAllText($"{inputDir}/etu.json", packageJson); // check fail if (result.failCount > 0) { throw new Exception("one or more excel file convert fail"); } }
public void Build(string dir, bool quick, EtuBuildResult result) { DmHelper.MagicNum = 1; DmHelper.DataNameGlobaList.Clear(); var fileList = Directory.GetFiles(dir); foreach (string file in fileList) { var fi = new FileInfo(file); if (fi.Extension != ".xlsx") { continue; } if (fi.Name.StartsWith("~")) { continue; } string fileNameWithoutExtension = fi.Name.Replace(".xlsx", ""); var package = OpenExcelFile(file); try { GenerateJsonFile(fileNameWithoutExtension, quick, package, result); result.successCount++; } catch (Exception e) { Console.WriteLine($"[ETU] {fileNameWithoutExtension} convert fial"); Console.WriteLine(e.StackTrace); result.failCount++; } int _lindex = file.LastIndexOf('/'); string _file_name = file.Substring(_lindex + 1); PlayerPrefs.SetString(_file_name, File.GetLastWriteTime(file).Ticks.ToString()); package.Dispose(); } Console.WriteLine($"[ETU] success: {result.successCount}, fail: {result.failCount}"); PlayerPrefs.Save(); }
public void GenerateJsonFile(string fileNameWithoutExtension, bool quick, ExcelPackage package, EtuBuildResult result) { Console.WriteLine(fileNameWithoutExtension); if (package.Workbook == null) { return; } if (package.Workbook.Worksheets == null) { return; } if (package.Workbook.Worksheets.Count < 1) { return; } foreach (var sheet in package.Workbook.Worksheets) { if (sheet.Dimension == null) { continue; } if (sheet.Dimension.Rows < MIN_ROW_COUNT) { if (result != null) { result.skip_sheet_list.Add(fileNameWithoutExtension + "." + sheet.Name); } continue; } var cell = GetSheetCell(sheet, 0, 0); string tableName = cell.ToString(); if (string.IsNullOrEmpty(tableName)) { if (result != null) { result.skip_sheet_list.Add(fileNameWithoutExtension + "." + sheet.Name); } continue; } if (sheet.Dimension.Columns > 2) { var value = GetSheetCell(sheet, 0, 2); if (!string.IsNullOrEmpty(value.ToString())) { if (result != null) { result.skip_sheet_list.Add(fileNameWithoutExtension + "." + sheet.Name); } continue; } } DmHelper.DataBankList.Clear(); DmHelper.DataNameBankList.Clear(); JsonDataType m_json_type = JsonDataType.NORMAL; if (!string.IsNullOrEmpty(GetSheetCell(sheet, 0, 1).ToString())) { if (GetSheetCell(sheet, 0, 1).ToString() == "kv") { m_json_type = JsonDataType.KEY_VALUE; } else if (GetSheetCell(sheet, 0, 1).ToString() == "array") { m_json_type = JsonDataType.ARRAY; } else if (GetSheetCell(sheet, 0, 1).ToString() == "nkv") { m_json_type = JsonDataType.NEW_KEY_VALUE; } else { m_json_type = JsonDataType.NORMAL; } } List <string> keys = new List <string>(); for (int i = sheet.Dimension.Columns - 1; i >= 0; i--) { string key = GetSheetCell(sheet, KEY_ROW_INDEX, i).ToString(); keys.Add(key); } int rowCount = sheet.Dimension.Rows; int colCount = sheet.Dimension.Columns; int _real_num = 0; string _info = ""; string _sinfo = ""; string m_id_key_type = GetSheetCell(sheet, KEY_TYPE_ROW_INDEX, 0).ToString(); List <string> _id_list = new List <string>(); Dictionary <string, List <string> > _dic = new Dictionary <string, List <string> >(); try { for (int i = VALUE_BEGIN_ROW_INDEX; i < rowCount; i++) { if (string.IsNullOrEmpty(GetSheetCell(sheet, i, 0).ToString())) { continue; } if (!_id_list.Contains(GetSheetCell(sheet, i, 0).ToString())) { _id_list.Add(GetSheetCell(sheet, i, 0).ToString()); } } for (int i = VALUE_BEGIN_ROW_INDEX; i < rowCount; i++) { if (string.IsNullOrEmpty(GetSheetCell(sheet, i, 0).ToString())) { continue; } RjData _data = new RjData(null); //Parse(_data,sheet,sheet.Rows[i], 0, sheet.Rows[KEY_ROW_INDEX][0].ToString(), colCount); Parse(_data, sheet, sheet.Row(i), 0, GetSheetCell(sheet, KEY_ROW_INDEX, 0).ToString(), colCount); switch (m_json_type) { case JsonDataType.ARRAY: { string _dd = _data.ToJson(); int _bindex = _dd.IndexOf("\"id\":"); int _eindex = _dd.IndexOf(","); //Debug.Log(_dd); //Debug.Log(_bindex+" "+_eindex); string _temp_id = _dd.Substring(_bindex + 5, _eindex - _bindex - 5); if (!_dic.ContainsKey(_temp_id)) { List <string> _ttt = new List <string>(); _dic.Add(_temp_id, _ttt); } _dic[_temp_id].Add("{" + _dd.Substring(_eindex + 1, _dd.Length - _eindex - 1)); } break; case JsonDataType.NORMAL: default: { string _dd = _data.ToJson(); int _bindex = _dd.IndexOf("\"id\":"); int _eindex = _dd.IndexOf(","); string _id = _dd.Substring(_bindex + 5, _eindex - _bindex - 5); // Debug.Log(_dd); //Debug.Log(_bindex+" "+_eindex); _info += _id.Replace("\"", "") + "`" + _dd; _info += "|"; //TODO zhaolei if (!quick) { //string _id=_dd.Substring(_bindex+5,_eindex-_bindex-5); if (!_id.Contains("\"")) { _sinfo += "\"" + _id + "\":" + _dd; } else { _sinfo += _id + ":" + _dd; } _sinfo += ","; } } break; case JsonDataType.NEW_KEY_VALUE: { _info += _data.ToKeyValue().Replace("=", "`"); _info += "|"; //TODO zhaolei if (!quick) { string _dd = _data.ToJson(); int _bindex = _dd.IndexOf("\"id\":"); int _eindex = _dd.IndexOf(","); string _id = _dd.Substring(_bindex + 5, _eindex - _bindex - 5); if (!_id.Contains("\"")) { _sinfo += "\"" + _id + "\":" + _dd; } else { _sinfo += _id + ":" + _dd; } _sinfo += ","; } } break; case JsonDataType.KEY_VALUE: { string _ii = _data.ToJson(); int _bindex = _ii.IndexOf("\"id\":"); int _eindex = _ii.IndexOf(","); string _id = _ii.Substring(_bindex + 5, _eindex - _bindex - 5); _info += _id.Replace("\"", "") + "`" + _ii; _info += "|"; if (!quick) { _sinfo += _data.ToKeyValueString(); _sinfo += ","; } } break; } _real_num++; } if (result != null) { result.done_list.Add(fileNameWithoutExtension + "." + sheet.Name + "==>" + tableName + " success!!!"); } } catch { if (result != null) { result.error_sheet_list.Add(fileNameWithoutExtension + "." + sheet.Name); } continue; } if (m_json_type == JsonDataType.ARRAY) { foreach (KeyValuePair <string, List <string> > _kv in _dic) { string _tinfo = "{\"id\":" + _kv.Key + ",\"Coll\":["; foreach (string _kk in _kv.Value) { _tinfo += _kk; _tinfo += ","; } _tinfo = _tinfo.Remove(_tinfo.Length - 1); _tinfo += "]}"; string _dd = _tinfo; int _bindex = _dd.IndexOf("\"id\":"); int _eindex = _dd.IndexOf(","); string _id = _dd.Substring(_bindex + 5, _eindex - _bindex - 5); _info += _id.Replace("\"", "") + "`" + _dd; _info += "|"; if (!quick) { _sinfo += "\"" + _id + "\":" + _dd; _sinfo += ","; } } } _info = _info.Remove(_info.Length - 1); if (!quick) { _sinfo = _sinfo.Remove(_sinfo.Length - 1); _sinfo = "{" + _sinfo + "}"; } //client if (result != null) { _info = _info.Replace('\n', ' '); } //----------youhua--------------// //_info=InfoTail(_file_name,_info); //result.json_str[_file_name] = JsonMapper.Instance.ToObject(_sinfo); result.fileNameToJsonObject[tableName] = JsonMapper.Instance.ToObject(_sinfo); // var filePath = DataMakerConf.Instance.m_server_dir + tableName + ".json"; // File.WriteAllText(filePath, _sinfo, Encoding.UTF8); // Console.WriteLine(filePath); } }