Пример #1
0
        public static string WriteDebugFile(XSheet.Renderers.NPOI.Formats.IFormatApplier formatApplier, Matrix mat, string filename, IWorkbook wb = null, List <Stream> streamsToClose = null)
        {
            if (wb is null)
            {
                wb = new XSSFWorkbook();
            }
            var rd             = new NPOIRenderer(wb, formatApplier);
            var targetFilename = TestUtils.GetFileName(filename);
            var fs             = File.Create(targetFilename);

            if (streamsToClose != null)
            {
                streamsToClose.Add(fs);
            }

            rd.GenerateExcelFile(mat, fs);
            return(targetFilename);
        }
Пример #2
0
        public void Should_Renderer_Matrix_With_Basic_Format()
        {
            // GIVEN
            const string Lastname  = "Lastname";
            const string Firstname = "Firstname";
            const string Age       = "Age";
            var          cols      = new List <ColumnDefinition>
            {
                new ColumnDefinition {
                    Label = Lastname, DataType = DataTypes.Text
                },
                new ColumnDefinition {
                    Label = Firstname, DataType = DataTypes.Text, HeaderCellFormat = new BasicFormat {
                        FontStyle = FontStyle.Italic
                    }
                },
                new ColumnDefinition {
                    Label = Age, DataType = DataTypes.Number
                },
            };

            const string Even = "EVEN";
            const string Odd  = "ODD";

            var ColorBlueIndex      = IndexedColors.LightBlue.Index;
            var ColorLightGreyIndex = IndexedColors.Grey25Percent.Index;
            var rows = new List <RowDefinition>
            {
                new RowDefinition {
                    DefaultCellFormat = new BasicFormat {
                        FontSize = 12
                    },
                    FormatsByColName = new Dictionary <string, IFormat> {
                        { Lastname, new BasicFormat {
                              FontStyle = FontStyle.Bold
                          } },
                        { Age, new BasicFormat {
                              BackgroundColor = ColorLightGreyIndex.ToString()
                          } }
                    }
                },
                new RowDefinition {
                    Key = Odd,
                    FormatsByColName = new Dictionary <string, IFormat> {
                        { Age, new BasicFormat {
                              BackgroundColor = ColorBlueIndex.ToString()
                          } }
                    }
                },
            };

            var values = new List <RowValue> {
                new RowValue {
                    Key             = Even,
                    ValuesByColName = new Dictionary <string, object> {
                        { Lastname, "Doe" },
                        { Firstname, "John" },
                        { Age, 30 }
                    }
                },
                new RowValue {
                    Key             = Odd,
                    ValuesByColName = new Dictionary <string, object> {
                        { Lastname, "Clinton" },
                        { Firstname, "Bob" },
                        { Age, 41 }
                    }
                },
                new RowValue {
                    Key             = Even,
                    ValuesByColName = new Dictionary <string, object> {
                        { Lastname, "Doa" },
                        { Firstname, "Johana" },
                        { Age, 36 }
                    }
                }
            };

            var mat = Matrix.With()
                      .Cols(cols)
                      .Rows(rows)
                      .RowValues(values)
                      .Build();

            var ms = new MemoryStream();

            var formatApplier = new BasicFormatApplier();
            var renderer      = new NPOIRenderer(_workbook, formatApplier);

            // WHEN
            renderer.GenerateExcelFile(mat, ms);
            ms.Close();

            WriteDebugFile(formatApplier, mat, FILE_TEST_FORMAT_BASIC);

            // THEN
            var fileBytes = ms.ToArray();

            Check.That(fileBytes).Not.IsEmpty();

            var readWb    = new XSSFWorkbook(new MemoryStream(fileBytes));
            var readSheet = readWb.GetSheetAt(0);

            // Headers
            var headerRow = readSheet.GetRow(0);

            // -- Firstname cell is Italic
            Check.That(headerRow.Cells[1].CellStyle.GetFont(readWb).IsItalic).IsTrue();

            // Row 1
            var rowValue1     = readSheet.GetRow(1);
            var lastnameFont1 = rowValue1.Cells[0].CellStyle.GetFont(readWb);

            // -- Lastname is Bold
            Check.That(lastnameFont1.IsBold).IsTrue();
            // -- Age has BgColor=LightGrey
            Check.That(rowValue1.Cells[2].CellStyle.FillForegroundColor).IsEqualTo(ColorLightGreyIndex);

            // Row 2
            var rowValue2 = readSheet.GetRow(2);

            // -- Age has BgColor=Blue
            Check.That(rowValue2.Cells[2].CellStyle.FillForegroundColor).IsEqualTo(ColorBlueIndex);
            // -- Lastname is Bold
            Check.That(rowValue2.Cells[0].CellStyle.GetFont(readWb).IsBold).IsTrue();

            // Row 3
            var rowValue3 = readSheet.GetRow(3);

            // -- Lastname is Bold
            Check.That(rowValue3.Cells[0].CellStyle.GetFont(readWb).IsBold).IsTrue();
            // -- Age has BgColor=LightGrey
            Check.That(rowValue3.Cells[2].CellStyle.FillForegroundColor).IsEqualTo(ColorLightGreyIndex);

            // All cells (skip headers)
            // -- FontSize is 12
            Check.That(TestUtils.ReadAllCells(readWb, 0).Skip(cols.Count)).ContainsOnlyElementsThatMatch(cell => cell.CellStyle.GetFont(readWb).FontHeightInPoints == 12);
        }