Example #1
0
 private static void WriteToCSV(DataCell[][] cells, string filename)
 {
     using (var stream = new FileStream(filename, FileMode.Create))
     using (var writer = new StreamWriter(stream))
     {
         foreach (var row in cells)
         {
             writer.WriteLine(row.Select(x => x.Content).Join(";"));
         }
     }
 }
Example #2
0
        private static void WriteToPDF(DataCell[][] dataCells, string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            // Create a new MigraDoc document

            var document = new Document();
            document.Styles["Normal"].Font.Name = "Arial";
            document.DefaultPageSetup.LeftMargin -= Unit.FromCentimeter(1);
            var section = document.AddSection();

            var rightStyle = document.AddStyle("RightAligned", "Normal");
            rightStyle.ParagraphFormat.Alignment = ParagraphAlignment.Right;

            var headingStyle = document.AddStyle("Heading", "Normal");
            headingStyle.Font.Size = Unit.FromPoint(11);

            var headingBigStyle = document.AddStyle("HeadingBig", "Normal");
            headingBigStyle.Font.Size = Unit.FromPoint(16);

            var table = section.AddTable();
            table.Style = "Table";
            table.Rows.LeftIndent = 0;

            var columnCount = dataCells.Max(x => x.Length);

            for (var i = 0; i < columnCount; i++)
            {
                var column = table.AddColumn((18.0/columnCount) + "cm");
                column.Format.Alignment = ParagraphAlignment.Left;
            }

            foreach (var row in dataCells)
            {
                var pdfRow = table.AddRow();

                int counter = 0;
                foreach (var dataCell in row)
                {
                    var pdfCell = pdfRow.Cells[counter];

                    pdfCell.AddParagraph(dataCell.Content);

                    if (dataCell.Outline.HasFlag(DataCellOutline.Top))
                    {
                        pdfCell.Borders.Top.Visible = true;
                        pdfCell.Borders.Top.Style = BorderStyle.Single;
                        pdfCell.Borders.Top.Width = Unit.FromMillimeter(0.5);
                        pdfCell.Borders.Top.Color = new Color(0, 0, 0);
                    }

                    if (dataCell.Outline.HasFlag(DataCellOutline.Bottom))
                    {
                        pdfCell.Borders.Bottom.Visible = true;
                        pdfCell.Borders.Bottom.Style = BorderStyle.Single;
                        pdfCell.Borders.Bottom.Width = Unit.FromMillimeter(0.5);
                        pdfCell.Borders.Bottom.Color = new Color(0, 0, 0);
                    }

                    if (dataCell.Outline.HasFlag(DataCellOutline.Left))
                    {
                        pdfCell.Borders.Left.Visible = true;
                        pdfCell.Borders.Left.Style = BorderStyle.Single;
                        pdfCell.Borders.Left.Width = Unit.FromMillimeter(0.5);
                        pdfCell.Borders.Left.Color = new Color(0, 0, 0);
                    }

                    if (dataCell.Outline.HasFlag(DataCellOutline.Right))
                    {
                        pdfCell.Borders.Right.Visible = true;
                        pdfCell.Borders.Right.Style = BorderStyle.Single;
                        pdfCell.Borders.Right.Width = Unit.FromMillimeter(0.5);
                        pdfCell.Borders.Right.Color = new Color(0, 0, 0);
                    }

                    switch (dataCell.Type)
                    {
                        case DataCellType.HeadingBig:
                            pdfCell.Style = "HeadingBig";
                            pdfCell.MergeRight = columnCount - 1;
                            break;
                        case DataCellType.Heading:
                            pdfCell.Style = "Heading";
                            break;
                        case DataCellType.Number:
                            pdfCell.Style = "RightAligned";
                            break;
                        case DataCellType.ResultGood:
                            PDFDataSerializer.SetCellResultStyle(pdfCell, new Color(0xBC, 0xED, 0x91));
                            break;
                        case DataCellType.ResultNeutral:
                            PDFDataSerializer.SetCellResultStyle(pdfCell, new Color(0xFF, 0xEC, 0xB3));
                            break;
                        case DataCellType.ResultBad:
                            PDFDataSerializer.SetCellResultStyle(pdfCell, new Color(0xFF, 0x8A, 0x65));
                            break;
                    }

                    counter++;
                }
            }

            var pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Automatic) {Document = document};
            pdfRenderer.RenderDocument();
            pdfRenderer.PdfDocument.Save(filename);
        }
        private static void WriteToXLSX(DataCell[][] cells, string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            var package = new ExcelPackage(new FileInfo(filename));
            var sheet = package.Workbook.Worksheets.Add("Content");

            foreach (var cell in new ExcelCellEnumerator(cells.GetLength(0), cells.Max(x => x.Length)))
            {
                var dataRow = cells[cell.Row - 1];
                if (cell.Column >= dataRow.Length) continue;
                var data = dataRow[cell.Column];
                var excelCell = sheet.Cells[cell.Value];
                excelCell.Value = data.Content;
                switch (data.Type)
                {
                    case DataCellType.HeadingBig:
                        excelCell.Style.Font.Bold = true;
                        excelCell.Style.Font.Size = 18;
                        sheet.Row(cell.Row).Merged = true;
                        break;
                    case DataCellType.Heading:
                        excelCell.Style.Font.Bold = true;
                        excelCell.Style.Font.Size = 12;
                        break;
                    case DataCellType.ResultGood:
                        XLSXDataSerializer.SetResultCell(excelCell, Color.FromArgb(0xBC, 0xED, 0x91));
                        break;
                    case DataCellType.ResultNeutral:
                        XLSXDataSerializer.SetResultCell(excelCell, Color.FromArgb(0xFF, 0xEC, 0xB3));
                        break;
                    case DataCellType.ResultBad:
                        XLSXDataSerializer.SetResultCell(excelCell, Color.FromArgb(0xFF, 0x8A, 0x65));
                        break;
                    case DataCellType.Number:
                        excelCell.Value = double.Parse(excelCell.Value.ToString().Replace(",", "."),
                            CultureInfo.InvariantCulture);
                        break;
                }

                if (data.Outline.HasFlag(DataCellOutline.Top))
                {
                    excelCell.Style.Border.Top.Style = ExcelBorderStyle.Medium;
                    excelCell.Style.Border.Top.Color.SetColor(Color.Black);
                }

                if (data.Outline.HasFlag(DataCellOutline.Bottom))
                {
                    excelCell.Style.Border.Bottom.Style = ExcelBorderStyle.Medium;
                    excelCell.Style.Border.Bottom.Color.SetColor(Color.Black);
                }

                if (data.Outline.HasFlag(DataCellOutline.Left))
                {
                    excelCell.Style.Border.Left.Style = ExcelBorderStyle.Medium;
                    excelCell.Style.Border.Left.Color.SetColor(Color.Black);
                }

                if (data.Outline.HasFlag(DataCellOutline.Right))
                {
                    excelCell.Style.Border.Right.Style = ExcelBorderStyle.Medium;
                    excelCell.Style.Border.Right.Color.SetColor(Color.Black);
                }
            }

            for (var i = 1; i < cells.Max(x => x.Length); i++)
            {
                sheet.Cells[2, i, sheet.Dimension.Rows, i].AutoFitColumns(10.71);
                    // 10.71 represents the default excel cell width, we do not want to go below that
            }

            package.Save();
            package.Dispose();
        }