Beispiel #1
0
        private static void SetCellProperties(PivotExportCellInfo cellInfo, CellSelection cellSelection)
        {
            var fill = GenerateFill(cellInfo.Background);

            if (fill != null)
            {
                cellSelection.SetFill(fill);
            }

            SolidColorBrush solidBrush = cellInfo.Foreground as SolidColorBrush;

            if (solidBrush != null)
            {
                cellSelection.SetForeColor(new ThemableColor(solidBrush.Color));
            }

            if (cellInfo.FontWeight.HasValue && cellInfo.FontWeight.Value != FontWeights.Normal)
            {
                cellSelection.SetIsBold(true);
            }

            SolidColorBrush solidBorderBrush = cellInfo.BorderBrush as SolidColorBrush;

            if (solidBorderBrush != null && cellInfo.BorderThickness.HasValue)
            {
                var borderThickness = cellInfo.BorderThickness.Value;
                var color           = new ThemableColor(solidBorderBrush.Color);
                //var leftBorder = new CellBorder(GetBorderStyle(borderThickness.Left), color);
                //var topBorder = new CellBorder(GetBorderStyle(borderThickness.Top), color);
                var rightBorder  = new CellBorder(GetBorderStyle(borderThickness.Right), color);
                var bottomBorder = new CellBorder(GetBorderStyle(borderThickness.Bottom), color);
                var insideBorder = cellInfo.Background != null ? new CellBorder(CellBorderStyle.None, color) : null;
                cellSelection.SetBorders(new CellBorders(null, null, rightBorder, bottomBorder, insideBorder, insideBorder, null, null));
            }
        }
Beispiel #2
0
        private static void SetCellProperties(PivotExportCellInfo cellInfo, CellSelection cellSelection)
        {
            var fill = GenerateFill(cellInfo.Background);
            if (fill != null)
            {
                cellSelection.SetFill(fill);
            }

            SolidColorBrush solidBrush = cellInfo.Foreground as SolidColorBrush;
            if (solidBrush != null)
            {
                cellSelection.SetForeColor(new ThemableColor(solidBrush.Color));
            }

            if (cellInfo.FontWeight.HasValue && cellInfo.FontWeight.Value != FontWeights.Normal)
            {
                cellSelection.SetIsBold(true);
            }

            SolidColorBrush solidBorderBrush = cellInfo.BorderBrush as SolidColorBrush;
            if (solidBorderBrush != null && cellInfo.BorderThickness.HasValue)
            {
                var borderThickness = cellInfo.BorderThickness.Value;
                var color = new ThemableColor(solidBorderBrush.Color);
                //var leftBorder = new CellBorder(GetBorderStyle(borderThickness.Left), color);
                //var topBorder = new CellBorder(GetBorderStyle(borderThickness.Top), color);
                var rightBorder = new CellBorder(GetBorderStyle(borderThickness.Right), color);
                var bottomBorder = new CellBorder(GetBorderStyle(borderThickness.Bottom), color);
                var insideBorder = cellInfo.Background != null ? new CellBorder(CellBorderStyle.None, color) : null;
                cellSelection.SetBorders(new CellBorders(null, null, rightBorder, bottomBorder, insideBorder, insideBorder, null, null));
            }
        }
Beispiel #3
0
        public async Task <byte[]> ExportPdf <T>(IQueryable <T> data, DataSourceRequest gridRequest)
        {
            var workbook = new Workbook();

            workbook.Name = "workbook-1";

            // performance
            workbook.History.IsEnabled = false;
            workbook.SuspendLayoutUpdate();

            workbook.Sheets.Add(SheetType.Worksheet);
            Worksheet worksheet = workbook.ActiveWorksheet;

            var dataResult = await data.ToDataSourceResultAsync(gridRequest);

            List <T> dataToExport = (dataResult.Data as IEnumerable <T>).ToList();

            int currRow = 0;

            Type typeParameterType = typeof(T);
            var  fieldsList        = typeParameterType.GetProperties();

            //some styling for the cells - borders in this example
            ThemableColor black          = new ThemableColor(Telerik.Documents.Media.Color.FromArgb(0, 0, 0, 0));
            CellBorders   desiredBorders = new CellBorders(
                new CellBorder(CellBorderStyle.Medium, black),     // Left border
                new CellBorder(CellBorderStyle.Medium, black),     // Top border
                new CellBorder(CellBorderStyle.Medium, black),     // Right border
                new CellBorder(CellBorderStyle.Medium, black),     // Bottom border
                new CellBorder(CellBorderStyle.Thin, black),       // Inside horizontal border
                new CellBorder(CellBorderStyle.Thin, black),       // Inside vertical border
                new CellBorder(CellBorderStyle.None, black),       // Diagonal up border
                new CellBorder(CellBorderStyle.None, black)        // Diagonal down border
                );

            //column sizes
            for (int i = 0; i < fieldsList.Length; i++)
            {
                worksheet.Columns[i].SetWidth(new ColumnWidth(120, true));
            }

            //automatic resizing is convenient, but comes with some performance hit
            //it would also have to be moved after setting the content
            //for (int i = 0; i < fieldsList.Length; i++)
            //{
            //    worksheet.Columns[i].AutoFitWidth();
            //}

            for (int i = 0; i < fieldsList.Length; i++)
            {
                CellSelection currCell = worksheet.Cells[0, i];
                currCell.SetValue(fieldsList[i].Name);
                currCell.SetBorders(desiredBorders);
            }

            currRow++;


            // content
            for (int i = 0; i < dataToExport.Count; i++)
            {
                for (int columnIndex = 0; columnIndex < fieldsList.Length; columnIndex++)
                {
                    var           cellValue = GetFieldValue(dataToExport[i], fieldsList[columnIndex].Name);
                    CellSelection currCell  = worksheet.Cells[currRow, columnIndex];
                    SetCellValue(currCell, cellValue);
                    currCell.SetBorders(desiredBorders);
                }
                currRow++;
            }

            // performance
            workbook.ResumeLayoutUpdate();

            //convert the excel workbook to a pdf
            var pdfFormatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();

            byte[] fileBytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                pdfFormatProvider.Export(workbook, ms);
                fileBytes = ms.ToArray();
            }

            return(await Task.FromResult(fileBytes));
        }