/// <summary> /// 导入Excel Xml格式的文件 /// </summary> /// <param name="stream"></param> private void ImportFromXml(Stream stream) { WorkbookNode workbook = new WorkbookNode(); workbook.Load(stream); ExceptionHelper.FalseThrow(workbook.Worksheets.Contains("Matrix"), "The workbook must contains a 'Matrix' worksheet."); SOARole role = null; ServiceBrokerContext.Current.SaveContextStates(); try { if (this.AppCodeName.IsNotEmpty() && this.RoleCodeName.IsNotEmpty()) { role = new SOARole(this.AppCodeName + ":" + this.RoleCodeName); } else { role = new SOARole(this.Definition) { ID = RoleID } }; role.Rows.Clear(); NamedLocationCollection fieldLocations = workbook.Names.ToLocations(); TableNode table = workbook.Worksheets["Matrix"].Table; int baseRowIndex = GetStartRow(fieldLocations); RowNode titleRow = table.GetRowByIndex(baseRowIndex); int currentRowIndex = table.Rows.IndexOf(titleRow) + 1; if (table.Rows.Count > currentRowIndex) { UploadProgressStatus status = new UploadProgressStatus(); status.CurrentStep = 1; status.MinStep = 0; status.MaxStep = table.Rows.Count - currentRowIndex; int currentVirtualRow = baseRowIndex; for (int i = status.MinStep; i < status.MaxStep; i++) { RowNode row = table.Rows[currentRowIndex]; if (row.Index > 0) { currentVirtualRow = row.Index; } else { currentVirtualRow++; } GenerateMatrixRow(role, row, fieldLocations, i); status.CurrentStep = i; status.Response(); currentRowIndex++; } status.CurrentStep = status.MaxStep; status.Response(); } //插入记录 SOARolePropertiesAdapter.Instance.Update(role); } finally { ServiceBrokerContext.Current.RestoreSavedStates(); } }
private RowNode GetOrAddRowNode(RowType entryRow) { var curr = RowNodeHead; if (curr == null) { RowNodeHead = new RowNode(entryRow); return(RowNodeHead); } var prevOfCurr = curr.Up; RowType currValue; int comparisonResult; RowNode entryNode; while (curr != null) { currValue = curr.Value; comparisonResult = currValue.CompareTo(entryRow); if (comparisonResult == 0) { return(curr); } else if (comparisonResult > 0) { var prevNode = curr.Up; var nextNode = curr; entryNode = new RowNode(entryRow); if (prevNode == null)//El nodo a insertar es first { RowNodeHead = entryNode; entryNode.Up = null; } else { prevNode.Down = entryNode; entryNode.Up = prevNode; } nextNode.Up = entryNode; entryNode.Down = nextNode; return(entryNode); } prevOfCurr = curr; curr = curr.Down; } //Como ahora curr es null tenemos que movernos de regreso a su 'prev' curr = prevOfCurr; currValue = curr.Value; comparisonResult = currValue.CompareTo(entryRow); if (comparisonResult == 0) { return(curr); } entryNode = new RowNode(entryRow); curr.Down = entryNode; entryNode.Up = curr; return(entryNode); }
protected void uploadProgress_DoUploadProgress(HttpPostedFile file, UploadProgressResult result) { string tag = uploadProgress.Tag; string postedData = uploadProgress.PostedData; const int baseRowIndex = 3; ExceptionHelper.FalseThrow(Path.GetExtension(file.FileName).ToLower() == ".xml", "'{0}' must be a xml file.", file.FileName); WorkbookNode workbook = new WorkbookNode(); workbook.Load(file.InputStream); ExceptionHelper.FalseThrow(workbook.Worksheets.Contains("PC Tracker Form"), "The workbook must contains a 'PC Tracker Form' worksheet."); Dictionary <string, CellLocation> fieldLocations = BuildNameColumnDictionary(workbook); TableNode table = workbook.Worksheets["PC Tracker Form"].Table; StringBuilder strB = new StringBuilder(); if (table.Rows.Count > 3) { int currentRowIndex = baseRowIndex; UploadProgressStatus status = new UploadProgressStatus(); status.CurrentStep = 1; status.MinStep = 0; status.MaxStep = table.Rows.Count - currentRowIndex; int currentVirtualRow = baseRowIndex; for (int i = status.MinStep; i < status.MaxStep; i++) { currentRowIndex = baseRowIndex + i; RowNode row = table.Rows[currentRowIndex]; if (row.Index > 0) { currentVirtualRow = row.Index; } else { currentVirtualRow++; } if (strB.Length > 0) { strB.Append("\n"); } strB.AppendFormat("Processed={0}, Row={1}:", (i + 1), currentVirtualRow); foreach (KeyValuePair <string, CellLocation> kp in fieldLocations) { CellNode cell = row.GetCellByIndex(kp.Value.Column); strB.AppendFormat(";Name={0}, Value={1}", kp.Key, cell != null ? cell.Data.Value : string.Empty); } status.CurrentStep = i; status.StatusText = string.Format("处理完成第{0:#,##0}行", i); status.Response(); Thread.Sleep(1000); //假装等待 } status.StatusText = "处理完成"; status.CurrentStep = status.MaxStep; status.Response(); } result.DataChanged = true; result.CloseWindow = false; result.ProcessLog = strB.ToString(); }
//导出带数据的Excel private static void FillMatrixRowsToWorksheet(WorkbookNode workbook, SOARolePropertyRowCollection rows) { NamedLocationCollection locations = workbook.Names.ToLocations(); locations.SortByColumn(); WorksheetNode worksheet = workbook.Worksheets[GetWorksheet(locations)]; int startRowIndex = GetStartRow(locations); int currentRowIndex = -1; foreach (SOARolePropertyRow matrixRow in rows) { RowNode row = new RowNode(); if (currentRowIndex == -1) { currentRowIndex = startRowIndex + 1; row.Index = currentRowIndex; } for (int i = 0; i < locations.Count; i++) { CellNode cell = new CellNode(); CellLocation location = locations[i]; SOARolePropertyValue propertyValue = matrixRow.Values.FindByColumnName(location.Name); string dataValue = null; if (propertyValue != null) { dataValue = propertyValue.Value; } else { switch (location.Name.ToLower()) { case "operatortype": dataValue = matrixRow.OperatorType.ToString(); break; case "operator": dataValue = matrixRow.Operator; break; } } if (dataValue != null) { cell.Data.Value = dataValue; } else { cell.Data.Value = string.Empty; } row.Cells.Add(cell); } worksheet.Table.Rows.Add(row); } }