/// <summary> /// 从Excel文件中读取树节点的父子级关系。 /// </summary> /// <returns></returns> private TreeValue InitTreeNode() { TreeValue value = null, parent = null, root = null; Hashtable table = new Hashtable(); using (Office.Excel.ForwardExcelReader reader = new Office.Excel.ForwardExcelReader(@"..\..\..\演示数据\CCC\目录字典.xlsx")) { reader.Open(); Office.Excel.ForwardReadWorksheet sheet = reader.Activate(1) as Office.Excel.ForwardReadWorksheet; sheet.ReadFollowingRow(2); string id = null, name = null, parentId = null; object content = null; do { while (sheet.ReadNextCell(false)) { content = sheet.GetContent(); switch (sheet.CurrentCell.ColumnIndex) { case 1: id = content == null ? null : content.ToString(); break; case 2: name = content == null ? null : content.ToString(); break; case 3: parentId = content == null ? null : content.ToString(); break; } } if (id == null || name == null) { continue; } value = new TreeValue(name, parentId, null); table.Add(id, value); } while (sheet.ReadNextRow()); } foreach (DictionaryEntry entry in table) { value = entry.Value as TreeValue; if (value != null) { if (value.Parent == null) { root = value; } else { parent = table[value.Parent.Name] as TreeValue; parent.AddChild(value); } } } return(root); }
/// <summary> /// 从Excel文件中读取树节点的父子级关系。 /// </summary> /// <returns></returns> private TreeValue InitTreeNode() { TreeValue value = null, parent = null, root = null; string fileName = FileHelper.GetPublicPage(FillRuleVerson, FillType); string ruleFilePath = FileHelper.GetFillVersionByName(FillRuleVerson); Hashtable table = new Hashtable(); using (Office.Excel.ForwardExcelReader reader = new Office.Excel.ForwardExcelReader(string.Format(@"{0}\{1}", ruleFilePath, fileName))) { reader.Open(); Office.Excel.ForwardReadWorksheet sheet = reader.Activate(1) as Office.Excel.ForwardReadWorksheet; sheet.ReadFollowingRow(2); string name = null, parentName = null; object content = null; do { while (sheet.ReadNextCell(false)) { content = sheet.GetContent(); switch (sheet.CurrentCell.ColumnIndex) { case 1: name = content == null ? null : content.ToString(); break; case 2: parentName = content == null ? null : content.ToString(); break; } } if (name == null) { continue; } value = new TreeValue(name, parentName, null); table.Add(name, value); } while (sheet.ReadNextRow()); } foreach (DictionaryEntry entry in table) { value = entry.Value as TreeValue; if (value != null) { if (value.Parent == null) { root = value; } else { parent = table[value.Parent.Name] as TreeValue; parent.AddChild(value); } } } return(root); }
/// <summary> /// 从指定文件中读取需填报的参数值。 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private TreeValue ReadData(string fileName, Hashtable data, Hashtable fillParameters) { try { TreeValue root = InitTreeNode(); Hashtable treeDir = GetTreeDir(root); Hashtable current = null; TreeValue lastTreeNode = root; Hashtable columnHeader = new Hashtable(); using (Office.Excel.ForwardExcelReader reader = new Office.Excel.ForwardExcelReader(fileName)) { reader.Open(); Office.Excel.ForwardReadWorksheet sheet = reader.Activate(1) as Office.Excel.ForwardReadWorksheet; if (sheet != null) { object header = null; if (sheet.ReadNextRow() && sheet.CurrentRowIndex == 1) { while (sheet.ReadNextCell(false)) { header = sheet.GetContent(); columnHeader.Add(sheet.CurrentCell.ColumnIndex, header == null ? "" : header.ToString()); } } FillValue3C fillValue = null; object content = null; string str = null; bool nextRow = false; while (sheet.ReadNextRow()) { fillValue = new FillValue3C(); while (sheet.ReadNextCell(false)) { content = sheet.GetContent(); str = content == null ? "" : content.ToString(); switch (columnHeader[sheet.CurrentCell.ColumnIndex] as string) { case "序号": fillValue.Key = str; break; case "参数项的值": fillValue.SetValue(string.Format("{0}{1}", string.IsNullOrEmpty(fillValue.Value) ? "" : string.Format("{0}\t", fillValue.Value), str)); break; case "参数项名称": Match match = paraNameMatcher.Match(str); if (match.Success && match.Groups["name"].Success) { str = match.Groups["name"].Value.Trim(); } else { str = str.Trim(); } if (treeDir.ContainsKey(str)) { TreeValue parent = lastTreeNode; TreeValue child = null; while (parent != null) { child = parent.FindChild(str, null); if (child == null) { parent = parent.Parent; } else { break; } } if (child == null) { child = treeDir[str] as TreeValue; parent = child.Parent; } System.Diagnostics.Trace.Assert(child != null && parent != null); match = suffixMatcher.Match(fillValue.Key); if (match.Success) { string suffix = match.Groups["suffix"].Value; TreeValue temp = new TreeValue(child.Name, suffix); temp.CopyFrom(child); parent.AddChild(temp); child = temp; } current = child.Values; lastTreeNode = child; nextRow = true; // 当前行为目录行,忽略其它内容 } break; case "附件": fillValue.PublicAttachFile = str; break; } if (nextRow) { break; } } if (string.IsNullOrEmpty(fillValue.Key) == false) { if (nextRow) { nextRow = false; continue; } AddFillValue(fillValue, lastTreeNode, current == null ? data : current); //if (current == null) // 若当前值为树节点下的值则将其添加到当前树节点的Values中, // _data.Add(fillValue.Key, fillValue); // 否则(如证书名称、厂商关系)添加到管理器的数据中。 //else // current.Add(fillValue.Key, fillValue); } } } } return(root); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.StackTrace + ex.Message); throw; } }
/// <summary> /// 从指定文件中读取需填报的参数值。 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private TreeValue ReadData(string fileName) { TreeValue root = InitTreeNode(); Hashtable treeDir = GetTreeDir(root); Hashtable current = null; TreeValue lastTreeNode = root; Hashtable columnHeader = new Hashtable(); using (Office.Excel.ForwardExcelReader reader = new Office.Excel.ForwardExcelReader(fileName)) { reader.Open(); Office.Excel.ForwardReadWorksheet sheet = reader.Activate(1) as Office.Excel.ForwardReadWorksheet; if (sheet != null) { object header = null; if (sheet.ReadNextRow() && sheet.CurrentRowIndex == 1) { while (sheet.ReadNextCell(false)) { header = sheet.GetContent(); columnHeader.Add(sheet.CurrentCell.ColumnIndex, header == null ? "" : header.ToString()); } } FillValue3C fillValue = null; object content = null; string str = null; bool nextRow = false; while (sheet.ReadNextRow()) { fillValue = new FillValue3C(); while (sheet.ReadNextCell(false)) { content = sheet.GetContent(); str = content == null ? "" : content.ToString(); switch (columnHeader[sheet.CurrentCell.ColumnIndex] as string) { case "序号": fillValue.Key = str; break; case "参数项的值": fillValue.SetValue(str); break; case "参数项名称": str = str.Trim(); if (treeDir.ContainsKey(str)) { TreeValue parent = lastTreeNode; TreeValue child = null; while (parent != null) { child = parent.FindChild(str, null); if (child == null) { parent = parent.Parent; } else { break; } } if (child == null) { child = treeDir[str] as TreeValue; parent = child.Parent; } System.Diagnostics.Trace.Assert(child != null && parent != null); Match match = Main_3C.suffixMatcher.Match(fillValue.Key); if (match.Success) { string suffix = match.Groups["suffix"].Value; TreeValue temp = new TreeValue(child.Name, suffix); temp.CopyFrom(child); parent.AddChild(temp); child = temp; } current = child.Values; lastTreeNode = child; nextRow = true; // 当前行为目录行,忽略其它内容 } break; case "附件": fillValue.AttachFile = str; break; } if (nextRow) { break; } } if (string.IsNullOrEmpty(fillValue.Key) == false) { if (nextRow) { nextRow = false; continue; } if (current == null) { _data.Add(fillValue.Key, fillValue); } else { current.Add(fillValue.Key, fillValue); } } } } } return(root); }