Beispiel #1
0
        private static Workbook GenerateWorkbook(RadPivotGrid pivot)
        {
            var export = pivot.GenerateExport();

            Workbook workbook = new Workbook();

            workbook.History.IsEnabled = false;

            var worksheet = workbook.Worksheets.Add();

            workbook.SuspendLayoutUpdate();
            int rowCount    = export.RowCount;
            int columnCount = export.ColumnCount;

            var allCells = worksheet.Cells[0, 0, rowCount - 1, columnCount - 1];

            allCells.SetFontFamily(new ThemableFontFamily(pivot.FontFamily));
            allCells.SetFontSize(12);
            allCells.SetFill(GenerateFill(pivot.Background));

            foreach (var cellInfo in export.Cells)
            {
                int rowStartIndex    = cellInfo.Row;
                int rowEndIndex      = rowStartIndex + cellInfo.RowSpan - 1;
                int columnStartIndex = cellInfo.Column;
                int columnEndIndex   = columnStartIndex + cellInfo.ColumnSpan - 1;

                CellSelection cellSelection = worksheet.Cells[rowStartIndex, columnStartIndex];

                var value = cellInfo.Value;
                if (value != null)
                {
                    cellSelection.SetValue(Convert.ToString(value));
                    cellSelection.SetVerticalAlignment((Telerik.Windows.Documents.Spreadsheet.Model.RadVerticalAlignment)RadVerticalAlignment.Center);
                    cellSelection.SetHorizontalAlignment(GetHorizontalAlignment(cellInfo.TextAlignment));
                    int indent = cellInfo.Indent;
                    if (indent > 0)
                    {
                        cellSelection.SetIndent(indent);
                    }
                }

                cellSelection = worksheet.Cells[rowStartIndex, columnStartIndex, rowEndIndex, columnEndIndex];

                SetCellProperties(cellInfo, cellSelection);
            }

            for (int i = 0; i < columnCount; i++)
            {
                var columnSelection = worksheet.Columns[i];
                columnSelection.AutoFitWidth();

                //NOTE: workaround for incorrect autofit.
                var newWidth = worksheet.Columns[i].GetWidth().Value.Value + 15;
                columnSelection.SetWidth(new ColumnWidth(newWidth, false));
            }

            workbook.ResumeLayoutUpdate();
            return(workbook);
        }
Beispiel #2
0
        private static Workbook GenerateWorkbook(RadPivotGrid pivot)
        {
            var export = pivot.GenerateExport();

            Workbook workbook = new Workbook();
            workbook.History.IsEnabled = false;

            var worksheet = workbook.Worksheets.Add();

            workbook.SuspendLayoutUpdate();
            int rowCount = export.RowCount;
            int columnCount = export.ColumnCount;

            var allCells = worksheet.Cells[0, 0, rowCount - 1, columnCount - 1];
            allCells.SetFontFamily(new ThemableFontFamily(pivot.FontFamily));
            allCells.SetFontSize(12);
            allCells.SetFill(GenerateFill(pivot.Background));

            foreach (var cellInfo in export.Cells)
            {
                int rowStartIndex = cellInfo.Row;
                int rowEndIndex = rowStartIndex + cellInfo.RowSpan - 1;
                int columnStartIndex = cellInfo.Column;
                int columnEndIndex = columnStartIndex + cellInfo.ColumnSpan - 1;

                CellSelection cellSelection = worksheet.Cells[rowStartIndex, columnStartIndex];

                var value = cellInfo.Value;
                if (value != null)
                {
                    cellSelection.SetValue(Convert.ToString(value));
                    cellSelection.SetVerticalAlignment((Telerik.Windows.Documents.Spreadsheet.Model.RadVerticalAlignment) RadVerticalAlignment.Center);
                    cellSelection.SetHorizontalAlignment(GetHorizontalAlignment(cellInfo.TextAlignment));
                    int indent = cellInfo.Indent;
                    if (indent > 0)
                    {
                        cellSelection.SetIndent(indent);
                    }
                }

                cellSelection = worksheet.Cells[rowStartIndex, columnStartIndex, rowEndIndex, columnEndIndex];

                SetCellProperties(cellInfo, cellSelection);
            }

            for (int i = 0; i < columnCount; i++)
            {
                var columnSelection = worksheet.Columns[i];
                columnSelection.AutoFitWidth();

                //NOTE: workaround for incorrect autofit.
                var newWidth = worksheet.Columns[i].GetWidth().Value.Value + 15;
                columnSelection.SetWidth(new ColumnWidth(newWidth, false));
            }

            workbook.ResumeLayoutUpdate();
            return workbook;
        }
Beispiel #3
0
        private static RadDocument GenerateRadDocument(RadPivotGrid pivot)
        {
            var export      = pivot.GenerateExport();
            int rowCount    = export.RowCount;
            int columnCount = export.ColumnCount;

            RadDocument document = new RadDocument();

            document.SectionDefaultPageMargin = new Padding(10);
            document.LayoutMode = DocumentLayoutMode.Paged;
            document.SectionDefaultPageOrientation          = PageOrientation.Landscape;
            document.Style.SpanProperties.FontFamily        = pivot.FontFamily;
            document.Style.SpanProperties.FontSize          = pivot.FontSize;
            document.Style.ParagraphProperties.SpacingAfter = 0;

            var section = new Section();

            document.Sections.Add(section);
            section.Blocks.Add(new Paragraph());

            var table = new Table(rowCount, columnCount);

            section.Blocks.Add(table);

            var tableRows = table.Rows.ToArray();

            foreach (var cellInfo in export.Cells)
            {
                int rowStartIndex    = cellInfo.Row;
                int rowEndIndex      = rowStartIndex + cellInfo.RowSpan - 1;
                int columnStartIndex = cellInfo.Column;
                int columnEndIndex   = columnStartIndex + cellInfo.ColumnSpan - 1;

                var value = cellInfo.Value;
                var text  = Convert.ToString(value);
                if (!string.IsNullOrWhiteSpace(text))
                {
                    var       cells     = tableRows[rowStartIndex].Cells.ToArray();
                    var       cell      = cells[columnStartIndex];
                    Paragraph paragraph = new Paragraph();
                    cell.Blocks.Add(paragraph);
                    var span = new Span(text);
                    paragraph.Inlines.Add(span);
                    paragraph.TextAlignment = GetTextAlignment(cellInfo.TextAlignment);

                    if (cellInfo.FontWeight.HasValue)
                    {
                        span.FontWeight = cellInfo.FontWeight.Value;
                    }

                    Color foreColor;
                    if (GetColor(cellInfo.Foreground, out foreColor))
                    {
                        span.ForeColor = foreColor;
                    }

                    cell.VerticalAlignment = GetVerticalAlignment(cellInfo.VerticalAlignment);
                    paragraph.LeftIndent   = cellInfo.Indent * 20;
                }

                var borderThickness = cellInfo.BorderThickness;
                var borderBrush     = cellInfo.BorderBrush;
                var background      = cellInfo.Background;

                Color backColor;
                bool  hasBackground = GetColor(cellInfo.Background, out backColor);

                if (cellInfo.RowSpan > 1 && cellInfo.ColumnSpan > 1)
                {
                    for (int k = rowStartIndex; k <= rowEndIndex; k++)
                    {
                        var cells = tableRows[k].Cells.ToArray();
                        for (int j = columnStartIndex; j <= columnEndIndex; j++)
                        {
                            var cell = cells[j];
                            if (hasBackground)
                            {
                                cell.Background = backColor;
                            }

                            cell.Borders = GetCellBorders(borderThickness, borderBrush, cell.Borders, k, rowStartIndex, rowEndIndex, j, columnStartIndex, columnEndIndex, hasBackground);
                        }
                    }
                }
                else if (cellInfo.RowSpan > 1)
                {
                    for (int j = rowStartIndex; j <= rowEndIndex; j++)
                    {
                        // TODO: check when ColumnSpan > 1;
                        var cell = tableRows[j].Cells.ToArray()[columnStartIndex];

                        Position position = j == rowStartIndex ? Position.First : ((j == rowEndIndex) ? Position.Last : Position.Middle);

                        cell.Borders = GetCellBorders(borderThickness, borderBrush, position, cell.Borders, true, cellInfo.Background != null);
                        if (hasBackground)
                        {
                            cell.Background = backColor;
                        }
                    }
                }
                else if (cellInfo.ColumnSpan > 1)
                {
                    var cells = tableRows[rowStartIndex].Cells.ToArray();
                    for (int j = columnStartIndex; j <= columnEndIndex; j++)
                    {
                        // TODO: check when RowSpan > 1;
                        var cell = cells[j];

                        Position position = j == columnStartIndex ? Position.First : ((j == columnEndIndex) ? Position.Last : Position.Middle);
                        if (hasBackground)
                        {
                            cell.Background = backColor;
                        }

                        cell.Borders = GetCellBorders(borderThickness, borderBrush, position, cell.Borders, false, hasBackground);
                    }
                }
            }

            return(document);
        }
Beispiel #4
0
        private static RadDocument GenerateRadDocument(RadPivotGrid pivot)
        {
            var export = pivot.GenerateExport();
            int rowCount = export.RowCount;
            int columnCount = export.ColumnCount;

            RadDocument document = new RadDocument();
            document.SectionDefaultPageMargin = new Padding(10);
            document.LayoutMode = DocumentLayoutMode.Paged;
            document.SectionDefaultPageOrientation = PageOrientation.Landscape;
            document.Style.SpanProperties.FontFamily = pivot.FontFamily;
            document.Style.SpanProperties.FontSize = pivot.FontSize;
            document.Style.ParagraphProperties.SpacingAfter = 0;

            var section = new Section();
            document.Sections.Add(section);
            section.Blocks.Add(new Paragraph());

            var table = new Table(rowCount, columnCount);
            section.Blocks.Add(table);

            var tableRows = table.Rows.ToArray();
            foreach (var cellInfo in export.Cells)
            {
                int rowStartIndex = cellInfo.Row;
                int rowEndIndex = rowStartIndex + cellInfo.RowSpan - 1;
                int columnStartIndex = cellInfo.Column;
                int columnEndIndex = columnStartIndex + cellInfo.ColumnSpan - 1;

                var value = cellInfo.Value;
                var text = Convert.ToString(value);
                if (!string.IsNullOrWhiteSpace(text))
                {
                    var cells = tableRows[rowStartIndex].Cells.ToArray();
                    var cell = cells[columnStartIndex];
                    Paragraph paragraph = new Paragraph();
                    cell.Blocks.Add(paragraph);
                    var span = new Span(text);
                    paragraph.Inlines.Add(span);
                    paragraph.TextAlignment = GetTextAlignment(cellInfo.TextAlignment);

                    if (cellInfo.FontWeight.HasValue)
                    {
                        span.FontWeight = cellInfo.FontWeight.Value;
                    }

                    Color foreColor;
                    if (GetColor(cellInfo.Foreground, out foreColor))
                    {
                        span.ForeColor = foreColor;
                    }

                    cell.VerticalAlignment = GetVerticalAlignment(cellInfo.VerticalAlignment);
                    paragraph.LeftIndent = cellInfo.Indent * 20;
                }

                var borderThickness = cellInfo.BorderThickness;
                var borderBrush = cellInfo.BorderBrush;
                var background = cellInfo.Background;

                Color backColor;
                bool hasBackground = GetColor(cellInfo.Background, out backColor);

                if (cellInfo.RowSpan > 1 && cellInfo.ColumnSpan > 1)
                {
                    for (int k = rowStartIndex; k <= rowEndIndex; k++)
                    {
                        var cells = tableRows[k].Cells.ToArray();
                        for (int j = columnStartIndex; j <= columnEndIndex; j++)
                        {
                            var cell = cells[j];
                            if (hasBackground)
                            {
                                cell.Background = backColor;
                            }

                            cell.Borders = GetCellBorders(borderThickness, borderBrush, cell.Borders, k, rowStartIndex, rowEndIndex, j, columnStartIndex, columnEndIndex, hasBackground);
                        }

                    }
                }
                else if (cellInfo.RowSpan > 1)
                {
                    for (int j = rowStartIndex; j <= rowEndIndex; j++)
                    {
                        // TODO: check when ColumnSpan > 1;
                        var cell = tableRows[j].Cells.ToArray()[columnStartIndex];

                        Position position = j == rowStartIndex ? Position.First : ((j == rowEndIndex) ? Position.Last : Position.Middle);

                        cell.Borders = GetCellBorders(borderThickness, borderBrush, position, cell.Borders, true, cellInfo.Background != null);
                        if (hasBackground)
                        {
                            cell.Background = backColor;
                        }
                    }
                }
                else if (cellInfo.ColumnSpan > 1)
                {
                    var cells = tableRows[rowStartIndex].Cells.ToArray();
                    for (int j = columnStartIndex; j <= columnEndIndex; j++)
                    {
                        // TODO: check when RowSpan > 1;
                        var cell = cells[j];

                        Position position = j == columnStartIndex ? Position.First : ((j == columnEndIndex) ? Position.Last : Position.Middle);
                        if (hasBackground)
                        {
                            cell.Background = backColor;
                        }

                        cell.Borders = GetCellBorders(borderThickness, borderBrush, position, cell.Borders, false, hasBackground);
                    }
                }
            }

            return document;
        }