Example #1
0
        public void PrepareExport(GridControl grid, bool exportColors, HashSet <GridColumn> excludeColumn,
                                  Action <ExcelExportData, IDocumentPresenter, ExcelExportProgressCommand> afterExportComplete,
                                  ExcelExportProgressCommand progressCommand, IDocumentPresenter document)
        {
            Rows = grid.VisibleRowCount;
            progressCommand.Rows       = Rows;
            progressCommand.CurrentRow = 0;
            var tv = grid.View as TableView;
            var eo = new ExcelExportObject
            {
                VisibleColumns =
                    grid.Columns.Where(
                        c => !excludeColumn.Contains(c) && !(c is GridExpandColumn) && c.Visible)
                    .OrderBy(c => c.VisibleIndex)
                    .ToList(),
                RowBrushes          = new Brush[grid.VisibleRowCount],
                CurrentRow          = 0,
                PrevRow             = 0,
                ExportColors        = exportColors && tv != null,
                Grid                = grid,
                RowData             = new List <object>(),
                ProgressCommand     = progressCommand,
                AfterExportComplete = afterExportComplete,
                Document            = document
            };

            Headers = new RealColumnHeadersCollection(eo.VisibleColumns.Select(c => GetHeaderString(c.Header)).ToArray());
            Columns = eo.VisibleColumns.Count;
            NumberFormatting.ClearFormats();
            for (int i = 0; i < eo.VisibleColumns.Count; i++)
            {
                NumberFormatting.AddFormat(i, eo.VisibleColumns[i].FieldType);
            }
            eo.CellBrushes = new Brush[grid.VisibleRowCount, eo.VisibleColumns.Count];
            eo.Cells       = new string[grid.VisibleRowCount, eo.VisibleColumns.Count];
            if (eo.ExportColors)
            {
                eo.PhantomCells = new CellContentPresenter[32, Columns];
                eo.PhantomRows  = new GridRowContent[32];
                for (int i = 0; i < 32; i++)
                {
                    eo.PhantomRows[i] = new GridRowContent()
                    {
                        Style = tv.RowStyle
                    };
                    for (int j = 0; j < Columns; j++)
                    {
                        eo.PhantomCells[i, j] = new CellContentPresenter {
                            Style = eo.VisibleColumns[j].CellStyle
                        }
                    }
                    ;
                }
            }

            DoExport(eo);
        }
Example #2
0
        private void DoExport(ExcelExportObject eo)
        {
            try
            {
                var rows     = Math.Min(32, Rows - eo.CurrentRow) - 1;
                var rowIndex = 0;
                while (rows >= 0)
                {
                    var rowHandle = eo.Grid.GetRowHandleByVisibleIndex(eo.CurrentRow);
                    if (rowHandle < 0)
                    {
                        eo.CurrentRow++;
                        rows--;
                        continue;
                    }
                    var data = eo.Grid.GetRow(rowHandle);
                    eo.RowData.Add(data);
                    RowData rd = null;
                    if (eo.ExportColors)
                    {
                        rd = new RowData(new VisualDataTreeBuilder(eo.Grid.View, null, null, null))
                        {
                            Row = data
                        };
                        eo.PhantomRows[rowIndex].DataContext = rd;
                    }

                    int j = 0;
                    foreach (var column in eo.VisibleColumns)
                    {
                        try
                        {
                            eo.Cells[rowIndex, j] = eo.Grid.GetCellDisplayText(rowHandle, column);
                        }
                        catch { }

                        if (eo.ExportColors)
                        {
                            eo.PhantomCells[rowIndex, j].DataContext = new EditGridCellData(rd);
                        }
                        j++;
                    }

                    eo.CurrentRow++;
                    rowIndex++;
                    rows--;
                }

                eo.Grid.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action <ExcelExportObject>(ExportStep), eo);
            }
            catch
            {
                eo.ProgressCommand.CurrentStep = 4;
                throw;
            }
        }
Example #3
0
        private void ExportStep(ExcelExportObject eo)
        {
            try
            {
                if (eo.ExportColors)
                {
                    for (int i = eo.PrevRow; i < eo.CurrentRow; i++)
                    {
                        eo.RowBrushes[i] = eo.PhantomRows[i - eo.PrevRow].Background;
                        for (int j = 0; j < eo.VisibleColumns.Count; j++)
                        {
                            eo.CellBrushes[i, j] = eo.PhantomCells[i - eo.PrevRow, j].Background;
                        }
                    }
                }

                eo.PrevRow = eo.CurrentRow;
                eo.ProgressCommand.CurrentRow = eo.CurrentRow;
                if (eo.CurrentRow < Rows)
                {
                    eo.Grid.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action <ExcelExportObject>(DoExport), eo);
                }
                else
                {
                    eo.ProgressCommand.CurrentStep = 1;
                    if (!eo.ExportColors)
                    {
                        for (int i = 0; i < Rows; i++)
                        {
                            eo.RowBrushes[i] = Brushes.Transparent;
                            for (int j = 0; j < Columns; j++)
                            {
                                eo.CellBrushes[i, j] = Brushes.Transparent;
                            }
                        }
                    }
                    RowBrushes  = new RealRowBrushCollection(eo.RowBrushes);
                    CellBrushes = new RealCellBrushCollection(eo.CellBrushes);
                    Cells       = new RealCellsCollection(eo.Cells);
                    _rowData    = eo.RowData.ToList();

                    eo.Grid.Dispatcher.BeginInvoke(DispatcherPriority.Background, eo.AfterExportComplete, this, eo.Document, eo.ProgressCommand);
                }
            }
            catch
            {
                eo.ProgressCommand.CurrentStep = 4;
                throw;
            }
        }