1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| protected override void ExportData(DataTable dt, string excelName, string sheetName) { StringBuilder sb = new StringBuilder(); sb.Append("local data = {\n");
for (int i = 4; i < dt.Rows.Count; i++) { sb.AppendFormat("\t[{0}] = ", i - 3); sb.Append("{\n");
for (int j = 1; j < dt.Columns.Count; j++) { string fieldName = dt.Rows[0][j].ToString().Trim(); string fieldType = dt.Rows[1][j].ToString().Trim(); string fieldValue = dt.Rows[i][j].ToString().Trim();
string fieldStr = GetFieldStr(fieldName, fieldValue, fieldType);
if (!string.IsNullOrEmpty(fieldStr)) { sb.AppendFormat("\t\t{0}\n", fieldStr); } }
sb.Append("\t},\n"); }
sb.Append("}\n"); sb.AppendFormat("--excelName = {0}\n", excelName); sb.AppendFormat("--sheetName = {0}\n", sheetName); sb.Append("return data");
File.WriteAllText(string.Format("{0}/Lua/{1}Data.lua", m_ExportPath, dt.Rows[1][0].ToString()), sb.ToString()); }
private string GetFieldStr(string fieldName, string fieldValue, string fieldType) { if (fieldType.Equals("string")) { return string.Format("{0} = \"{1}\",", fieldName, fieldValue); } else if (fieldType.Equals("json")) { if (!string.IsNullOrEmpty(fieldValue)) { JsonData jsonData = LitJson.JsonMapper.ToObject(fieldValue);
if (jsonData != null) { StringBuilder jsonSB = new StringBuilder(); ParseJson(jsonData, jsonSB); return string.Format("{0} = {1}\n{2}\t\t{3},", fieldName, "{", jsonSB.ToString(), "}"); } }
return string.Format("{0} = nil,", fieldName); }
if (string.IsNullOrEmpty(fieldValue)) { fieldValue = fieldType.Contains("bool") ? "false" : "nil"; } else if (fieldType.Contains("[]")) { string result = fieldName + " = {\n\t\t\t"; string fieldValueTemp = fieldValue.Replace(" ", "").Replace(",", ",\n\t\t\t");
if (fieldType.Contains("string")) { fieldValueTemp = "\"" + fieldValue.Replace(" ", "").Replace(",", "\",\n\t\t\t\"") + "\""; } else if (fieldType.Contains("bool")) { fieldValueTemp = fieldValueTemp.ToLower(); }
return result + fieldValueTemp + ",\n\t\t},"; } else if (fieldType.Contains("Vector")) { string[] vectorValues = fieldValue.Split(','); string[] vectorFieldName = { "x", "y", "z" }; string result = fieldName + " = {";
for (int i = 0; i < vectorValues.Length; i++) { result += string.Format("{0} = {1}", vectorFieldName[i], vectorValues[i]);
if(i < vectorValues.Length - 1) { result += ","; } }
return result + "},"; }
return string.Format("{0} = {1},", fieldName, fieldValue); }
private void ParseJson(JsonData jsonData, StringBuilder sb, int tCount = 3) { if (jsonData.IsArray) { for (int i = 0; i < jsonData.Count; i++) { if (!JsonFieldIsBaseValueType(string.Format("[{0}]", i + 1), jsonData[i], tCount, sb)) { for (int j = 0; j < tCount; j++) { sb.Append("\t"); }
sb.AppendFormat("[{0}] = ", i + 1); sb.Append("{\n");
ParseJson(jsonData[i], sb, tCount + 1);
for (int j = 0; j < tCount; j++) { sb.Append("\t"); }
sb.Append("},\n"); } } } else if (jsonData.Keys.Count > 0) { foreach (KeyValuePair<string, LitJson.JsonData> kvp in jsonData) { string key = kvp.Key; JsonData val = kvp.Value;
if (!JsonFieldIsBaseValueType(key, val, tCount, sb)) { for (int i = 0; i < tCount; i++) { sb.Append("\t"); }
sb.AppendFormat("{0} = ", key); sb.Append("{\n");
ParseJson(val, sb, tCount + 1);
for (int i = 0; i < tCount; i++) { sb.Append("\t"); }
sb.Append("},\n"); } } } }
private bool JsonFieldIsBaseValueType(string fieldName, JsonData jsonData, int tCount, StringBuilder sb) { string fieldValueStr = jsonData.ToString(); string fieldType = string.Empty;
if (jsonData.IsInt) { fieldType = "int"; } else if (jsonData.IsLong) { fieldType = "long"; } else if (jsonData.IsDouble) { fieldType = "double"; } else if (jsonData.IsBoolean) { fieldType = "bool"; fieldValueStr = fieldValueStr.ToLower(); } else if (jsonData.IsString) { fieldType = "string"; }
if (string.IsNullOrEmpty(fieldType)) { return false; }
for (int i = 0; i < tCount; i++) { sb.Append("\t"); }
sb.AppendFormat("{0}\n", GetFieldStr(fieldName, fieldValueStr, fieldType)); return true; }
|