Exemple #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();
                }
            }
        }
Exemple #2
0
        /**
         * Copy font attributes from CTFont bean into CTRPrElt bean
         */
        private void SetRunAttributes(CT_Font ctFont, CT_RPrElt pr)
        {
            if (ctFont.SizeOfBArray() > 0)
            {
                pr.AddNewB().val = (ctFont.GetBArray(0).val);
            }
            if (ctFont.sizeOfUArray() > 0)
            {
                pr.AddNewU().val = (ctFont.GetUArray(0).val);
            }
            if (ctFont.sizeOfIArray() > 0)
            {
                pr.AddNewI().val = (ctFont.GetIArray(0).val);
            }
            if (ctFont.sizeOfColorArray() > 0)
            {
                CT_Color c1 = ctFont.GetColorArray(0);
                CT_Color c2 = pr.AddNewColor();
                if (c1.IsSetAuto())
                {
                    c2.auto          = (c1.auto);
                    c2.autoSpecified = true;
                }
                if (c1.IsSetIndexed())
                {
                    c2.indexed          = (c1.indexed);
                    c2.indexedSpecified = true;
                }
                if (c1.IsSetRgb())
                {
                    c2.SetRgb(c1.rgb);
                    c2.rgbSpecified = true;
                }
                if (c1.IsSetTheme())
                {
                    c2.theme          = (c1.theme);
                    c2.themeSpecified = true;
                }
                if (c1.IsSetTint())
                {
                    c2.tint          = (c1.tint);
                    c2.tintSpecified = true;
                }
            }

            if (ctFont.sizeOfSzArray() > 0)
            {
                pr.AddNewSz().val = (ctFont.GetSzArray(0).val);
            }
            if (ctFont.sizeOfNameArray() > 0)
            {
                pr.AddNewRFont().val = (ctFont.name.val);
            }
            if (ctFont.sizeOfFamilyArray() > 0)
            {
                pr.AddNewFamily().val = (ctFont.GetFamilyArray(0).val);
            }
            if (ctFont.sizeOfSchemeArray() > 0)
            {
                pr.AddNewScheme().val = (ctFont.GetSchemeArray(0).val);
            }
            if (ctFont.sizeOfCharsetArray() > 0)
            {
                pr.AddNewCharset().val = (ctFont.GetCharsetArray(0).val);
            }
            if (ctFont.sizeOfCondenseArray() > 0)
            {
                pr.AddNewCondense().val = (ctFont.GetCondenseArray(0).val);
            }
            if (ctFont.sizeOfExtendArray() > 0)
            {
                pr.AddNewExtend().val = (ctFont.GetExtendArray(0).val);
            }
            if (ctFont.sizeOfVertAlignArray() > 0)
            {
                pr.AddNewVertAlign().val = (ctFont.GetVertAlignArray(0).val);
            }
            if (ctFont.sizeOfOutlineArray() > 0)
            {
                pr.AddNewOutline().val = (ctFont.GetOutlineArray(0).val);
            }
            if (ctFont.sizeOfShadowArray() > 0)
            {
                pr.AddNewShadow().val = (ctFont.GetShadowArray(0).val);
            }
            if (ctFont.sizeOfStrikeArray() > 0)
            {
                pr.AddNewStrike().val = (ctFont.GetStrikeArray(0).val);
            }
        }
Exemple #3
0
        /**
         *
         * CTRPrElt --> CTFont adapter
         */
        protected static CT_Font ToCTFont(CT_RPrElt pr)
        {
            CT_Font ctFont = new CT_Font();

            if (pr.SizeOfBArray() > 0)
            {
                ctFont.AddNewB().val = (pr.GetBArray(0).val);
            }
            if (pr.SizeOfUArray() > 0)
            {
                ctFont.AddNewU().val = (pr.GetUArray(0).val);
            }
            if (pr.SizeOfIArray() > 0)
            {
                ctFont.AddNewI().val = (pr.GetIArray(0).val);
            }
            if (pr.SizeOfColorArray() > 0)
            {
                CT_Color c1 = pr.GetColorArray(0);
                CT_Color c2 = ctFont.AddNewColor();
                if (c1.IsSetAuto())
                {
                    c2.auto          = (c1.auto);
                    c2.autoSpecified = true;
                }
                if (c1.IsSetIndexed())
                {
                    c2.indexed          = (c1.indexed);
                    c2.indexedSpecified = true;
                }
                if (c1.IsSetRgb())
                {
                    c2.SetRgb(c1.GetRgb());
                    c2.rgbSpecified = true;
                }
                if (c1.IsSetTheme())
                {
                    c2.theme          = (c1.theme);
                    c2.themeSpecified = true;
                }
                if (c1.IsSetTint())
                {
                    c2.tint          = (c1.tint);
                    c2.tintSpecified = true;
                }
            }

            if (pr.SizeOfSzArray() > 0)
            {
                ctFont.AddNewSz().val = (pr.GetSzArray(0).val);
            }
            if (pr.SizeOfRFontArray() > 0)
            {
                ctFont.AddNewName().val = (pr.GetRFontArray(0).val);
            }
            if (pr.SizeOfFamilyArray() > 0)
            {
                ctFont.AddNewFamily().val = (pr.GetFamilyArray(0).val);
            }
            if (pr.sizeOfSchemeArray() > 0)
            {
                ctFont.AddNewScheme().val = (pr.GetSchemeArray(0).val);
            }
            if (pr.sizeOfCharsetArray() > 0)
            {
                ctFont.AddNewCharset().val = (pr.GetCharsetArray(0).val);
            }
            if (pr.sizeOfCondenseArray() > 0)
            {
                ctFont.AddNewCondense().val = (pr.GetCondenseArray(0).val);
            }
            if (pr.sizeOfExtendArray() > 0)
            {
                ctFont.AddNewExtend().val = (pr.GetExtendArray(0).val);
            }
            if (pr.sizeOfVertAlignArray() > 0)
            {
                ctFont.AddNewVertAlign().val = (pr.GetVertAlignArray(0).val);
            }
            if (pr.sizeOfOutlineArray() > 0)
            {
                ctFont.AddNewOutline().val = (pr.GetOutlineArray(0).val);
            }
            if (pr.sizeOfShadowArray() > 0)
            {
                ctFont.AddNewShadow().val = (pr.GetShadowArray(0).val);
            }
            if (pr.sizeOfStrikeArray() > 0)
            {
                ctFont.AddNewStrike().val = (pr.GetStrikeArray(0).val);
            }

            return(ctFont);
        }
 private void SetRunAttributes(CT_Font ctFont, CT_RPrElt pr)
 {
     if (ctFont.sizeOfBArray() > 0)
     {
         pr.AddNewB().val = ctFont.GetBArray(0).val;
     }
     if (ctFont.sizeOfUArray() > 0)
     {
         pr.AddNewU().val = ctFont.GetUArray(0).val;
     }
     if (ctFont.sizeOfIArray() > 0)
     {
         pr.AddNewI().val = ctFont.GetIArray(0).val;
     }
     if (ctFont.sizeOfColorArray() > 0)
     {
         CT_Color colorArray = ctFont.GetColorArray(0);
         CT_Color ctColor    = pr.AddNewColor();
         if (colorArray.IsSetAuto())
         {
             ctColor.auto          = colorArray.auto;
             ctColor.autoSpecified = true;
         }
         if (colorArray.IsSetIndexed())
         {
             ctColor.indexed          = colorArray.indexed;
             ctColor.indexedSpecified = true;
         }
         if (colorArray.IsSetRgb())
         {
             ctColor.SetRgb(colorArray.rgb);
             ctColor.rgbSpecified = true;
         }
         if (colorArray.IsSetTheme())
         {
             ctColor.theme          = colorArray.theme;
             ctColor.themeSpecified = true;
         }
         if (colorArray.IsSetTint())
         {
             ctColor.tint          = colorArray.tint;
             ctColor.tintSpecified = true;
         }
     }
     if (ctFont.sizeOfSzArray() > 0)
     {
         pr.AddNewSz().val = ctFont.GetSzArray(0).val;
     }
     if (ctFont.sizeOfNameArray() > 0)
     {
         pr.AddNewRFont().val = ctFont.GetNameArray(0).val;
     }
     if (ctFont.sizeOfFamilyArray() > 0)
     {
         pr.AddNewFamily().val = ctFont.GetFamilyArray(0).val;
     }
     if (ctFont.sizeOfSchemeArray() > 0)
     {
         pr.AddNewScheme().val = ctFont.GetSchemeArray(0).val;
     }
     if (ctFont.sizeOfCharsetArray() > 0)
     {
         pr.AddNewCharset().val = ctFont.GetCharsetArray(0).val;
     }
     if (ctFont.sizeOfCondenseArray() > 0)
     {
         pr.AddNewCondense().val = ctFont.GetCondenseArray(0).val;
     }
     if (ctFont.sizeOfExtendArray() > 0)
     {
         pr.AddNewExtend().val = ctFont.GetExtendArray(0).val;
     }
     if (ctFont.sizeOfVertAlignArray() > 0)
     {
         pr.AddNewVertAlign().val = ctFont.GetVertAlignArray(0).val;
     }
     if (ctFont.sizeOfOutlineArray() > 0)
     {
         pr.AddNewOutline().val = ctFont.GetOutlineArray(0).val;
     }
     if (ctFont.sizeOfShadowArray() > 0)
     {
         pr.AddNewShadow().val = ctFont.GetShadowArray(0).val;
     }
     if (ctFont.sizeOfStrikeArray() <= 0)
     {
         return;
     }
     pr.AddNewStrike().val = ctFont.GetStrikeArray(0).val;
 }