最近在做一个火炬之光的技能系统的demo,需要用到配置表工具。
&在网上没有找到让自己满意的工具&自己感兴趣,
so自己做了一个。
我使用的C#语言,用了网上的SimpleJSON工具脚本来做的,下面直接上代码:
界面代码(ps.注释掉的部分是生成对应代码的类,但感觉目前写的不好,就不上传了。。。)
1 using System; 2 using System.Collections.Generic; 3 using System.Windows.Forms; 4 5 namespace ConfigTool 6 { 7 public partial class Form1 : Form 8 { 9 private string excelPath; 10 private string jsonPath; 11 private string codePath; 12 13 public Form1() 14 { 15 InitializeComponent(); 16 InitPath(); 17 } 18 19 private void InitPath() 20 { 21 excelPath = Tool. GetPath(4) + "ConfigExcel"; 22 jsonPath = Tool.GetPath(5) + "Assets\\Resources\\Config"; 23 //codePath = Tool.GetPath(5) + "Assets\\Scripts\\Config"; 24 folderPath.Text = excelPath; 25 } 26 27 private void createJsonAndCode_Click(object sender, EventArgs e) 28 { 29 ExcelToJson etoJson = new ExcelToJson(excelPath, jsonPath); 30 etoJson.CreateJsonFiles(); 31 fileNum.Text = etoJson.GetFileCount().ToString(); 32 33 //AutoCreateCode createCode = new AutoCreateCode(codePath, etoJson.JsonDic); 34 //createCode.CreateCodeFiles(); 35 } 36 } 37 }
工具代码
1 using System; 2 using System.IO; 3 4 namespace ConfigTool 5 { 6 public static class Tool 7 { 8 /// <summary> 9 /// 创建文件 10 /// </summary> 11 /// <param name="_path">文件路径(包含文件名及其后缀)</param> 12 /// <param name="_content">文件存储的内容</param> 13 public static void CreateFile(string _path, string _content) 14 { 15 FileStream file = new FileStream(_path, FileMode.Create, FileAccess.Write); 16 StreamWriter writer = new StreamWriter(file); 17 writer.WriteLine(_content); 18 writer.Close(); 19 file.Close(); 20 Console.WriteLine(string.Format("生成{0}", _path)); 21 } 22 23 /// <summary> 24 /// 删除指定文件夹下的子文件(不含子文件夹) 25 /// </summary> 26 /// <param name="_folderPath">文件夹路径</param> 27 public static void ClearFiles(string _folderPath) 28 { 29 Console.WriteLine("开始删除文件,文件夹目录为" + _folderPath); 30 if (!Directory.Exists(_folderPath)) 31 { 32 Console.WriteLine("指定的文件路径不存在"); 33 return; 34 } 35 var files = Directory.GetFiles(_folderPath); 36 foreach (var a in files) 37 { 38 File.Delete(a); 39 } 40 Console.WriteLine("删除完成"); 41 } 42 43 /// <summary> 44 /// 获取当前exe的上层几层路径 45 /// </summary> 46 /// <param name="_upperNum">向上几级</param> 47 /// <returns>路径</returns> 48 public static string GetPath(int _upperNum) 49 { 50 string exePath = Directory.GetCurrentDirectory(); 51 string[] temp = exePath.Split("\\".ToCharArray()); 52 string path = string.Empty; 53 for (int i = 0; i < temp.Length - _upperNum; i++) 54 { 55 path += temp[i]; 56 path += "\\"; 57 } 58 return path; 59 } 60 } 61 }
excel转json文件代码
1 using System; 2 using System.Collections.Generic; 3 using SimpleJSON; 4 using System.IO; 5 using System.Data.OleDb; 6 using System.Data; 7 8 namespace ConfigTool 9 { 10 public class ExcelToJson 11 { 12 private string sourcePath; 13 private string savePath; 14 private Dictionary<string, JSONClass> jsonDic = new Dictionary<string, JSONClass>(); 15 private int fileNum; 16 public Dictionary<string, JSONClass> JsonDic 17 { 18 get 19 { 20 return jsonDic; 21 } 22 } 23 24 public ExcelToJson(string _sourcePath, string _savePath) 25 { 26 sourcePath = _sourcePath; 27 savePath = _savePath; 28 } 29 30 public void CreateJsonFiles() 31 { 32 Tool.ClearFiles(savePath); 33 AllConvertToJsons(); 34 foreach (var a in jsonDic) 35 { 36 string path = string.Format("{0}\\{1}.json", savePath, a.Key); 37 Tool.CreateFile(path, a.Value.ToString()); 38 } 39 } 40 41 public int GetFileCount() 42 { 43 return fileNum; 44 } 45 46 private void AllConvertToJsons() 47 { 48 jsonDic.Clear(); 49 var excelPathList = GetAllExcel(); 50 fileNum = excelPathList.Count; 51 foreach (var a in excelPathList) 52 { 53 ConvertToJson(a); 54 } 55 } 56 57 private void ConvertToJson(string _excelPath) 58 { 59 OleDbConnection connection = CreateExcelOleDbConnection(_excelPath); 60 if (connection == null) 61 { 62 Console.WriteLine("无法成功生成OleDbConnection"); 63 return; 64 } 65 List<OleDbDataReader> readers = InitExcel(_excelPath, connection); 66 ReadersToJson(readers, connection); 67 } 68 69 #region ConvertToJson 具体实现方法 70 private List<OleDbDataReader> InitExcel(string _excelPath, OleDbConnection _connection) 71 { 72 if (_connection == null) 73 { 74 return null; 75 } 76 _connection.Open(); 77 DataTable dataTable = _connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 78 79 List<OleDbDataReader> readers = new List<OleDbDataReader>(); 80 for (int i = 0; i < dataTable.Rows.Count; i++) 81 { 82 string sheetName = dataTable.Rows[i]["Table_Name"].ToString(); 83 if (sheetName.Contains("#"))//表中页签开头‘#‘为注释 84 { 85 continue; 86 } 87 OleDbCommand command = new OleDbCommand(string.Format("select * from [{0}]", sheetName), _connection); 88 readers.Add(command.ExecuteReader()); 89 } 90 return readers; 91 } 92 93 private OleDbConnection CreateExcelOleDbConnection(string _excelPath) 94 { 95 if (!File.Exists(_excelPath)) 96 { 97 Console.WriteLine("未找到指定文件" + _excelPath); 98 return null; 99 } 100 101 string strExtension = Path.GetExtension(_excelPath); 102 string initStr = string.Empty; 103 switch (strExtension) 104 { 105 case ".xls": 106 initStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"", _excelPath); 107 break; 108 case ".xlsx": 109 initStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1;\"", _excelPath); 110 break; 111 default: 112 Console.WriteLine("目标文件不是excel文件"); 113 return null; 114 break; 115 } 116 117 return new OleDbConnection(initStr); 118 } 119 120 private void ReadersToJson(List<OleDbDataReader> _readers, OleDbConnection _connection) 121 { 122 for (int i = 0; i < _readers.Count; i++) 123 { 124 OleDbDataReader reader = _readers[i]; 125 126 //获取表头 127 reader.Read(); 128 string configTitle = reader[0].ToString(); 129 if (configTitle == null || configTitle == "") 130 { 131 Console.WriteLine("表头填写不正确"); 132 } 133 int nextLineIndex = 1; 134 135 //跳过无用行 136 while (reader.Read()) 137 { 138 nextLineIndex++; 139 if (reader[0].ToString() == "Start") 140 { 141 break; 142 } 143 } 144 145 //存储json的key,simplejson无法直接获取key 146 int maxRowNum = reader.FieldCount; 147 JSONClass jsonKey = new JSONClass(); 148 JSONClass jsonData = new JSONClass(); 149 reader.Read(); 150 for (int j = 1; j < maxRowNum; j++) 151 { 152 string key = reader[j].ToString(); 153 jsonKey.Add(key, key); 154 } 155 jsonData.Add("variate", jsonKey); 156 157 //依次按行读取有效数据 158 while (reader.Read()) 159 { 160 string key = reader[0].ToString(); 161 if (key == "End") 162 { 163 break; 164 } 165 166 JSONClass curLineJson = new JSONClass(); 167 for (int j = 1; j < maxRowNum; j++) 168 { 169 curLineJson.Add(jsonKey[j - 1], reader[j].ToString()); 170 } 171 jsonData.Add(key, curLineJson); 172 } 173 reader.Close(); 174 175 //将当前页签的json文件存储到字典中 176 if (jsonDic.ContainsKey(configTitle)) 177 { 178 jsonDic[configTitle].Add(jsonData); 179 } 180 else 181 { 182 jsonDic.Add(configTitle, jsonData); 183 } 184 } 185 _connection.Close(); 186 } 187 #endregion ConvertToJson 具体实现方法 188 189 /// <summary> 190 /// 获取源文件夹中的excel文件,文件名中的"###"作为注释,含有"###"的文件不计进行转换 191 /// </summary> 192 /// <returns>excel文件路径列表</returns> 193 private List<string> GetAllExcel() 194 { 195 DirectoryInfo dirInfo = new DirectoryInfo(sourcePath); 196 FileInfo[] fileInfos = dirInfo.GetFiles(); 197 List<string> rtnValue = new List<string>(); 198 foreach (var a in fileInfos) 199 { 200 if (a.FullName.Contains("###")) 201 { 202 continue; 203 } 204 if (a.FullName.Contains(".xlsx") 205 || a.FullName.Contains(".xls")) 206 { 207 if (rtnValue.Contains(a.FullName)) 208 { 209 continue; 210 } 211 rtnValue.Add(a.FullName); 212 } 213 } 214 return rtnValue; 215 } 216 } 217 }
总结:
起始最核心的部分还是读取excel的部分,感觉应该对OleDbDataReader做一下说明:OleDbDataReader是一行一行来读取excel的读完一行再读下一行。我所写的确定表头,确定excel有效数据开始行就是利用这个性质来做的。
ps.刚开始理解不好,写了好几个无用的循环,后来反应过来改掉了(手动捂脸)。
时间: 2024-10-17 16:01:19