You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestTableStyles.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package org.apache.poi.xssf.usermodel;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNotNull;
  4. import static org.junit.Assert.assertNull;
  5. import static org.junit.Assert.assertTrue;
  6. import org.apache.poi.ss.usermodel.DifferentialStyleProvider;
  7. import org.apache.poi.ss.usermodel.FontFormatting;
  8. import org.apache.poi.ss.usermodel.PatternFormatting;
  9. import org.apache.poi.ss.usermodel.Table;
  10. import org.apache.poi.ss.usermodel.TableStyle;
  11. import org.apache.poi.ss.usermodel.TableStyleInfo;
  12. import org.apache.poi.ss.usermodel.TableStyleType;
  13. import org.apache.poi.xssf.XSSFTestDataSamples;
  14. import org.junit.Test;
  15. /**
  16. * Test built-in table styles
  17. */
  18. public class TestTableStyles {
  19. /**
  20. * Test that a built-in style is initialized properly
  21. */
  22. @Test
  23. public void testBuiltinStyleInit() {
  24. TableStyle style = XSSFBuiltinTableStyle.TableStyleMedium2.getStyle();
  25. assertNotNull("no style found for Medium2", style);
  26. assertNull("Should not have style info for blankRow", style.getStyle(TableStyleType.blankRow));
  27. DifferentialStyleProvider headerRow = style.getStyle(TableStyleType.headerRow);
  28. assertNotNull("no header row style", headerRow);
  29. FontFormatting font = headerRow.getFontFormatting();
  30. assertNotNull("No header row font formatting", font);
  31. assertTrue("header row not bold", font.isBold());
  32. PatternFormatting fill = headerRow.getPatternFormatting();
  33. assertNotNull("No header fill", fill);
  34. assertEquals("wrong header fill", 4, ((XSSFColor) fill.getFillBackgroundColorColor()).getTheme());
  35. }
  36. @Test
  37. public void testCustomStyle() throws Exception {
  38. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("tableStyle.xlsx");
  39. Table table = wb.getTable("Table1");
  40. assertNotNull("missing table", table);
  41. TableStyleInfo style = table.getStyle();
  42. assertNotNull("Missing table style info", style);
  43. assertNotNull("Missing table style", style.getStyle());
  44. assertEquals("Wrong name", "TestTableStyle", style.getName());
  45. assertEquals("Wrong name", "TestTableStyle", style.getStyle().getName());
  46. DifferentialStyleProvider firstColumn = style.getStyle().getStyle(TableStyleType.firstColumn);
  47. assertNotNull("no first column style", firstColumn);
  48. FontFormatting font = firstColumn.getFontFormatting();
  49. assertNotNull("no first col font", font);
  50. assertTrue("wrong first col bold", font.isBold());
  51. wb.close();
  52. }
  53. }