/// <summary> /// 读取Excel /// </summary> void DoParserExcelData(ExcelOutputData data) { FileStream stream = File.Open(data.ExcelPath, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); //解析表名 var _tableName = data.ExcelPath.Substring(data.ExcelPath.LastIndexOf("/") + 1); _tableName = _tableName.Substring(0, _tableName.LastIndexOf(".")); SetTableName(_tableName); //解析表内容 if (string.IsNullOrEmpty(data.SheetNames.Trim())) { //sheetName没填默认第一个页 var _table = result.Tables[0]; //解析头 DoParserTable(_table); //开始导出对应文件 DoOutputFile(data); return; } else { var _names = data.SheetNames.Trim().Split(','); for (int i = 0; i < _names.Length; i++) { var _name = _names[i].Trim(); for (int j = 0; j < result.Tables.Count; j++) { if (result.Tables[j].TableName == _name) { //解析头 DoParserTable(result.Tables[j]); //开始导出对应文件 DoOutputFile(data); return; } } } } EditorUtility.DisplayDialog("提示", "Sheet页名称错误:" + data.SheetNames, "确定"); }
/// <summary> /// 开始导出对应文件 /// </summary> void DoOutputFile(ExcelOutputData data) { if (data.IsNeedCS) { if (string.IsNullOrEmpty(CSharpOutputPath)) { EditorUtility.DisplayDialog("提示", "当前C#脚本导出路径不存在", "确定"); return; } else { if (!Directory.Exists(CSharpOutputPath)) { Directory.CreateDirectory(CSharpOutputPath); } string _code = GetCSCode(); IOTool.CreateFileString(CSharpOutputPath + "/" + _TableName + ".cs", _code); } } if (data.IsNeedLua) { if (string.IsNullOrEmpty(LuaOutputPath)) { EditorUtility.DisplayDialog("提示", "当前Lua脚本导出路径不存在", "确定"); return; } else { if (!Directory.Exists(LuaOutputPath)) { Directory.CreateDirectory(LuaOutputPath); } string _code = GetLuaCode(); IOTool.CreateFileString(LuaOutputPath + "/" + _TableName + ".lua", _code); } } ClearData(); //一组数据导出后,清除临时数据 }