private static void FillMatrixRowsToWorksheet(WorkbookNode workbook, WfMatrix matrix, bool roleAsPerson) { NamedLocationCollection locations = workbook.Names.ToLocations(); locations.SortByColumn(); WorksheetNode worksheet = workbook.Worksheets[GetWorksheet(locations)]; int startRowIndex = GetStartRow(locations); int currentRowIndex = -1; foreach (WfMatrixRow matrixRow in matrix.Rows) { RowNode row = new RowNode(); if (currentRowIndex == -1) { currentRowIndex = startRowIndex + 1; row.Index = currentRowIndex; } foreach (CellLocation location in locations) { CellNode cell = new CellNode(); WfMatrixCell mCell = matrixRow.Cells.Find(c => c.Definition.DimensionKey == location.Name); if (mCell != null) { cell.Data.Value = GetCellValue(roleAsPerson, mCell, matrixRow); } row.Cells.Add(cell); } worksheet.Table.Rows.Add(row); } }
private static int GetStartRow(NamedLocationCollection locations) { int result = 0; if (locations.Count > 0) { result = locations[0].Row; } return(result); }
private static string GetWorksheet(NamedLocationCollection locations) { string worksheetName = string.Empty; if (locations.Count > 0) { worksheetName = locations[0].WorksheetName; } return(worksheetName); }
private static void ImportExcelXml(Stream stream, WfMatrix matrix, Action notifier) { WorkbookNode workbook = new WorkbookNode(); workbook.Load(stream); ExceptionHelper.FalseThrow(workbook.Worksheets.Contains("Matrix"), "The workbook must contains a 'Matrix' worksheet."); 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) { int currentVirtualRow = baseRowIndex; int count = table.Rows.Count - currentRowIndex; for (int i = 0; i < count; i++) { RowNode row = table.Rows[currentRowIndex]; if (row.Index > 0) { currentVirtualRow = row.Index; } else { currentVirtualRow++; } GenerateMatrixRow(matrix, row, fieldLocations, i); if (notifier != null) { notifier(); } currentRowIndex++; } } WfMatrixAdapter.Instance.DeleteByProcessKey(matrix.ProcessKey); WfMatrixAdapter.Instance.Update(matrix); }
private SOARolePropertyRow GenerateMatrixRow(SOARole role, RowNode rowNode, NamedLocationCollection locations, int index) { SOARolePropertyRow mRow = new SOARolePropertyRow(role); mRow.RowNumber = index; int emptyCellCount = 0; foreach (var row in this.Definition) { CellLocation location = locations[row.Name]; CellNode cell = rowNode.GetCellByIndex(location.Column); SOARolePropertyValue mCell = new SOARolePropertyValue(row); mCell.Value = cell.Data.InnerText.Trim(); mRow.Values.Add(mCell); switch (row.Name) { case "Operator": mRow.Operator = cell.Data.InnerText; break; case "OperatorType": SOARoleOperatorType opType = SOARoleOperatorType.User; Enum.TryParse(cell.Data.InnerText, out opType); mRow.OperatorType = opType; break; default: if (mCell.Value.IsNullOrEmpty()) { emptyCellCount++; } break; } } role.Rows.Add(mRow); return(mRow); }
protected void uploadProgress_DoUploadProgress(HttpPostedFile file, UploadProgressResult result) { 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("Matrix"), "The workbook must contains a 'Matrix' worksheet."); NamedLocationCollection fieldLocations = workbook.Names.ToLocations(); TableNode table = workbook.Worksheets["Matrix"].Table; StringBuilder strB = new StringBuilder(); 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++; } if (strB.Length > 0) { strB.Append("\n"); } strB.AppendFormat("Processed={0}, Row={1}:", (i + 1), currentVirtualRow); foreach (CellLocation location in fieldLocations) { CellNode cell = row.GetCellByIndex(location.Column); strB.AppendFormat(";Name={0}, Value={1}", location.Name, cell != null ? cell.Data.Value : string.Empty); } status.CurrentStep = i; status.Response(); currentRowIndex++; } status.CurrentStep = status.MaxStep; status.Response(); } result.DataChanged = true; result.CloseWindow = false; result.ProcessLog = strB.ToString(); }
private static WfMatrixRow GenerateMatrixRow(WfMatrix matrix, RowNode rowNode, NamedLocationCollection locations, int index) { WfMatrixRow mRow = new WfMatrixRow(matrix); mRow.RowNumber = index; int emptyCellCount = 0; foreach (WfMatrixDimensionDefinition dd in matrix.Definition.Dimensions) { CellLocation location = locations[dd.DimensionKey]; CellNode cell = rowNode.GetCellByIndex(location.Column); WfMatrixCell mCell = new WfMatrixCell(dd); mCell.StringValue = cell.Data.InnerText.Trim(); mRow.Cells.Add(mCell); switch (dd.DimensionKey) { case "Operator": mRow.Operator = cell.Data.InnerText; break; case "OperatorType": WfMatrixOperatorType opType = WfMatrixOperatorType.Person; Enum.TryParse(cell.Data.InnerText, out opType); mRow.OperatorType = opType; break; default: if (mCell.StringValue.IsNullOrEmpty()) { emptyCellCount++; } break; } } if (emptyCellCount >= matrix.Definition.Dimensions.Count - 2) { //如果每一列都为空(不算Operator和OperatorType),那么认为整行都为空 mRow = null; } else { matrix.Rows.Add(mRow); } return(mRow); }
private SOARolePropertyRow GenerateMatrixRow(SOARole role, RowNode rowNode, NamedLocationCollection locations, int index) { SOARolePropertyRow mRow = new SOARolePropertyRow(role); mRow.RowNumber = index; int emptyCellCount = 0; foreach (var row in this.Definition) { CellLocation location = locations[row.Name]; CellNode cell = rowNode.GetCellByIndex(location.Column); SOARolePropertyValue mCell = new SOARolePropertyValue(row); mCell.Value = cell.Data.InnerText.Trim(); mRow.Values.Add(mCell); switch (row.Name) { case "Operator": mRow.Operator = cell.Data.InnerText; break; case "OperatorType": SOARoleOperatorType opType = SOARoleOperatorType.User; Enum.TryParse(cell.Data.InnerText, out opType); mRow.OperatorType = opType; break; default: if (mCell.Value.IsNullOrEmpty()) emptyCellCount++; break; } } role.Rows.Add(mRow); return mRow; }
private static int GetStartRow(NamedLocationCollection locations) { int result = 0; if (locations.Count > 0) result = locations[0].Row; return result; }
/// <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 static string GetWorksheet(NamedLocationCollection locations) { string worksheetName = string.Empty; if (locations.Count > 0) worksheetName = locations[0].WorksheetName; return worksheetName; }
//导出带数据的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); } }