private void RecordMergeParameter(ConverterParameter parameter) { string[] parameters = parameter.MergePara.Split(new char[] { '&' }, System.StringSplitOptions.RemoveEmptyEntries); foreach (var str in parameters) { if (mergeParameter.ContainsKey(str) == false) { mergeParameter.Add(str, parameter); } } }
private void GetValue(string originValue, ConverterParameter para, StringBuilder result) { Hashtable replaceTable = GetReplace(para.ValueReplaceGroup); if (string.IsNullOrEmpty(para.SplitParten)) { result.Append(GetValue(originValue, para, replaceTable)); return; } else { SplitValue(originValue, para, result, replaceTable); } }
private void ReadConverterRule(string fileName, string sheetName) { _converter.Clear(); using (Office.Excel.ForwardExcelReader reader = new Office.Excel.ForwardExcelReader(fileName)) { reader.Open(); Office.Excel.ForwardReadWorksheet sheet = reader.Activate(sheetName) as Office.Excel.ForwardReadWorksheet; if (sheet == null) { return; } Hashtable columnHeader = new Hashtable(); if (sheet.ReadNextRow()) { while (sheet.ReadNextCell(false)) { columnHeader.Add(sheet.CurrentCell.ColumnIndex, sheet.GetContent()); } } string str; object content; while (sheet.ReadNextRow()) { ConverterParameter para = new ConverterParameter(); while (sheet.ReadNextCell(false)) { content = sheet.GetContent(); str = content == null ? "" : content.ToString(); switch (columnHeader[sheet.CurrentCell.ColumnIndex] as string) { case "字符替换": para.ValueReplaceGroup = str; break; case "参数编号": para.OriginName = str; break; } } if (string.IsNullOrEmpty(para.OriginName)) { continue; } _converter.Add(para.OriginName, para); } } }
private void InnerConvert(FillValue3C fillValue, ConverterParameter parameter) { StringBuilder builder = new StringBuilder(fillValue.Value); if (string.IsNullOrEmpty(parameter.ValueReplaceGroup) == false) { string[] replaceGroup = parameter.ValueReplaceGroup.Split('|'); foreach (var group in replaceGroup) { string[] keyAndValue = group.Split(':'); if (keyAndValue.Length >= 2) { builder.Replace(keyAndValue[0], keyAndValue[1]); } } } fillValue.Value = builder.ToString(); }
private void SplitValue(string originValue, ConverterParameter para, StringBuilder result, Hashtable replaceTable) { // 按填报级数分割符逐级拆分originValue Stack <SplitParameter> stack = new Stack <SplitParameter>(); char[] spliters = para.SplitParten.ToCharArray(); int splitDepth = spliters.Length; SplitParameter current = new SplitParameter(originValue.Split(spliters[splitDepth - 1])); string currentValue = ""; do { for (int index = 0; index < current.values.Length; index++) { if (splitDepth > 1) { current.index = index; stack.Push(current); currentValue = current.values[index]; splitDepth--; current = new SplitParameter(currentValue.Split(spliters[splitDepth - 1])); index = -1; } else { string str = GetValue(current.values[index], para, replaceTable); result.Append(string.Format("{0}{1}", str, this.SpliterChars[0])); } if (current.values.Length - 1 == index) { result.Remove(result.Length - 1, 1); SplitParameter t = stack.Count >= 1 ? stack.Peek() : null; if (t != null) { if (t.values.Length - 1 > t.index) { result.Append(this.SpliterChars[splitDepth]); } current = stack.Pop(); } } } } while (stack.Count > 0); }
private string GetValue(string value, ConverterParameter para, Hashtable replaceTable) { // 使用转换参数的拆分正则匹配value,并返回匹配到的值,若匹配失败则返回原字符串。 string str; if (string.IsNullOrEmpty(para.RegexParten)) { str = value; } else { System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(value, para.RegexParten); str = match.Success ? match.Value : ""; } if (replaceTable == null || string.IsNullOrEmpty(str) || replaceTable.ContainsKey(str) == false) { return(str); } else { return(replaceTable[str] as string); } }
private string GetExpressionResult(ConverterParameter parameter, Hashtable table, Hashtable converted) { StringBuilder result = new StringBuilder(); System.Text.RegularExpressions.Match match = calcParameterMatcher.Match(parameter.CalcExpression); int startIndex = 0; while (match.Success) { result.Append(parameter.CalcExpression.Substring(startIndex, match.Index - startIndex)); string name = match.Groups["name"].Value; string val = ""; if (converted.Contains(name)) { val = converted[name] as string; } else if (table.Contains(name)) { val = this.GetValue(table[name], parameter); } if (string.IsNullOrEmpty(val)) { return(""); } result.Append(val); startIndex = match.Index + match.Length; match = match.NextMatch(); } result.Append(parameter.CalcExpression.Substring(startIndex)); string expr = result.ToString(); if (Applications.Maths.FormulaHelper.IsValidExpression(expr) == false) { return(""); } return(Applications.Maths.FormulaHelper.Calculate(expr).ToString("0.000")); }
public virtual System.Collections.Hashtable Convert(string fileName, string sheetName, System.Collections.Hashtable table) { Hashtable converted = new Hashtable(); this.ReadConverterRule(fileName, sheetName); StringBuilder result = new StringBuilder(); foreach (var item in _converter) { if (item.UseOriginString == false) { // 若不使用原始值 if (item.IsSheetRelation && string.IsNullOrEmpty(item.OriginName) == false) { sheetNameRelation.Add(item.OriginName, item.EntName); continue; } if (string.IsNullOrEmpty(item.EntName)) { item.EntName = item.OriginName; } // 计算表达式类型转换规则 if (string.IsNullOrEmpty(item.CalcExpression) == false) { converted.Add(item.OriginName, GetExpressionResult(item, table, converted)); continue; } // 获取原始值 string value = this.GetValue(table[item.EntName], item); if (string.IsNullOrEmpty(value) == false) { result.Clear(); this.GetValue(value, item, result); if (converted.ContainsKey(item.OriginName)) { converted.Remove(item.OriginName); } string str = result.ToString(); // str为空时返回默认值 //str = string.IsNullOrEmpty(str) ? item.DefaultValue : str; converted.Add(item.OriginName, str); } } else { converted.Add(item.OriginName, this.GetValue(table[item.EntName], item)); } if (string.IsNullOrEmpty(item.MergePara) == false) // 待合并的参数 { RecordMergeParameter(item); } if (mergeParameter.Contains(item.EntName)) { ConverterParameter parameter = mergeParameter[item.EntName] as ConverterParameter; if (parameter != null) { converted[parameter.OriginName] = MergeParameter(parameter, converted, table); } } if (item.IsRequired) { string val = converted[item.OriginName] as string; if (string.IsNullOrEmpty(val) && string.IsNullOrEmpty(item.DefaultValue) == false) { if (string.IsNullOrEmpty(item.DefaultValue) == false) { System.Text.RegularExpressions.Match match = defaultTypeMatcher.Match(item.DefaultValue); val = ""; if (match.Success) { switch (match.Groups["type"].Value.ToUpper()) { case "RANDOM": val = System.Guid.NewGuid().ToString(); val = val.Substring(val.Length - 10, 10); break; } } else { val = item.DefaultValue; } converted[item.OriginName] = val; } } } #if FY if (string.IsNullOrEmpty(str)) { converted[item.OriginName] = TestFile(item.EntName); } #endif } return(converted); }
private void ReadConverterRule(string fileName, string sheetName) { _converter.Clear(); using (Office.Excel.ForwardExcelReader reader = new Office.Excel.ForwardExcelReader(fileName)) { reader.Open(); Office.Excel.ForwardReadWorksheet sheet = reader.Activate(sheetName) as Office.Excel.ForwardReadWorksheet; if (sheet == null) { return; } Hashtable columnHeader = new Hashtable(); if (sheet.ReadNextRow()) { while (sheet.ReadNextCell(false)) { columnHeader.Add(sheet.CurrentCell.ColumnIndex, sheet.GetContent()); } } string str; object content; while (sheet.ReadNextRow()) { ConverterParameter para = new ConverterParameter(); while (sheet.ReadNextCell(false)) { content = sheet.GetContent(); str = content == null ? "" : content.ToString(); switch (columnHeader[sheet.CurrentCell.ColumnIndex] as string) { case "字符替换": para.ValueReplaceGroup = str; break; case "企业内部参数编号": para.EntName = str; break; case "原参数名称": para.OriginName = str; break; case "填报级数分割符": para.SplitParten = str; break; case "参数拆分正则": para.RegexParten = str; break; case "是否为表格": para.IsSheetRelation = str == "是"; break; case "参数合并": para.MergePara = str; break; case "参数合并时使用的分割符号": para.MergeSpliter = str; break; case "默认值": para.DefaultValue = str; break; case "是否使用原始内容": para.UseOriginString = str == "是"; break; case "计算表达式": para.CalcExpression = str; break; case "是否必填": para.IsRequired = str == "是"; break; case "元素类型": para.ElementType = str; break; } } if (string.IsNullOrEmpty(para.OriginName)) { continue; } _converter.Add(para); } } }
// 此合并方法仅支持多级参数合并时所有参数长度均相同的情况 private string MergeParameter(ConverterParameter parameter, Hashtable convertedData, Hashtable originData) { StringBuilder result = new StringBuilder(); string[] parameters = parameter.MergePara.Split(new char[] { '&' }, System.StringSplitOptions.RemoveEmptyEntries); List <string> strList = new List <string>(); foreach (var str in parameters) { if (mergeParameter.ContainsKey(str)) { if (convertedData.ContainsKey(str)) { strList.Add(convertedData[str] as string); } else { strList.Add(this.GetValue(originData[str], parameter)); } } } // 若合并所需的参数名称不全,则回滚所有操作 if (strList.Count != parameters.Length) { foreach (var str in parameters) { if (mergeParameter.ContainsKey(str) == false) { mergeParameter.Add(str, parameter); } } return(""); } List <System.Text.RegularExpressions.Match> matchList = new List <System.Text.RegularExpressions.Match>(); foreach (var item in strList) { if (item != null) { matchList.Add(mergeHelper.Match(item)); } } bool merge = false; string lastSymbol; do { lastSymbol = ""; for (int i = 0; i < matchList.Count; i++) { if (matchList[i].Success) { if (string.IsNullOrEmpty(lastSymbol) || lastSymbol == matchList[i].Groups["symbol"].Value) { lastSymbol = matchList[i].Groups["symbol"].Value; result.Append(matchList[i].Groups["value"].Value); //if (string.IsNullOrEmpty(parameter.MergeSpliter) == false) // result.Append(parameter.MergeSpliter); matchList[i] = matchList[i].NextMatch(); } else if (matchList[i].Groups["symbol"].Value != lastSymbol) { matchList[i] = matchList[i].NextMatch(); } if (string.IsNullOrEmpty(parameter.MergeSpliter) == false && i != matchList.Count - 1) { result.Append(parameter.MergeSpliter); } } } result.Append(lastSymbol); } while (merge); return(result.ToString()); }
internal virtual string GetValue(object value, ConverterParameter parameter) { return(value as string); }