Beispiel #1
0
        public override byte[] Render(global::Campus.Report.Base.Report report)
        {
            var document = new StringBuilder();

            document.AppendLine("<html>");
            document.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");

            document.AppendLine("<head>");
            document.AppendLine(RenderHtmlHead(report));
            document.AppendLine("</head>");

            document.AppendLine("<body>");
            document.AppendLine(ToHtmlBlock(report));
            document.AppendLine("</body>");

            document.AppendLine("</html>");

            var memoryStream = new MemoryStream();
            var streamWriter = new StreamWriter(memoryStream);
            streamWriter.Write(document.ToString());
            streamWriter.Flush();
            memoryStream.Position = 0;

            return memoryStream.ToArray();
        }
 public abstract byte[] Render(global::Campus.Report.Base.Report report);
Beispiel #3
0
        public override byte[] Render(global::Campus.Report.Base.Report report)
        {
            byte[] buffer;

            using (var outputMemoryStream = new MemoryStream())
            {
                var pdfDocument = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 25, 15, 10);

                var arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arial.ttf");
                FontFactory.Register(arialuniTff);

                //FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));

                var pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);

                pdfWriter.CloseStream = false;
                pdfDocument.Open();

                //pdfDocument.NewPage();
                pdfDocument.Add(new Paragraph(new Phrase(" ")));

                foreach (var x in report)
                {
                    if (x is TextElement)
                    {
                        var element = x as TextElement;

                        if (Render(element, pdfDocument, arialuniTff))
                        {
                            continue;
                        }
                    }

                    if (x is TableElement)
                    {
                        var element = x as TableElement;
                        if (Render(element, pdfDocument, arialuniTff))
                        {
                            continue;
                        }
                    }

                    if (x is ImageElement)
                    {
                        var element = x as ImageElement;

                        if (Render(element, pdfDocument))
                        {
                            continue;
                        }
                    }

                    if (x is ComplexHeaderCell)
                    {
                        var element = x as ComplexHeaderCell;

                        if (Render(element, pdfDocument))
                        {
                            continue;
                        }
                    }

                    if (x is ComplexHeader)
                    {
                        var element = x as ComplexHeader;

                        if (Render(element, pdfDocument))
                        {
                            continue;
                        }
                    }
                }

                pdfDocument.Close();
                pdfDocument.CloseDocument();

                buffer = new byte[outputMemoryStream.Position];
                outputMemoryStream.Position = 0;
                outputMemoryStream.Read(buffer, 0, buffer.Length);
            }

            return(buffer);
        }
Beispiel #4
0
        public override byte[] Render(global::Campus.Report.Base.Report report)
        {
            #warning Need to extend text style support

            var memoryStream = new MemoryStream();

            using (var document = WordprocessingDocument.Create(memoryStream, WordprocessingDocumentType.Document))
            {
                document.AddMainDocumentPart();
                document.MainDocumentPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();

                var stylesPart = document.MainDocumentPart.AddNewPart <StyleDefinitionsPart>();
                var body       = document.MainDocumentPart.Document.AppendChild <DocumentFormat.OpenXml.Wordprocessing.Body>(new DocumentFormat.OpenXml.Wordprocessing.Body());


                var styles = report.Select(o => o.Style).ToList();
                stylesPart.Styles = WordStyles(styles);
                stylesPart.Styles.Save();

                foreach (var x in report)
                {
                    if (x is TextElement)
                    {
                        var element = x as TextElement;

                        if (Render(element, document, body))
                        {
                            continue;
                        }
                    }

                    if (x is TableElement)
                    {
                        var element = x as TableElement;

                        if (Render(element, document, body))
                        {
                            continue;
                        }
                    }

                    if (x is ImageElement)
                    {
                        var element = x as ImageElement;

                        if (Render(element, document, body))
                        {
                            continue;
                        }
                    }

                    if (x is ComplexHeaderCell)
                    {
                        var element = x as ComplexHeaderCell;

                        if (Render(element, document, body))
                        {
                            continue;
                        }
                    }

                    if (x is ComplexHeader)
                    {
                        var element = x as ComplexHeader;

                        if (Render(element, document, body))
                        {
                            continue;
                        }
                    }

                    document.MainDocumentPart.Document.Save();
                }
            }

            return(memoryStream.ToArray());
        }
        public override byte[] Render(global::Campus.Report.Base.Report report)
        {
            byte[] buffer;
            using (var outputMemoryStream = new MemoryStream())
            {
                using (var document = SpreadsheetDocument.Create(outputMemoryStream, SpreadsheetDocumentType.Workbook, true))
                {
                    // Add a WorkbookPart to the document.
                    var workbookpart = document.AddWorkbookPart();
                    workbookpart.Workbook = new Workbook();

                    // Add a WorksheetPart to the WorkbookPart.
                    var worksheetPart = workbookpart.AddNewPart <WorksheetPart>();
                    worksheetPart.Worksheet = new Worksheet(new SheetData());

                    // Add Sheets to the Workbook.
                    var sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets());

                    // Append a new worksheet and associate it with the workbook.
                    var sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                    {
                        Id      = document.WorkbookPart.GetIdOfPart(worksheetPart),
                        SheetId = 1,
                        Name    = "Sheet1"
                    };

                    sheets.Append(sheet);

                    // Add a WorkbookStylesPart to the WorkbookPart
                    var stylesPart = document.WorkbookPart.AddNewPart <WorkbookStylesPart>();

                    var shData = worksheetPart.Worksheet.GetFirstChild <SheetData>();



                    stylesPart.Stylesheet = InitializeDefaultStyles(stylesPart.Stylesheet);

                    uint styleId = 2; //обязательно 2 потому что уже додано два стиля по умолчанию с индексами 0 и 1;

                    var styles = report.Select(o => o.Style).ToList();

                    //нельзя перенести в отдельный метод :(
                    foreach (var s in styles)
                    {
                        if (s is TableStyle)
                        {
                            var style = s as TableStyle;

                            var border = new DocumentFormat.OpenXml.Spreadsheet.Border();

                            var leftBorder   = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder();
                            var rightBorder  = new DocumentFormat.OpenXml.Spreadsheet.RightBorder();
                            var topBorder    = new DocumentFormat.OpenXml.Spreadsheet.TopBorder();
                            var bottomBorder = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder();

                            leftBorder.Style   = SetLineType(style.BorderLine);
                            rightBorder.Style  = SetLineType(style.BorderLine);
                            topBorder.Style    = SetLineType(style.BorderLine);
                            bottomBorder.Style = SetLineType(style.BorderLine);

                            leftBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color {
                                Rgb = ToHexBinaryValue(style.BorderColor)
                            };
                            rightBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color {
                                Rgb = ToHexBinaryValue(style.BorderColor)
                            };
                            topBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color {
                                Rgb = ToHexBinaryValue(style.BorderColor)
                            };
                            bottomBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color {
                                Rgb = ToHexBinaryValue(style.BorderColor)
                            };

                            border.LeftBorder   = leftBorder;
                            border.RightBorder  = rightBorder;
                            border.TopBorder    = topBorder;
                            border.BottomBorder = bottomBorder;

                            stylesPart.Stylesheet.Borders.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Border>(border);



                            var patternFill = new DocumentFormat.OpenXml.Spreadsheet.PatternFill
                            {
                                BackgroundColor = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor {
                                    Rgb = ToHexBinaryValue(style.Background)
                                },
                                ForegroundColor = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor {
                                    Rgb = ToHexBinaryValue(style.Foreground)
                                },
                                PatternType = SetPatternType(style.PatternType)
                            };

                            var fill = new DocumentFormat.OpenXml.Spreadsheet.Fill {
                                PatternFill = patternFill
                            };

                            stylesPart.Stylesheet.Fills.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Fill>(fill);

                            DocumentFormat.OpenXml.Spreadsheet.Font font = new DocumentFormat.OpenXml.Spreadsheet.Font()
                            {
                                FontName = new DocumentFormat.OpenXml.Spreadsheet.FontName {
                                    Val = new StringValue {
                                        Value = s.FontName
                                    }
                                },
                                FontSize = new DocumentFormat.OpenXml.Spreadsheet.FontSize {
                                    Val = s.FontSize
                                },
                                Color = new DocumentFormat.OpenXml.Spreadsheet.Color {
                                    Rgb = ToHexBinaryValue(s.FontColor)
                                }
                            };
                            stylesPart.Stylesheet.Fonts.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Font>(font);
                        }
                        else
                        {
                            stylesPart.Stylesheet.Fills.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Fill>(new DocumentFormat.OpenXml.Spreadsheet.Fill());
                            stylesPart.Stylesheet.Borders.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Border>(new DocumentFormat.OpenXml.Spreadsheet.Border());
                            DocumentFormat.OpenXml.Spreadsheet.Font font;
                            if (s != null)
                            {
                                font = new DocumentFormat.OpenXml.Spreadsheet.Font
                                {
                                    FontName = new DocumentFormat.OpenXml.Spreadsheet.FontName {
                                        Val = new StringValue {
                                            Value = s.FontName
                                        }
                                    },
                                    FontSize = new DocumentFormat.OpenXml.Spreadsheet.FontSize {
                                        Val = s.FontSize
                                    },
                                    Color = new DocumentFormat.OpenXml.Spreadsheet.Color {
                                        Rgb = ToHexBinaryValue(s.FontColor)
                                    }
                                };


                                foreach (var item in s.TextStyle)
                                {
                                    switch (item)
                                    {
                                    case TextStyleType.Bold: font.Bold = new DocumentFormat.OpenXml.Spreadsheet.Bold {
                                            Val = true
                                    }; break;

                                    case TextStyleType.Italic: font.Italic = new DocumentFormat.OpenXml.Spreadsheet.Italic {
                                            Val = true
                                    }; break;

                                    case TextStyleType.Underline: font.Underline = new DocumentFormat.OpenXml.Spreadsheet.Underline {
                                            Val = DocumentFormat.OpenXml.Spreadsheet.UnderlineValues.Single
                                    }; break;

                                    case TextStyleType.Normal: break;
                                    }
                                }
                            }
                            else
                            {
                                font = new DocumentFormat.OpenXml.Spreadsheet.Font();
                            }

                            stylesPart.Stylesheet.Fonts.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Font>(font);
                        }


                        if (s != null)
                        {
                            var cellFormat = new DocumentFormat.OpenXml.Spreadsheet.CellFormat
                            {
                                BorderId = UInt32Value.ToUInt32(styleId),
                                FillId   = UInt32Value.ToUInt32(styleId),
                                FontId   = UInt32Value.ToUInt32(styleId)
                            };

                            if (s.Aligment != null)
                            {
                                var align = new Alignment()
                                {
                                    TextRotation = new UInt32Value(s.Aligment.Rotation),
                                    Horizontal   = AlignmentMapper.MapHorizontalAligment(s.Aligment.HorizontalAligment),
                                    Vertical     = AlignmentMapper.MapVerticalAligment(s.Aligment.VerticalAligment)
                                };

                                cellFormat.Append(align);
                            }

                            stylesPart.Stylesheet.CellFormats.AppendChild <DocumentFormat.OpenXml.Spreadsheet.CellFormat>(cellFormat);
                        }

                        styleId = styleId + 1;
                    }

                    foreach (var x in report)
                    {
                        if (x is ComplexHeaderCell)
                        {
                            var element = x as ComplexHeaderCell;

                            if (Render(element, styles, shData, worksheetPart, workbookpart))
                            {
                                continue;
                            }
                        }

                        if (x is TextElement)
                        {
                            var element = x as TextElement;

                            if (Render(element, styles, shData, workbookpart))
                            {
                                continue;
                            }
                        }

                        if (x is TableElement)
                        {
                            var element = x as TableElement;
                            if (Render(element, styles, shData, workbookpart))
                            {
                                continue;
                            }
                        }

                        if (x is ImageElement)
                        {
                            var element = x as ImageElement;

                            if (Render(element, styles, shData, workbookpart))
                            {
                                continue;
                            }
                        }
                    }

                    //var dateLine = shData.AppendChild(new Spreadsheet.Row());

                    //dateLine.AppendChild(new Spreadsheet.Cell()
                    //{
                    //    CellValue = new Spreadsheet.CellValue(_report.TimeStamp.Date.ToString()),
                    //    DataType = Spreadsheet.CellValues.String,
                    //    StyleIndex = 0
                    //});

                    workbookpart.Workbook.Save();
                }

                buffer = new byte[outputMemoryStream.Position];
                outputMemoryStream.Position = 0;
                outputMemoryStream.Read(buffer, 0, buffer.Length);
            }
            return(buffer);
        }
Beispiel #6
0
        public virtual string ToHtmlBlock(global::Campus.Report.Base.Report report)
        {
            var body = new StringBuilder();

            body.AppendFormat("<section class=\"report\">");

            foreach (var x in report)
            {
                if (x is TextElement)
                {
                    var element = x as TextElement;

                    if (element.Value == InternalContants.NewLine)
                    {
                        body.AppendFormat("<br />");
                        continue;
                    }

                    if (element.Value == InternalContants.NewSection)
                    {
                        body.AppendFormat("<p />");
                        continue;
                    }

                    if (element.Style.DocumentTitle != DocumentTitle.None)
                    {
                        body.AppendFormat("<h{2} class=\"{0}\">{1}</h{2}>", element.Style.Name, element.Value, ((int)element.Style.DocumentTitle));
                        continue;
                    }

                    body.AppendFormat("<span class=\"{0}\">{1}</span>", element.Style.Name, element.Value);
                }

                if (x is TableElement)
                {
                    var element = x as TableElement;
                    body.AppendLine(RenderTableToHtml(element));
                }

                if (x is ImageElement)
                {
                    var element = x as ImageElement;
                    body.AppendLine(RenderImage(element));
                }

                if (x is ComplexHeaderCell)
                {
                    var element = x as ComplexHeaderCell;
                    body.AppendLine("<div style=\"color: red; font-weight: bold;\">Rended for ComplexHeaderCell not implemented.</div>");
                
                }

                if (x is ComplexHeader)
                {
                    var element = x as ComplexHeader;
                    body.AppendLine("<div style=\"color: red; font-weight: bold;\">Rended for ComplexHeader not implemented.</div>");
                }
            }

            body.AppendFormat("<span>{0}</span>", report.TimeStamp);
            body.AppendFormat("</section>");

            return body.ToString();
        }