public static void Parse(DataField dataField, string txt, JsonData lineJD, XlsxManager xlsxManager) { string field = dataField.field; string typeName = dataField.typeName.ToLower().Trim(); switch (typeName) { case "string": lineJD[field] = txt; //lineJD[field] = string.Format("\"{0}\"", txt.Replace("\"", "\\\"")); return; case "int64": lineJD[field] = txt.ToInt64(); return; case "int": lineJD[field] = txt.ToInt32(); return; case "double": lineJD[field] = txt.ToDouble(); return; case "float": lineJD[field] = txt.ToSingle(); return; case "boolean": case "bool": lineJD[field] = txt.ToBoolean(); return; } if (typeName.EndsWith("[]")) { string[] csv = txt.toStringArray(); JsonData jd = new JsonData(); jd.SetJsonType(JsonType.Array); lineJD[field] = jd; switch (typeName) { case "string[]": for (int i = 0; i < csv.Length; i++) { jd.Add(csv[i]); } return; case "int64[]": for (int i = 0; i < csv.Length; i++) { jd.Add(csv[i].ToInt64()); } return; case "int[]": for (int i = 0; i < csv.Length; i++) { jd.Add(csv[i].ToInt32()); } return; case "double[]": for (int i = 0; i < csv.Length; i++) { jd.Add(csv[i].ToDouble()); } return; case "float[]": for (int i = 0; i < csv.Length; i++) { jd.Add(csv[i].ToSingle()); } return; case "boolean[]": case "bool[]": for (int i = 0; i < csv.Length; i++) { jd.Add(csv[i].ToBoolean()); } return; } } typeName = dataField.GetTsTypeName(); if (typeName.EndsWith("[]")) { typeName = typeName.Replace("[]", ""); DataStruct dataStruct = xlsxManager.GetDataStruct(typeName); if (dataStruct != null) { lineJD[field] = ParseStructArray(txt, dataStruct, xlsxManager); } else { lineJD[field] = txt; } } else { DataStruct dataStruct = xlsxManager.GetDataStruct(typeName); if (dataStruct != null) { lineJD[field] = ParseStruct(txt, dataStruct, xlsxManager); } else { lineJD[field] = txt; } } }
public void Load() { if (string.IsNullOrEmpty(tableName)) { tableName = Path.GetFileNameWithoutExtension(path).FirstUpper(); } Log.Info($"读取配置表 {tableName}"); dataStruct.name = tableName; var xlsx = new FileInfo(path); using (var package = new ExcelPackage(xlsx)) { ExcelWorksheet ws = null; if (package.Workbook.Worksheets.Count > 0) { IEnumerator enumerator = package.Workbook.Worksheets.GetEnumerator(); while (enumerator.MoveNext() && ws == null) { if (string.IsNullOrEmpty(sheetName)) { ws = (ExcelWorksheet)enumerator.Current; } else { if (((ExcelWorksheet)enumerator.Current).Name == sheetName) { ws = (ExcelWorksheet)enumerator.Current; } } } } if (ws == null) { Log.Error($"没有找到sheet path:{path}, sheetName:{sheetName}"); return; } if (ws.Cells.Rows < 3) { Log.Error($" path:{path}, sheetName:{sheetName}, rows:{ws.Cells.Rows}, 行数小于3行, 必须要有(type, cn, field)"); return; } int columnNum = 0; for (int i = 1; i < ws.Cells.Columns; i++) { if (ws.Cells[1, i].Value == null) { break; } if (ws.GetValue(2, i) == null) { Log.Error($" path:{path}, sheetName:{sheetName}, 是空单元格 2行{i}列 "); continue; } if (ws.GetValue(3, i) == null) { Log.Error($" path:{path}, sheetName:{sheetName}, 是空单元格 3行{i}列 "); continue; } string type = ws.GetValue(Setting.Options.xlsxHeadTypeLine, i).ToString().Trim(); string cn = ws.GetValue(Setting.Options.xlsxHeadCnLine, i).ToString().Trim(); string en = ws.GetValue(Setting.Options.xlsxHeadFieldLine, i).ToString().Trim(); if (string.IsNullOrEmpty(type)) { Log.Error($" path:{path}, sheetName:{sheetName}, 是空单元格 type行{i}列 {type} {cn} {en} "); continue; } if (string.IsNullOrEmpty(en)) { Log.Error($" path:{path}, sheetName:{sheetName}, 是空单元格 field{i}列 {type} {cn} {en} "); continue; } DataField field = new DataField(); field.typeName = type; field.cn = cn; field.field = en; field.index = i; dataStruct.fields.Add(field); fieldDictByIndex.Add(i, field); columnNum = i; } for (int r = 4; r < ws.Cells.Rows; r++) { if (ws.Cells[r, 1].Value == null) { break; } Dictionary <string, string> rowData = new Dictionary <string, string>(); for (int c = 1; c <= columnNum; c++) { string value = string.Empty; if (ws.Cells[r, c].Value != null) { value = ws.GetValue(r, c).ToString().Trim(); } if (fieldDictByIndex.ContainsKey(c)) { if (rowData.ContainsKey(fieldDictByIndex[c].field)) { Log.Error($" path:{path}, sheetName:{sheetName}, 存在相同的 field={fieldDictByIndex[c].field} {c}列"); } else { rowData.Add(fieldDictByIndex[c].field, value); } } } if (rowData.Count > 0) { dataList.Add(rowData); } } } }