コード例 #1
0
        public void TestThemesTableColors()
        {
            // Load our two test workbooks
            XSSFWorkbook simple  = XSSFTestDataSamples.OpenSampleWorkbook(testFileSimple);
            XSSFWorkbook complex = XSSFTestDataSamples.OpenSampleWorkbook(testFileComplex);
            // Save and re-load them, to check for stability across that
            XSSFWorkbook simpleRS  = XSSFTestDataSamples.WriteOutAndReadBack(simple) as XSSFWorkbook;
            XSSFWorkbook complexRS = XSSFTestDataSamples.WriteOutAndReadBack(complex) as XSSFWorkbook;

            // Fetch fresh copies to test with
            simple  = XSSFTestDataSamples.OpenSampleWorkbook(testFileSimple);
            complex = XSSFTestDataSamples.OpenSampleWorkbook(testFileComplex);
            // Files and descriptions
            Dictionary <String, XSSFWorkbook> workbooks = new Dictionary <String, XSSFWorkbook>();

            workbooks.Add(testFileSimple, simple);
            workbooks.Add("Re-Saved_" + testFileSimple, simpleRS);
            workbooks.Add(testFileComplex, complex);
            workbooks.Add("Re-Saved_" + testFileComplex, complexRS);

            // Sanity check
            //Assert.AreEqual(rgbExpected.Length, rgbExpected.Length);

            // For offline testing
            bool createFiles = false;

            // Check each workbook in turn, and verify that the colours
            //  for the theme-applied cells in Column A are correct
            foreach (String whatWorkbook in workbooks.Keys)
            {
                XSSFWorkbook workbook = workbooks[whatWorkbook];
                XSSFSheet    sheet    = workbook.GetSheetAt(0) as XSSFSheet;
                int          startRN  = 0;
                if (whatWorkbook.EndsWith(testFileComplex))
                {
                    startRN++;
                }

                for (int rn = startRN; rn < rgbExpected.Length + startRN; rn++)
                {
                    XSSFRow row = sheet.GetRow(rn) as XSSFRow;
                    Assert.IsNotNull(row, "Missing row " + rn + " in " + whatWorkbook);
                    String   ref1 = (new CellReference(rn, 0)).FormatAsString();
                    XSSFCell cell = row.GetCell(0) as XSSFCell;
                    Assert.IsNotNull(cell,
                                     "Missing cell " + ref1 + " in " + whatWorkbook);

                    int          expectedThemeIdx = rn - startRN;
                    ThemeElement themeElem        = ThemeElement.ById(expectedThemeIdx);
                    Assert.AreEqual(themeElem.name.ToLower(), cell.StringCellValue,
                                    "Wrong theme at " + ref1 + " in " + whatWorkbook);

                    // Fonts are theme-based in their colours
                    XSSFFont font    = (cell.CellStyle as XSSFCellStyle).GetFont();
                    CT_Color ctColor = font.GetCTFont().GetColorArray(0);
                    Assert.IsNotNull(ctColor);
                    Assert.AreEqual(true, ctColor.IsSetTheme());
                    Assert.AreEqual(themeElem.idx, ctColor.theme);

                    // Get the colour, via the theme
                    XSSFColor color = font.GetXSSFColor();

                    // Theme colours aren't tinted
                    Assert.AreEqual(color.HasTint, false);
                    // Check the RGB part (no tint)
                    Assert.AreEqual(rgbExpected[expectedThemeIdx], HexDump.EncodeHexString(color.RGB),
                                    "Wrong theme colour " + themeElem.name + " on " + whatWorkbook);

                    long themeIdx = font.GetCTFont().GetColorArray(0).theme;
                    Assert.AreEqual(expectedThemeIdx, themeIdx,
                                    "Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook
                                    );

                    if (createFiles)
                    {
                        XSSFCellStyle cs = row.Sheet.Workbook.CreateCellStyle() as XSSFCellStyle;
                        cs.SetFillForegroundColor(color);
                        cs.FillPattern = FillPatternType.SolidForeground;
                        row.CreateCell(1).CellStyle = (cs);
                    }
                }

                if (createFiles)
                {
                    FileStream fos = new FileStream("Generated_" + whatWorkbook, FileMode.Create, FileAccess.ReadWrite);
                    workbook.Write(fos);
                    fos.Close();
                }
            }
        }
コード例 #2
0
        public void ThemedAndNonThemedColours()
        {
            XSSFWorkbook  wb    = XSSFTestDataSamples.OpenSampleWorkbook(testFileComplex);
            XSSFSheet     sheet = wb.GetSheetAt(0) as XSSFSheet;
            XSSFCellStyle style;
            XSSFColor     color;
            XSSFCell      cell;

            String[] names          = { "White", "Black", "Grey", "Dark Blue", "Blue", "Red", "Green" };
            String[] explicitFHexes = { "FFFFFFFF", "FF000000", "FFC0C0C0", "FF002060",
                                        "FF0070C0", "FFFF0000", "FF00B050" };
            String[] explicitBHexes = { "FFFFFFFF", "FF000000", "FFC0C0C0", "FF002060",
                                        "FF0000FF", "FFFF0000", "FF00FF00" };
            Assert.AreEqual(7, names.Length);

            // Check the non-CF colours in Columns A, B, C and E
            for (int rn = 1; rn < 8; rn++)
            {
                int     idx = rn - 1;
                XSSFRow row = sheet.GetRow(rn) as XSSFRow;
                Assert.IsNotNull(row, "Missing row " + rn);

                // Theme cells come first
                XSSFCell     themeCell = row.GetCell(0) as XSSFCell;
                ThemeElement themeElem = ThemeElement.ById(idx);
                assertCellContents(themeElem.name, themeCell);
                // Sanity check names
                assertCellContents(names[idx], row.GetCell(1));
                assertCellContents(names[idx], row.GetCell(2));
                assertCellContents(names[idx], row.GetCell(4));

                // Check the colours

                //  A: Theme Based, Foreground
                style = themeCell.CellStyle as XSSFCellStyle;
                color = style.GetFont().GetXSSFColor();
                Assert.AreEqual(true, color.IsThemed);
                Assert.AreEqual(idx, color.Theme);
                Assert.AreEqual(rgbExpected[idx], HexDump.EncodeHexString(color.RGB));
                //  B: Theme Based, Foreground
                cell  = row.GetCell(1) as XSSFCell;
                style = cell.CellStyle as XSSFCellStyle;
                color = style.GetFont().GetXSSFColor();
                Assert.AreEqual(true, color.IsThemed);
                // TODO Fix the grey theme color in Column B
                if (idx != 2)
                {
                    Assert.AreEqual(true, color.IsThemed);
                    Assert.AreEqual(idx, color.Theme);
                    Assert.AreEqual(rgbExpected[idx], HexDump.EncodeHexString(color.RGB));
                }
                else
                {
                    Assert.AreEqual(1, color.Theme);
                    Assert.AreEqual(0.50, color.Tint, 0.001);
                }

                //  C: Explicit, Foreground
                cell  = row.GetCell(2) as XSSFCell;
                style = cell.CellStyle as XSSFCellStyle;
                color = style.GetFont().GetXSSFColor();
                Assert.AreEqual(false, color.IsThemed);
                Assert.AreEqual(explicitFHexes[idx], color.ARGBHex);

                // E: Explicit Background, Foreground all Black
                cell  = row.GetCell(4) as XSSFCell;
                style = cell.CellStyle as XSSFCellStyle;

                color = style.GetFont().GetXSSFColor();
                Assert.AreEqual(true, color.IsThemed);
                Assert.AreEqual("FF000000", color.ARGBHex);

                color = style.FillForegroundXSSFColor;
                Assert.AreEqual(false, color.IsThemed);
                Assert.AreEqual(explicitBHexes[idx], color.ARGBHex);
                color = style.FillBackgroundColorColor as XSSFColor;
                Assert.AreEqual(false, color.IsThemed);
                Assert.AreEqual(null, color.ARGBHex);
            }

            // Check the CF colours
            // TODO
        }