/// <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); }