/// <summary> /// Exports a single grid row to the specified worksheet. /// /// </summary> protected virtual void ExportRow(GridRecord gridRecord, Worksheet worksheet, ref int rowIndex, int columnOffset, bool hasHiddenCols, int outlineLevel, bool hidden, string gridCssClass, string itemCssClass) { WorksheetRow worksheetRow = worksheet.Rows[rowIndex]; worksheetRow.OutlineLevel = outlineLevel; worksheetRow.Hidden = hidden; int rowIndex1 = rowIndex; ExcelRowExportingEventArgs e = new ExcelRowExportingEventArgs(worksheet, worksheetRow, rowIndex1, columnOffset, outlineLevel, gridRecord, false, false, false); this.OnRowExporting(e); if (e.Cancel) return; WebDataGrid webDataGrid = gridRecord.Owner.ControlMain as WebDataGrid; int num = 0; foreach (ControlDataField controlDataField in (IVisibleItemsEnumerable)webDataGrid.Fields) { if (!controlDataField.Hidden && !controlDataField.HiddenByParent) { this.ExportCell(gridRecord.Items[controlDataField.Index], worksheetRow, rowIndex, columnOffset + num, gridCssClass, itemCssClass); ++num; } } ++rowIndex; if (gridRecord is ContainerGridRecord && ((ContainerGridRecord)gridRecord).HasRowIslands) this.ExportRowIslands(((ContainerGridRecord)gridRecord).RowIslands, worksheet, ref rowIndex, columnOffset + 1, outlineLevel + 1); this.OnRowExported(new ExcelRowExportedEventArgs(worksheet, worksheetRow, rowIndex1, columnOffset, outlineLevel, gridRecord, false, false, false)); }
/// <summary> /// Raises the Row Exporting Event. /// /// </summary> protected virtual void OnRowExporting(ExcelRowExportingEventArgs e) { ExcelRowExportingEventHandler exportingEventHandler = this.Events[WebExcelExporter.EventRowExporting] as ExcelRowExportingEventHandler; if (exportingEventHandler == null) return; exportingEventHandler((object)this, e); }
/// <summary> /// Exports a single flat grid to the specified worksheet. /// Exporting starts from the defined rowIndex and columnOffset. /// The rowIndex is updated to point after the last exported row. /// /// </summary> protected virtual void ExportGrid(WebDataGrid grid, Worksheet worksheet, ref int rowIndex, int columnOffset, int outlineLevel) { bool hidden = grid is ContainerGrid && !this.IsGridVisible((ContainerGrid)grid); bool hasHiddenCols = false; foreach (GridField gridField in (CollectionBase)grid.Fields) { if (gridField.Hidden || gridField.HiddenByParent) { hasHiddenCols = true; break; } } string styleClassString1 = grid.RunBot.StyleBot.GetStyleClassString(0, grid.ControlStyle.CssClass); string styleClassString2 = grid.RunBot.StyleBot.GetStyleClassString(5, grid.ItemCssClass); string styleClassString3 = grid.RunBot.StyleBot.GetStyleClassString(6, grid.AltItemCssClass); if (grid.ShowHeader) { WorksheetRow worksheetRow1 = worksheet.Rows[rowIndex]; worksheetRow1.OutlineLevel = outlineLevel; worksheetRow1.Hidden = hidden; ExcelRowExportingEventArgs exportingEventArgs = new ExcelRowExportingEventArgs(worksheet, worksheetRow1, rowIndex, columnOffset, outlineLevel, true, false, false); string styleClassString4 = grid.RunBot.StyleBot.GetStyleClassString(1); if (grid.Fields.Count == 0) grid.EnsureDataBoundInternal(); int columnIndex1 = 0; if (grid.HasHeaderLayout) { for (int index = 0; index < grid.HeaderLayout.Count; ++index) { int columnIndex2 = 0; WorksheetRow worksheetRow2 = worksheet.Rows[rowIndex]; worksheetRow2.OutlineLevel = outlineLevel; worksheetRow2.Hidden = hidden; ExcelRowExportingEventArgs e = new ExcelRowExportingEventArgs(worksheet, worksheetRow2, rowIndex, columnOffset, outlineLevel, true, false, false); this.OnRowExporting(e); if (!e.Cancel) { foreach (ControlDataField controlDataField in (IVisibleItemsEnumerable)grid.HeaderLayout[index]) { if (!controlDataField.Hidden && !controlDataField.HiddenByParent && (!(controlDataField is GroupField) || !((GroupField)controlDataField).HiddenByChildren)) { GridFieldCaption caption = controlDataField.HeaderCaption as GridFieldCaption; WorksheetCell wsCell = worksheetRow2.Cells[columnOffset + columnIndex2]; int rowSpan = controlDataField.RowSpanInternal == 0 ? 1 : controlDataField.RowSpanInternal; int colSpan = controlDataField is GroupField ? (controlDataField as GroupField).ColSapn : 1; for (; wsCell.AssociatedMergedCellsRegion != null; wsCell = worksheetRow2.Cells[wsCell.AssociatedMergedCellsRegion.LastColumn + 1]) columnIndex2 = wsCell.AssociatedMergedCellsRegion.LastColumn + 1 - columnOffset; this.ExportGridFieldCaption(grid, caption, worksheetRow2, wsCell, rowIndex, columnIndex2, styleClassString1, styleClassString4, true, rowSpan, colSpan, columnOffset); columnIndex2 += colSpan; } } this.OnRowExported(new ExcelRowExportedEventArgs(worksheet, worksheetRow2, rowIndex, columnOffset, outlineLevel, true, false, false)); ++rowIndex; } } } else { ExcelRowExportingEventArgs e = new ExcelRowExportingEventArgs(worksheet, worksheetRow1, rowIndex, columnOffset, outlineLevel, true, false, false); this.OnRowExporting(e); if (e.Cancel) return; foreach (ControlDataField controlDataField in (IVisibleItemsEnumerable)grid.Fields) { if (!controlDataField.Hidden) { GridFieldCaption caption = controlDataField.HeaderCaption as GridFieldCaption; WorksheetCell wsCell = worksheetRow1.Cells[columnOffset + columnIndex1]; this.ExportGridFieldCaption(grid, caption, worksheetRow1, wsCell, rowIndex, columnIndex1, styleClassString1, styleClassString4, true, 0, 0, columnOffset); ++columnIndex1; } } this.OnRowExported(new ExcelRowExportedEventArgs(worksheet, worksheetRow1, rowIndex, columnOffset, outlineLevel, true, false, false)); ++rowIndex; } } bool flag1 = false; bool flag2 = false; VirtualScrolling behavior1 = grid.Behaviors.GetBehavior<VirtualScrolling>(); Paging behavior2 = grid.Behaviors.GetBehavior<Paging>(); if (this.DataExportMode == DataExportMode.AllDataInDataSource) { if (behavior1 != null && behavior1.Enabled) { behavior1.Enabled = false; flag1 = true; } if (behavior2 != null && behavior2.Enabled) { behavior2.Enabled = false; flag2 = true; } } int firstDataRowIndex = rowIndex; if (grid is ContainerGrid && ((ContainerGrid)grid).HasGroupedRows && ((ContainerGrid)grid).GroupedRows.Count > 0) { this.ExportGroupedRows(((ContainerGrid)grid).GroupedRows, worksheet, ref rowIndex, columnOffset, hasHiddenCols, outlineLevel, hidden); } else { foreach (GridRecord gridRecord in (ControlDataRecordCollection)grid.Rows) { string itemCssClass = styleClassString2; if (gridRecord.Index % 2 == 1) itemCssClass = itemCssClass + " " + styleClassString3; if (!string.IsNullOrEmpty(gridRecord.CssClass)) itemCssClass = itemCssClass + " " + gridRecord.CssClass; this.ExportRow(gridRecord, worksheet, ref rowIndex, columnOffset, hasHiddenCols, outlineLevel, hidden, styleClassString1, itemCssClass); } } if (flag1) behavior1.Enabled = true; if (flag2) behavior2.Enabled = true; if (grid.ShowFooter) { WorksheetRow worksheetRow = worksheet.Rows[rowIndex]; worksheetRow.OutlineLevel = outlineLevel; worksheetRow.Hidden = hidden; ExcelRowExportingEventArgs e = new ExcelRowExportingEventArgs(worksheet, worksheetRow, rowIndex, columnOffset, outlineLevel, false, true, false); this.OnRowExporting(e); if (e.Cancel) return; string styleClassString4 = grid.RunBot.StyleBot.GetStyleClassString(3); int columnIndex = 0; foreach (ControlDataField controlDataField in (IVisibleItemsEnumerable)grid.Fields) { if (!controlDataField.Hidden && !controlDataField.HiddenByParent && (!(controlDataField is GroupField) || !((GroupField)controlDataField).HiddenByChildren)) { GridFieldFooterCaption fieldFooterCaption = controlDataField.FooterCaption as GridFieldFooterCaption; WorksheetCell wsCell = worksheetRow.Cells[columnOffset + columnIndex]; int colSpanResolved = fieldFooterCaption.ColSpanResolved; if (wsCell.AssociatedMergedCellsRegion == null) this.ExportGridFieldCaption(grid, (GridFieldCaption)fieldFooterCaption, worksheetRow, wsCell, rowIndex, columnIndex, styleClassString1, styleClassString4, false, 1, colSpanResolved, columnOffset); ++columnIndex; } } this.OnRowExported(new ExcelRowExportedEventArgs(worksheet, worksheetRow, rowIndex, columnOffset, outlineLevel, false, true, false)); ++rowIndex; } SummaryRow behavior3 = grid.Behaviors.GetBehavior<SummaryRow>(); if (behavior3 == null || !behavior3.Enabled) return; this.ExportSummaries(grid, worksheet, ref rowIndex, columnOffset, outlineLevel, hidden, styleClassString1, firstDataRowIndex); }
/// <summary> /// Exports grid SummaryRow to the specified worksheet. /// /// </summary> protected virtual void ExportSummaries(WebDataGrid grid, Worksheet worksheet, ref int rowIndex, int columnOffset, int outlineLevel, bool hidden, string gridCssClass, int firstDataRowIndex) { SummaryRow behavior = grid.Behaviors.GetBehavior<SummaryRow>(); int index1 = rowIndex - (grid.ShowFooter ? 2 : 1); bool renderingResolved = behavior.EnableCompactRenderingResolved; int summaryNumberVisible = behavior.ColumnSummaries.GetMaxSummaryNumberVisible(); int maxSummaryRows = behavior.ColumnSummaries.GetMaxSummaryRows(); string str1 = string.Empty; if (this.EnableStylesExport) str1 = behavior.ResolveCssClass(behavior.SummariesCssClass, 0); for (int index2 = 0; index2 < maxSummaryRows; ++index2) { if ((index2 < summaryNumberVisible || !renderingResolved) && (renderingResolved || behavior.ColumnSummaries.IsVisibleRow(index2))) { WorksheetRow worksheetRow = worksheet.Rows[rowIndex]; worksheetRow.OutlineLevel = outlineLevel; worksheetRow.Hidden = hidden; int rowIndex1 = rowIndex; ExcelRowExportingEventArgs e1 = new ExcelRowExportingEventArgs(worksheet, worksheetRow, rowIndex1, columnOffset, outlineLevel, false, false, true); this.OnRowExporting(e1); if (!e1.Cancel) { int columnIndex = columnOffset; foreach (ControlDataField summaryCell in (IVisibleItemsEnumerable)grid.Fields) { if (!summaryCell.Hidden && !summaryCell.HiddenByParent) { WorksheetCell worksheetCell = worksheetRow.Cells[columnIndex]; Summary summaryByOrder = behavior.ColumnSummaries.GetSummaryByOrder(summaryCell.Key, index2); SummarySetting summarySetting = (SummarySetting)null; ExcelCellExportingEventArgs e2 = new ExcelCellExportingEventArgs(worksheet, worksheetCell, rowIndex, columnIndex, outlineLevel, false, false, true); e2.Summary = summaryByOrder; this.OnCellExporting(e2); if (e2.Cancel) { ++columnIndex; } else { SummaryCellExportingEventArgs e3 = new SummaryCellExportingEventArgs(summaryCell, worksheet, worksheetCell, rowIndex, columnIndex, outlineLevel, false, false, true); e3.Summary = summaryByOrder; this.OnSummaryCellExporting(e3); if (e3.Cancel) { ++columnIndex; } else { if (summaryByOrder == null) { worksheetCell.Value = (object)behavior.EmptyFooterText; } else { SummaryRowSetting sumRowSetting = behavior.ColumnSettings[summaryCell.Key]; summarySetting = sumRowSetting != null ? (summaryByOrder.SummaryType == SummaryType.Custom ? sumRowSetting.SummarySettings.CustomSummaryByName(summaryByOrder.CustomSummaryName) : sumRowSetting.SummarySettings[summaryByOrder.SummaryType]) : (SummarySetting)null; string format1 = summarySetting == null ? (sumRowSetting == null ? behavior.FormatString : sumRowSetting.FormatString) : summarySetting.FormatString; if (summaryByOrder.SummaryType == SummaryType.Custom) worksheetCell.Value = (object)string.Format(format1, (object)summaryByOrder.CustomSummaryName, summaryByOrder.Value); else if (summaryByOrder.SummaryType == SummaryType.Custom) { worksheetCell.Value = (object)string.Format(format1, (object)summaryByOrder.CustomSummaryName, summaryByOrder.Value); } else { List<string> list = (List<string>)null; string str2 = string.Empty; string str3; if (grid is ContainerGrid && ((ContainerGrid)grid).HasChildGrids()) { list = new List<string>(); for (int index3 = firstDataRowIndex; index3 <= index1; ++index3) { if (worksheet.Rows[index3].OutlineLevel == outlineLevel) list.Add(worksheet.Rows[index3].Cells[columnIndex].ToString(CellReferenceMode.A1, false, true, true)); } str3 = string.Join(",", list.ToArray()); } else str3 = string.Format("{0}:{1}", (object)worksheet.Rows[firstDataRowIndex].Cells[columnIndex].ToString(CellReferenceMode.A1, false, true, true), (object)worksheet.Rows[index1].Cells[columnIndex].ToString(CellReferenceMode.A1, false, true, true)); int num1 = format1.IndexOf("{1"); string format2; if (num1 > -1) { string str4 = "\"" + format1.Substring(0, num1) + "\" & "; string str5 = format1.Substring(num1); int num2 = str5.IndexOf("}"); string str6 = str5.Insert(num2 + 1, " & \"") + "\""; format2 = "=" + str4 + str6; } else format2 = "=\"" + format1 + "\""; string format3 = string.Format(format2, (object)"{0}", (object)"{1}({3})").Replace("\"\" &", "").Replace("& \"\"", ""); if (summaryByOrder.SummaryType == SummaryType.Count && format3.Contains("{1")) format3 = format3.Replace("{1}({3})", "{1}({3}) + {2}({3})"); string[] strArray = this.GetExcelFunctionName(summaryByOrder.SummaryType).Split(new char[1] { '|' }, StringSplitOptions.RemoveEmptyEntries); string str7 = strArray.Length > 1 ? strArray[1] : ""; string str8 = string.Format(format3, (object)behavior.GetCultureInvariantSummaryString(summaryByOrder.SummaryType, summaryByOrder.CustomSummaryName), (object)strArray[0], (object)str7, (object)str3); if (list != null && (list.Count > 30 || summaryByOrder.SummaryType == SummaryType.Count)) worksheetCell.Value = (object)behavior.GetSummaryValue((GridField)summaryCell, summaryByOrder, behavior.ColumnSummaries[summaryCell.Key], sumRowSetting, false); else worksheetCell.ApplyFormula(str8); } } if (this.EnableStylesExport) { string classes = str1; if (summarySetting != null && !string.IsNullOrEmpty(summarySetting.CssClass)) classes = classes + " " + summarySetting.CssClass; if (summaryByOrder != null && !string.IsNullOrEmpty(summaryByOrder.CssClass)) classes = classes + " " + summaryByOrder.CssClass; CssSelector selector1 = new CssSelector(); selector1.AddClasses(string.Format("{0} {1}", (object)gridCssClass, (object)classes)); CssStyle styleObject1 = this._currentStyleSheet.GetStyleObject(selector1); this.ApplyCellFormatFromStyle(worksheetCell.Worksheet.Workbook, worksheetCell.CellFormat, styleObject1, grid); CssSelector selector2 = new CssSelector(); selector2.AddClasses(classes); CssStyle styleObject2 = this._currentStyleSheet.GetStyleObject(selector2); this.ApplyCellBorderFromStyle(worksheetCell.CellFormat, styleObject2); } SummaryCellExportedEventArgs e4 = new SummaryCellExportedEventArgs(summaryCell, worksheet, worksheetCell, rowIndex1, columnIndex, outlineLevel, false, false, true); e4.Summary = summaryByOrder; this.OnSummaryCellExported(e4); ExcelCellExportedEventArgs e5 = new ExcelCellExportedEventArgs(worksheet, worksheetCell, rowIndex1, columnIndex, outlineLevel, false, false, true); e5.Summary = summaryByOrder; this.OnCellExported(e5); ++columnIndex; } } } } ++rowIndex; this.OnRowExported(new ExcelRowExportedEventArgs(worksheet, worksheetRow, rowIndex1, columnOffset, outlineLevel, false, false, true)); } } } }