/// <summary> /// 解析单元格公式 /// </summary> /// <param name="cell"></param> /// <param name="pairValue"></param> /// <returns>是否包含公式</returns> private static bool ParseCellFormula(TplCell cell, ref string pairValue) { var formulaStartIndex = pairValue.IndexOf(TemplateFlags.CellValueFormulaStart); if (formulaStartIndex < 0) { cell.TplValueColName = pairValue.Trim().ToUpper(); return(false); } cell.Formula = new CellForumla { //分隔符(前作为公式名 Formula = (formulaStartIndex == 0) ? "" : pairValue.Substring(0, formulaStartIndex).ToLower() }; //截取()内值 var formulaEndIndex = pairValue.IndexOf(TemplateFlags.CellValueFormulaEnd); if (formulaEndIndex < 0) { Console.WriteLine("Warning:: formula not closed. [" + pairValue + "]"); pairValue = pairValue.Substring(formulaStartIndex + 1); } else { pairValue = pairValue.Substring(formulaStartIndex + 1, (formulaEndIndex - formulaStartIndex) - 1); } return(true); }
private static TplLine ParseLine(Worksheet sheet, TplBlock block, int startCol, int startRow) { var colCount = block.ColumnsCount; var line = new TplLine { TplRange = RangeHelper.GetRange(sheet, startCol, startRow, colCount, 1) }; for (var i = 0; i < colCount; i++) { var range = RangeHelper.GetCell(sheet, startCol + i, startRow); var cell = new TplCell { TplRange = range, LastColIndex = i + startCol }; var str = range.Value2 as string; if (!string.IsNullOrEmpty(str)) { ParseCell(block, line, cell, str.Trim()); } line.CellList.Add(cell); } return(line); }
public TplCell Copy() { var cell = new TplCell { GroupAlign = GroupAlign, LastColIndex = LastColIndex, TplGroupColumnName = TplGroupColumnName, TplValueColName = TplValueColName, TplFormat = TplFormat, TplTextContent = TplTextContent, TplDefaultContent = TplDefaultContent, UseR1C1Formula = UseR1C1Formula, HgOption = HgOption }; if (Formula != null) { cell.Formula = Formula.Copy(); } cell.LastGroupedValue = null; return(cell); }
private static void ParseCell(TplBlock block, TplLine line, TplCell cell, string text) { text = text.Trim(); if (text.StartsWith("R1C1:")) { cell.UseR1C1Formula = true; cell.TplTextContent = text.Substring(5); } else if (text[0] != TemplateFlags.CellStart) { cell.TplTextContent = text; } else { var index = text.IndexOf(TemplateFlags.CellEnd); if (index > 0) { if ((index + 1) != text.Length) { cell.TplTextContent = text.Substring(index + 1); } text = text.Substring(1, index - 1); } cell.TplValueColName = text; var pair = ParseKeyValuePair(text); cell.TplFormat = GetPairValue(pair, TemplateFlags.CellFormat); if (!string.IsNullOrEmpty(cell.TplFormat)) { cell.TplFormat = cell.TplFormat.ToLower(); } cell.MergeOption = ParseMergeOption(GetPairValue(pair, TemplateFlags.CellMergeOption)); #region CellGroupAlign if (GetPairValue(pair, TemplateFlags.CellVGroup) != null) { cell.GroupAlign = GroupAlign.Vertical; cell.TplGroupColumnName = GetPairValue(pair, TemplateFlags.CellVGroup).Trim().ToUpper(); Console.WriteLine( string.Format("Contains Vg GroupColName:{0}", cell.TplGroupColumnName)); } else if (GetPairValue(pair, TemplateFlags.CellHGroup) != null) { cell.GroupAlign = GroupAlign.Horizontal; cell.TplGroupColumnName = GetPairValue(pair, TemplateFlags.CellHGroup).ToUpper(); line.ContainsHGroup = true; var insertOption = GetLineInsertOption(GetPairValue(pair, TemplateFlags.CellHGroupOption)); if ((insertOption != InsertOption.AfterChange) && (insertOption != InsertOption.BeforeChange)) { insertOption = InsertOption.AfterChange; } cell.HgOption = insertOption; Console.WriteLine( string.Format("Contains Hg GroupColName:{0},LineInsertOption:{1}", cell.TplGroupColumnName, insertOption)); } #endregion CellGroupAlign var pairValue = GetPairValue(pair, TemplateFlags.CellValue); if (string.IsNullOrEmpty(pairValue)) { if (!string.IsNullOrEmpty(cell.TplGroupColumnName)) { //无值(v)标记时以分组列字段绑定 cell.TplValueColName = cell.TplGroupColumnName; } } else { cell.TplDefaultContent = GetPairValue(pair, TemplateFlags.CellDefaultContent); if (ParseCellFormula(cell, ref pairValue)) { var groupKey = ParseCellFormulaGroupKey(block, line, cell.Formula.Formula, pairValue); Console.WriteLine(groupKey.ToString()); cell.Formula.KeyList.Add(groupKey); block.GroupKeyList.Add(groupKey.Copy()); } } } }