//检测是否包含tag,全不包含的直接移除 private void CheckTag(ExcelData excelData, string outputPath) { if (outputPath.Contains(OPTION_CSHARP_OUT_DIR)) { _tag = "c#"; } else if (outputPath.Contains(OPTION_JAVA_OUT_DIR)) { _tag = "java"; } for (int i = excelData.tableList.Count - 1; i >= 0; --i) { TableData table = excelData.tableList[i]; bool isExist = false; for (int j = 0; j < table.tableDeclare.Count; ++j) { VariableDeclare declare = table.tableDeclare[j]; if (!string.IsNullOrEmpty(declare.tag) && declare.tag.Contains(_tag)) { isExist = true; break; } } if (!isExist) { excelData.tableList.RemoveAt(i); } } }
/// <summary> /// 读取excel表 /// </summary> /// <param name="tablePath">表路径</param> /// <returns></returns> private static TableData ReadTable(string tablePath) { try { TableData tableData = new TableData(); //打开文件 FileStream stream = File.Open(tablePath, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); if (!excelReader.IsValid) { throw new Exception("无法打开文件"); } if (string.IsNullOrEmpty(excelReader.Name)) { throw new Exception("缺少表名"); } //读取每行数据 Dictionary <int, string[]> dataDict = ReadRowData(excelReader); //获取变量声明 string[] tags = null; dataDict.TryGetValue(EXPORT_TAG_ROW, out tags); string[] types = null; dataDict.TryGetValue(FIELD_TYPE_ROW, out types); string[] names = null; dataDict.TryGetValue(FIELD_NAME_ROW, out names); tableData.tableDeclare = GetVariableDeclare(tags, types, names); tableData.tableName = excelReader.Name; tableData.idTypeName = GetIdTypeName(tableData.tableDeclare); foreach (KeyValuePair <int, string[]> item in dataDict) { if (item.Key <= FIELD_NAME_ROW) { continue; } //整合每一行的数据 string[] values = item.Value; FieldData[] datas = new FieldData[values.Length]; for (int i = 0; i < values.Length; i++) { VariableDeclare declare = tableData.tableDeclare[i]; FieldData data = new FieldData() { type = declare.type, name = declare.name, data = values[i], }; datas[i] = data; } tableData.rowDatas.Add(datas); } excelReader.Close(); return(tableData); } catch (Exception e) { throw new Exception(e.Message + " " + tablePath); } }
private static string GetIdTypeName(List <VariableDeclare> declareList) { for (int i = 0; i < declareList.Count; ++i) { VariableDeclare declare = declareList[i]; if (!string.IsNullOrEmpty(declare.name) && declare.name.Equals(ProtobufGen.ID_NAME)) { return(declare.type); } } return(null); }
/// <summary> /// 获取变量声明 /// </summary> /// <returns></returns> private static List <VariableDeclare> GetVariableDeclare(string[] tags, string[] types, string[] names) { List <VariableDeclare> list = new List <VariableDeclare>(); for (int i = 0; i < types?.Length || i < tags?.Length || i < names?.Length; ++i) { VariableDeclare declare = new VariableDeclare() { type = i < types?.Length ? types[i] : null, name = i < names?.Length ? names[i] : null, tag = i < tags?.Length ? tags[i] : null, }; list.Add(declare); } return(list); }