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.

TestXSSFCellStyle.java 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertNotSame;
  21. import static org.junit.jupiter.api.Assertions.assertNull;
  22. import static org.junit.jupiter.api.Assertions.assertSame;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import java.io.IOException;
  25. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  26. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  27. import org.apache.poi.ss.usermodel.BorderStyle;
  28. import org.apache.poi.ss.usermodel.Cell;
  29. import org.apache.poi.ss.usermodel.CellStyle;
  30. import org.apache.poi.ss.usermodel.DataFormat;
  31. import org.apache.poi.ss.usermodel.FillPatternType;
  32. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  33. import org.apache.poi.ss.usermodel.IndexedColors;
  34. import org.apache.poi.ss.usermodel.ReadingOrder;
  35. import org.apache.poi.ss.usermodel.Row;
  36. import org.apache.poi.ss.usermodel.Sheet;
  37. import org.apache.poi.ss.usermodel.VerticalAlignment;
  38. import org.apache.poi.ss.usermodel.Workbook;
  39. import org.apache.poi.xssf.XSSFTestDataSamples;
  40. import org.apache.poi.xssf.model.StylesTable;
  41. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
  42. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  43. import org.junit.jupiter.api.BeforeEach;
  44. import org.junit.jupiter.api.Test;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
  47. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  48. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  49. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
  50. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
  51. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
  52. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
  53. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  54. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
  55. class TestXSSFCellStyle {
  56. private StylesTable stylesTable;
  57. private XSSFCellStyle cellStyle;
  58. @BeforeEach
  59. void setUp() {
  60. stylesTable = new StylesTable();
  61. CTStylesheet ctStylesheet = stylesTable.getCTStylesheet();
  62. CTBorder ctBorderA = CTBorder.Factory.newInstance();
  63. XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
  64. long borderId = stylesTable.putBorder(borderA);
  65. assertEquals(0, borderId);
  66. XSSFCellBorder borderB = new XSSFCellBorder();
  67. assertEquals(0, stylesTable.putBorder(borderB));
  68. CTFill ctFill = CTFill.Factory.newInstance();
  69. XSSFCellFill fill = new XSSFCellFill(ctFill, null);
  70. long fillId = stylesTable.putFill(fill);
  71. assertEquals(2, fillId);
  72. CTFont ctFont = CTFont.Factory.newInstance();
  73. XSSFFont font = new XSSFFont(ctFont);
  74. long fontId = stylesTable.putFont(font);
  75. assertEquals(1, fontId);
  76. CTXf cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
  77. cellStyleXf.setBorderId(1);
  78. cellStyleXf.setFillId(1);
  79. cellStyleXf.setFontId(1);
  80. CTCellXfs cellXfs = ctStylesheet.addNewCellXfs();
  81. CTXf cellXf = cellXfs.addNewXf();
  82. cellXf.setXfId(1);
  83. cellXf.setBorderId(1);
  84. cellXf.setFillId(1);
  85. cellXf.setFontId(1);
  86. assertEquals(2, stylesTable.putCellStyleXf(cellStyleXf));
  87. assertEquals(2, stylesTable.putCellXf(cellXf));
  88. cellStyle = new XSSFCellStyle(1, 1, stylesTable, null);
  89. assertNotNull(stylesTable.getFillAt(1).getCTFill().getPatternFill());
  90. assertEquals(STPatternType.INT_DARK_GRAY, stylesTable.getFillAt(1).getCTFill().getPatternFill().getPatternType().intValue());
  91. }
  92. @Test
  93. void testGetSetBorderBottom() {
  94. //default values
  95. assertEquals(BorderStyle.NONE, cellStyle.getBorderBottom());
  96. int num = stylesTable.getBorders().size();
  97. cellStyle.setBorderBottom(BorderStyle.MEDIUM);
  98. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderBottom());
  99. //a new border has been added
  100. assertEquals(num + 1, stylesTable.getBorders().size());
  101. //id of the created border
  102. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  103. assertTrue(borderId > 0);
  104. //check changes in the underlying xml bean
  105. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  106. assertSame(STBorderStyle.MEDIUM, ctBorder.getBottom().getStyle());
  107. num = stylesTable.getBorders().size();
  108. //setting the same border multiple times should not change borderId
  109. for (int i = 0; i < 3; i++) {
  110. cellStyle.setBorderBottom(BorderStyle.MEDIUM);
  111. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderBottom());
  112. }
  113. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  114. assertEquals(num, stylesTable.getBorders().size());
  115. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  116. //setting border to none removes the <bottom> element
  117. cellStyle.setBorderBottom(BorderStyle.NONE);
  118. assertEquals(num, stylesTable.getBorders().size());
  119. borderId = (int)cellStyle.getCoreXf().getBorderId();
  120. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  121. //none is not the same as "not set", therefore the following doesn't work any more
  122. //assertFalse(ctBorder.isSetBottom());
  123. //replacement:
  124. assertSame(STBorderStyle.NONE, ctBorder.getBottom().getStyle());
  125. }
  126. @Test
  127. void testGetSetBorderRight() {
  128. //default values
  129. assertEquals(BorderStyle.NONE, cellStyle.getBorderRight());
  130. int num = stylesTable.getBorders().size();
  131. cellStyle.setBorderRight(BorderStyle.MEDIUM);
  132. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderRight());
  133. //a new border has been added
  134. assertEquals(num + 1, stylesTable.getBorders().size());
  135. //id of the created border
  136. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  137. assertTrue(borderId > 0);
  138. //check changes in the underlying xml bean
  139. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  140. assertSame(STBorderStyle.MEDIUM, ctBorder.getRight().getStyle());
  141. num = stylesTable.getBorders().size();
  142. //setting the same border multiple times should not change borderId
  143. for (int i = 0; i < 3; i++) {
  144. cellStyle.setBorderRight(BorderStyle.MEDIUM);
  145. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderRight());
  146. }
  147. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  148. assertEquals(num, stylesTable.getBorders().size());
  149. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  150. //setting border to none removes the <right> element
  151. cellStyle.setBorderRight(BorderStyle.NONE);
  152. assertEquals(num, stylesTable.getBorders().size());
  153. borderId = (int)cellStyle.getCoreXf().getBorderId();
  154. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  155. //none is not the same as "not set", therefore the following doesn't work any more
  156. //assertFalse(ctBorder.isSetRight());
  157. //replacement:
  158. assertSame(STBorderStyle.NONE, ctBorder.getRight().getStyle());
  159. }
  160. @Test
  161. void testGetSetBorderLeft() {
  162. //default values
  163. assertEquals(BorderStyle.NONE, cellStyle.getBorderLeft());
  164. int num = stylesTable.getBorders().size();
  165. cellStyle.setBorderLeft(BorderStyle.MEDIUM);
  166. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderLeft());
  167. //a new border has been added
  168. assertEquals(num + 1, stylesTable.getBorders().size());
  169. //id of the created border
  170. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  171. assertTrue(borderId > 0);
  172. //check changes in the underlying xml bean
  173. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  174. assertSame(STBorderStyle.MEDIUM, ctBorder.getLeft().getStyle());
  175. num = stylesTable.getBorders().size();
  176. //setting the same border multiple times should not change borderId
  177. for (int i = 0; i < 3; i++) {
  178. cellStyle.setBorderLeft(BorderStyle.MEDIUM);
  179. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderLeft());
  180. }
  181. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  182. assertEquals(num, stylesTable.getBorders().size());
  183. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  184. //setting border to none removes the <left> element
  185. cellStyle.setBorderLeft(BorderStyle.NONE);
  186. assertEquals(num, stylesTable.getBorders().size());
  187. borderId = (int)cellStyle.getCoreXf().getBorderId();
  188. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  189. //none is not the same as "not set", therefore the following doesn't work any more
  190. //assertFalse(ctBorder.isSetLeft());
  191. //replacement:
  192. assertSame(STBorderStyle.NONE, ctBorder.getLeft().getStyle());
  193. }
  194. @Test
  195. void testGetSetBorderTop() {
  196. //default values
  197. assertEquals(BorderStyle.NONE, cellStyle.getBorderTop());
  198. int num = stylesTable.getBorders().size();
  199. cellStyle.setBorderTop(BorderStyle.MEDIUM);
  200. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderTop());
  201. //a new border has been added
  202. assertEquals(num + 1, stylesTable.getBorders().size());
  203. //id of the created border
  204. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  205. assertTrue(borderId > 0);
  206. //check changes in the underlying xml bean
  207. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  208. assertSame(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
  209. num = stylesTable.getBorders().size();
  210. //setting the same border multiple times should not change borderId
  211. for (int i = 0; i < 3; i++) {
  212. cellStyle.setBorderTop(BorderStyle.MEDIUM);
  213. assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderTop());
  214. }
  215. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  216. assertEquals(num, stylesTable.getBorders().size());
  217. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  218. //setting border to none removes the <top> element
  219. cellStyle.setBorderTop(BorderStyle.NONE);
  220. assertEquals(num, stylesTable.getBorders().size());
  221. borderId = (int)cellStyle.getCoreXf().getBorderId();
  222. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  223. //none is not the same as "not set", therefore the following doesn't work any more
  224. //assertFalse(ctBorder.isSetTop());
  225. //replacement:
  226. assertSame(STBorderStyle.NONE, ctBorder.getTop().getStyle());
  227. }
  228. private void testGetSetBorderXMLBean(BorderStyle border, STBorderStyle.Enum expected) {
  229. cellStyle.setBorderTop(border);
  230. assertEquals(border, cellStyle.getBorderTop());
  231. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  232. assertTrue(borderId > 0);
  233. //check changes in the underlying xml bean
  234. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  235. assertSame(expected, ctBorder.getTop().getStyle());
  236. }
  237. // Border Styles, in BorderStyle/STBorderStyle enum order
  238. @Test
  239. void testGetSetBorderNone() {
  240. cellStyle.setBorderTop(BorderStyle.NONE);
  241. assertEquals(BorderStyle.NONE, cellStyle.getBorderTop());
  242. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  243. // The default Style is already "none"
  244. // Therefore the new style already exists as Id=0
  245. //assertTrue(borderId > 0);
  246. // replacement:
  247. assertEquals(0, borderId);
  248. //check changes in the underlying xml bean
  249. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  250. assertNotNull(ctBorder.getTop());
  251. // no border style and STBorderStyle.NONE are equivalent
  252. // POI prefers to unset the border style than explicitly set it STBorderStyle.NONE
  253. }
  254. @Test
  255. void testGetSetBorderThin() {
  256. testGetSetBorderXMLBean(BorderStyle.THIN, STBorderStyle.THIN);
  257. }
  258. @Test
  259. void testGetSetBorderMedium() {
  260. testGetSetBorderXMLBean(BorderStyle.MEDIUM, STBorderStyle.MEDIUM);
  261. }
  262. @Test
  263. void testGetSetBorderDashed() {
  264. testGetSetBorderXMLBean(BorderStyle.DASHED, STBorderStyle.DASHED);
  265. }
  266. @Test
  267. void testGetSetBorderDotted() {
  268. testGetSetBorderXMLBean(BorderStyle.DOTTED, STBorderStyle.DOTTED);
  269. }
  270. @Test
  271. void testGetSetBorderThick() {
  272. testGetSetBorderXMLBean(BorderStyle.THICK, STBorderStyle.THICK);
  273. }
  274. @Test
  275. void testGetSetBorderDouble() {
  276. testGetSetBorderXMLBean(BorderStyle.DOUBLE, STBorderStyle.DOUBLE);
  277. }
  278. @Test
  279. void testGetSetBorderHair() {
  280. testGetSetBorderXMLBean(BorderStyle.HAIR, STBorderStyle.HAIR);
  281. }
  282. @Test
  283. void testGetSetBorderMediumDashed() {
  284. testGetSetBorderXMLBean(BorderStyle.MEDIUM_DASHED, STBorderStyle.MEDIUM_DASHED);
  285. }
  286. @Test
  287. void testGetSetBorderDashDot() {
  288. testGetSetBorderXMLBean(BorderStyle.DASH_DOT, STBorderStyle.DASH_DOT);
  289. }
  290. @Test
  291. void testGetSetBorderMediumDashDot() {
  292. testGetSetBorderXMLBean(BorderStyle.MEDIUM_DASH_DOT, STBorderStyle.MEDIUM_DASH_DOT);
  293. }
  294. @Test
  295. void testGetSetBorderDashDotDot() {
  296. testGetSetBorderXMLBean(BorderStyle.DASH_DOT_DOT, STBorderStyle.DASH_DOT_DOT);
  297. }
  298. @Test
  299. void testGetSetBorderMediumDashDotDot() {
  300. testGetSetBorderXMLBean(BorderStyle.MEDIUM_DASH_DOT_DOT, STBorderStyle.MEDIUM_DASH_DOT_DOT);
  301. }
  302. @Test
  303. void testGetSetBorderSlantDashDot() {
  304. testGetSetBorderXMLBean(BorderStyle.SLANTED_DASH_DOT, STBorderStyle.SLANT_DASH_DOT);
  305. }
  306. @Test
  307. void testGetSetBottomBorderColor() {
  308. //defaults
  309. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getBottomBorderColor());
  310. assertNull(cellStyle.getBottomBorderXSSFColor());
  311. int num = stylesTable.getBorders().size();
  312. XSSFColor clr;
  313. //setting indexed color
  314. cellStyle.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
  315. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getBottomBorderColor());
  316. clr = cellStyle.getBottomBorderXSSFColor();
  317. assertTrue(clr.getCTColor().isSetIndexed());
  318. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  319. //a new border was added to the styles table
  320. assertEquals(num + 1, stylesTable.getBorders().size());
  321. //id of the created border
  322. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  323. assertTrue(borderId > 0);
  324. //check changes in the underlying xml bean
  325. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  326. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getBottom().getColor().getIndexed());
  327. //setting XSSFColor
  328. num = stylesTable.getBorders().size();
  329. clr = new XSSFColor(java.awt.Color.CYAN, stylesTable.getIndexedColors());
  330. cellStyle.setBottomBorderColor(clr);
  331. assertEquals(clr.getCTColor().toString(), cellStyle.getBottomBorderXSSFColor().getCTColor().toString());
  332. byte[] rgb = cellStyle.getBottomBorderXSSFColor().getRGB();
  333. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  334. //another border was added to the styles table
  335. assertEquals(num + 1, stylesTable.getBorders().size());
  336. //passing null unsets the color
  337. cellStyle.setBottomBorderColor(null);
  338. assertNull(cellStyle.getBottomBorderXSSFColor());
  339. }
  340. @Test
  341. void testGetSetTopBorderColor() {
  342. //defaults
  343. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getTopBorderColor());
  344. assertNull(cellStyle.getTopBorderXSSFColor());
  345. int num = stylesTable.getBorders().size();
  346. XSSFColor clr;
  347. //setting indexed color
  348. cellStyle.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
  349. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getTopBorderColor());
  350. clr = cellStyle.getTopBorderXSSFColor();
  351. assertTrue(clr.getCTColor().isSetIndexed());
  352. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  353. //a new border was added to the styles table
  354. assertEquals(num + 1, stylesTable.getBorders().size());
  355. //id of the created border
  356. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  357. assertTrue(borderId > 0);
  358. //check changes in the underlying xml bean
  359. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  360. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getTop().getColor().getIndexed());
  361. //setting XSSFColor
  362. num = stylesTable.getBorders().size();
  363. clr = new XSSFColor(java.awt.Color.CYAN, stylesTable.getIndexedColors());
  364. cellStyle.setTopBorderColor(clr);
  365. assertEquals(clr.getCTColor().toString(), cellStyle.getTopBorderXSSFColor().getCTColor().toString());
  366. byte[] rgb = cellStyle.getTopBorderXSSFColor().getRGB();
  367. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  368. //another border was added to the styles table
  369. assertEquals(num + 1, stylesTable.getBorders().size());
  370. //passing null unsets the color
  371. cellStyle.setTopBorderColor(null);
  372. assertNull(cellStyle.getTopBorderXSSFColor());
  373. }
  374. @Test
  375. void testGetSetLeftBorderColor() {
  376. //defaults
  377. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getLeftBorderColor());
  378. assertNull(cellStyle.getLeftBorderXSSFColor());
  379. int num = stylesTable.getBorders().size();
  380. XSSFColor clr;
  381. //setting indexed color
  382. cellStyle.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
  383. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getLeftBorderColor());
  384. clr = cellStyle.getLeftBorderXSSFColor();
  385. assertTrue(clr.getCTColor().isSetIndexed());
  386. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  387. //a new border was added to the styles table
  388. assertEquals(num + 1, stylesTable.getBorders().size());
  389. //id of the created border
  390. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  391. assertTrue(borderId > 0);
  392. //check changes in the underlying xml bean
  393. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  394. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getLeft().getColor().getIndexed());
  395. //setting XSSFColor
  396. num = stylesTable.getBorders().size();
  397. clr = new XSSFColor(java.awt.Color.CYAN, stylesTable.getIndexedColors());
  398. cellStyle.setLeftBorderColor(clr);
  399. assertEquals(clr.getCTColor().toString(), cellStyle.getLeftBorderXSSFColor().getCTColor().toString());
  400. byte[] rgb = cellStyle.getLeftBorderXSSFColor().getRGB();
  401. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  402. //another border was added to the styles table
  403. assertEquals(num + 1, stylesTable.getBorders().size());
  404. //passing null unsets the color
  405. cellStyle.setLeftBorderColor(null);
  406. assertNull(cellStyle.getLeftBorderXSSFColor());
  407. }
  408. @Test
  409. void testGetSetRightBorderColor() {
  410. //defaults
  411. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getRightBorderColor());
  412. assertNull(cellStyle.getRightBorderXSSFColor());
  413. int num = stylesTable.getBorders().size();
  414. XSSFColor clr;
  415. //setting indexed color
  416. cellStyle.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
  417. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getRightBorderColor());
  418. clr = cellStyle.getRightBorderXSSFColor();
  419. assertTrue(clr.getCTColor().isSetIndexed());
  420. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  421. //a new border was added to the styles table
  422. assertEquals(num + 1, stylesTable.getBorders().size());
  423. //id of the created border
  424. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  425. assertTrue(borderId > 0);
  426. //check changes in the underlying xml bean
  427. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  428. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getRight().getColor().getIndexed());
  429. //setting XSSFColor
  430. num = stylesTable.getBorders().size();
  431. clr = new XSSFColor(java.awt.Color.CYAN, stylesTable.getIndexedColors());
  432. cellStyle.setRightBorderColor(clr);
  433. assertEquals(clr.getCTColor().toString(), cellStyle.getRightBorderXSSFColor().getCTColor().toString());
  434. byte[] rgb = cellStyle.getRightBorderXSSFColor().getRGB();
  435. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  436. //another border was added to the styles table
  437. assertEquals(num + 1, stylesTable.getBorders().size());
  438. //passing null unsets the color
  439. cellStyle.setRightBorderColor(null);
  440. assertNull(cellStyle.getRightBorderXSSFColor());
  441. }
  442. @Test
  443. void testGetSetFillBackgroundColor() {
  444. assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
  445. assertNull(cellStyle.getFillBackgroundXSSFColor());
  446. XSSFColor clr;
  447. int num = stylesTable.getFills().size();
  448. //setting indexed color
  449. cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
  450. assertEquals(IndexedColors.RED.getIndex(), cellStyle.getFillBackgroundColor());
  451. clr = cellStyle.getFillBackgroundXSSFColor();
  452. assertTrue(clr.getCTColor().isSetIndexed());
  453. assertEquals(IndexedColors.RED.getIndex(), clr.getIndexed());
  454. //a new fill was added to the styles table
  455. assertEquals(num + 1, stylesTable.getFills().size());
  456. //id of the created border
  457. int fillId = (int)cellStyle.getCoreXf().getFillId();
  458. assertTrue(fillId > 0);
  459. //check changes in the underlying xml bean
  460. CTFill ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
  461. assertEquals(IndexedColors.RED.getIndex(), ctFill2.getPatternFill().getBgColor().getIndexed());
  462. //setting XSSFColor
  463. num = stylesTable.getFills().size();
  464. clr = new XSSFColor(java.awt.Color.CYAN, stylesTable.getIndexedColors());
  465. cellStyle.setFillBackgroundColor(clr);
  466. assertEquals(clr.getCTColor().toString(), cellStyle.getFillBackgroundXSSFColor().getCTColor().toString());
  467. byte[] rgb = cellStyle.getFillBackgroundXSSFColor().getRGB();
  468. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  469. //another border was added to the styles table
  470. assertEquals(num + 1, stylesTable.getFills().size());
  471. //passing null unsets the color
  472. cellStyle.setFillBackgroundColor(null);
  473. assertNull(cellStyle.getFillBackgroundXSSFColor());
  474. assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
  475. }
  476. @Test
  477. void testDefaultStyles() throws IOException {
  478. XSSFWorkbook wb1 = new XSSFWorkbook();
  479. XSSFCellStyle style1 = wb1.createCellStyle();
  480. assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
  481. assertNull(style1.getFillBackgroundXSSFColor());
  482. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb1));
  483. wb1.close();
  484. //compatibility with HSSF
  485. HSSFWorkbook wb2 = new HSSFWorkbook();
  486. HSSFCellStyle style2 = wb2.createCellStyle();
  487. assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor());
  488. assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor());
  489. assertEquals(style2.getFillPattern(), style1.getFillPattern());
  490. assertEquals(style2.getFillPattern(), style1.getFillPattern());
  491. assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor());
  492. assertEquals(style2.getTopBorderColor(), style1.getTopBorderColor());
  493. assertEquals(style2.getRightBorderColor(), style1.getRightBorderColor());
  494. assertEquals(style2.getBottomBorderColor(), style1.getBottomBorderColor());
  495. assertEquals(style2.getBorderBottom(), style1.getBorderBottom());
  496. assertEquals(style2.getBorderLeft(), style1.getBorderLeft());
  497. assertEquals(style2.getBorderRight(), style1.getBorderRight());
  498. assertEquals(style2.getBorderTop(), style1.getBorderTop());
  499. wb2.close();
  500. }
  501. @Test
  502. void testGetFillForegroundColor() throws IOException {
  503. XSSFWorkbook wb = new XSSFWorkbook();
  504. StylesTable styles = wb.getStylesSource();
  505. assertEquals(1, wb.getNumCellStyles());
  506. assertEquals(2, styles.getFills().size());
  507. XSSFCellStyle defaultStyle = wb.getCellStyleAt(0);
  508. assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor());
  509. assertNull(defaultStyle.getFillForegroundXSSFColor());
  510. assertEquals(FillPatternType.NO_FILL, defaultStyle.getFillPattern());
  511. XSSFCellStyle customStyle = wb.createCellStyle();
  512. customStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  513. assertEquals(FillPatternType.SOLID_FOREGROUND, customStyle.getFillPattern());
  514. assertEquals(3, styles.getFills().size());
  515. customStyle.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  516. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), customStyle.getFillForegroundColor());
  517. assertEquals(4, styles.getFills().size());
  518. for (int i = 0; i < 3; i++) {
  519. XSSFCellStyle style = wb.createCellStyle();
  520. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  521. assertEquals(FillPatternType.SOLID_FOREGROUND, style.getFillPattern());
  522. assertEquals(4, styles.getFills().size());
  523. style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  524. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
  525. assertEquals(4, styles.getFills().size());
  526. }
  527. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  528. wb.close();
  529. }
  530. @Test
  531. void testGetFillPattern() {
  532. assertEquals(STPatternType.INT_DARK_GRAY-1, cellStyle.getFillPattern().getCode());
  533. int num = stylesTable.getFills().size();
  534. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  535. assertEquals(FillPatternType.SOLID_FOREGROUND, cellStyle.getFillPattern());
  536. assertEquals(num + 1, stylesTable.getFills().size());
  537. int fillId = (int)cellStyle.getCoreXf().getFillId();
  538. assertTrue(fillId > 0);
  539. //check changes in the underlying xml bean
  540. CTFill ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
  541. assertSame(STPatternType.SOLID, ctFill2.getPatternFill().getPatternType());
  542. //setting the same fill multiple time does not update the styles table
  543. for (int i = 0; i < 3; i++) {
  544. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  545. }
  546. assertEquals(num + 1, stylesTable.getFills().size());
  547. cellStyle.setFillPattern(FillPatternType.NO_FILL);
  548. assertEquals(FillPatternType.NO_FILL, cellStyle.getFillPattern());
  549. fillId = (int)cellStyle.getCoreXf().getFillId();
  550. ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
  551. assertNull(ctFill2.getPatternFill());
  552. }
  553. @Test
  554. void testGetFont() {
  555. assertNotNull(cellStyle.getFont());
  556. }
  557. @Test
  558. void testGetSetHidden() {
  559. assertFalse(cellStyle.getHidden());
  560. cellStyle.setHidden(true);
  561. assertTrue(cellStyle.getHidden());
  562. cellStyle.setHidden(false);
  563. assertFalse(cellStyle.getHidden());
  564. }
  565. @Test
  566. void testGetSetLocked() {
  567. assertTrue(cellStyle.getLocked());
  568. cellStyle.setLocked(true);
  569. assertTrue(cellStyle.getLocked());
  570. cellStyle.setLocked(false);
  571. assertFalse(cellStyle.getLocked());
  572. }
  573. @Test
  574. void testGetSetIndent() {
  575. assertEquals((short)0, cellStyle.getIndention());
  576. cellStyle.setIndention((short)3);
  577. assertEquals((short)3, cellStyle.getIndention());
  578. cellStyle.setIndention((short) 13);
  579. assertEquals((short)13, cellStyle.getIndention());
  580. }
  581. @Test
  582. void testGetSetAlignment() {
  583. assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  584. assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignment());
  585. cellStyle.setAlignment(HorizontalAlignment.LEFT);
  586. assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignment());
  587. assertSame(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  588. cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
  589. assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignment());
  590. assertSame(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  591. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  592. assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignment());
  593. assertSame(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  594. }
  595. @Test
  596. void testGetSetReadingOrder() {
  597. assertEquals(ReadingOrder.CONTEXT, cellStyle.getReadingOrder());
  598. assertEquals(ReadingOrder.CONTEXT.getCode(), cellStyle.getCellAlignment().getCTCellAlignment().getReadingOrder());
  599. cellStyle.setReadingOrder(ReadingOrder.LEFT_TO_RIGHT);
  600. assertEquals(ReadingOrder.LEFT_TO_RIGHT, cellStyle.getReadingOrder());
  601. assertEquals(ReadingOrder.LEFT_TO_RIGHT.getCode(), cellStyle.getCellAlignment().getCTCellAlignment().getReadingOrder());
  602. cellStyle.setReadingOrder(ReadingOrder.RIGHT_TO_LEFT);
  603. assertEquals(ReadingOrder.RIGHT_TO_LEFT, cellStyle.getReadingOrder());
  604. assertEquals(ReadingOrder.RIGHT_TO_LEFT.getCode(), cellStyle.getCellAlignment().getCTCellAlignment().getReadingOrder());
  605. cellStyle.setReadingOrder(ReadingOrder.CONTEXT);
  606. assertEquals(ReadingOrder.CONTEXT, cellStyle.getReadingOrder());
  607. assertEquals(ReadingOrder.CONTEXT.getCode(), cellStyle.getCellAlignment().getCTCellAlignment().getReadingOrder());
  608. }
  609. @Test
  610. void testGetSetVerticalAlignment() {
  611. assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignment());
  612. assertFalse(cellStyle.getCellAlignment().getCTCellAlignment().isSetVertical());
  613. assertSame(STVerticalAlignment.BOTTOM, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  614. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  615. assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignment());
  616. assertSame(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  617. cellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);
  618. assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignment());
  619. assertSame(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  620. }
  621. @Test
  622. void testGetSetWrapText() {
  623. assertFalse(cellStyle.getWrapText());
  624. cellStyle.setWrapText(true);
  625. assertTrue(cellStyle.getWrapText());
  626. cellStyle.setWrapText(false);
  627. assertFalse(cellStyle.getWrapText());
  628. }
  629. /**
  630. * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
  631. */
  632. @Test
  633. void testCloneStyleSameWB() throws IOException {
  634. XSSFWorkbook wb = new XSSFWorkbook();
  635. assertEquals(1, wb.getNumberOfFonts());
  636. XSSFFont fnt = wb.createFont();
  637. fnt.setFontName("TestingFont");
  638. assertEquals(2, wb.getNumberOfFonts());
  639. XSSFCellStyle orig = wb.createCellStyle();
  640. orig.setAlignment(HorizontalAlignment.RIGHT);
  641. orig.setFont(fnt);
  642. orig.setDataFormat((short)18);
  643. assertEquals(HorizontalAlignment.RIGHT, orig.getAlignment());
  644. assertEquals(fnt, orig.getFont());
  645. assertEquals(18, orig.getDataFormat());
  646. XSSFCellStyle clone = wb.createCellStyle();
  647. assertNotSame(HorizontalAlignment.RIGHT, clone.getAlignment());
  648. assertNotSame(fnt, clone.getFont());
  649. assertNotEquals(18, clone.getDataFormat());
  650. clone.cloneStyleFrom(orig);
  651. assertEquals(HorizontalAlignment.RIGHT, clone.getAlignment());
  652. assertEquals(fnt, clone.getFont());
  653. assertEquals(18, clone.getDataFormat());
  654. assertEquals(2, wb.getNumberOfFonts());
  655. XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
  656. assertNotNull(wb2);
  657. wb2.close();
  658. wb.close();
  659. }
  660. /**
  661. * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
  662. */
  663. @Test
  664. void testCloneStyleDiffWB() throws IOException {
  665. XSSFWorkbook wbOrig = new XSSFWorkbook();
  666. assertEquals(1, wbOrig.getNumberOfFonts());
  667. assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
  668. XSSFFont fnt = wbOrig.createFont();
  669. fnt.setFontName("TestingFont");
  670. assertEquals(2, wbOrig.getNumberOfFonts());
  671. assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
  672. XSSFDataFormat fmt = wbOrig.createDataFormat();
  673. fmt.getFormat("MadeUpOne");
  674. fmt.getFormat("MadeUpTwo");
  675. XSSFCellStyle orig = wbOrig.createCellStyle();
  676. orig.setAlignment(HorizontalAlignment.RIGHT);
  677. orig.setFont(fnt);
  678. orig.setDataFormat(fmt.getFormat("Test##"));
  679. orig.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  680. orig.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  681. XSSFCellStyle origEmpty = wbOrig.createCellStyle();
  682. assertNotNull(origEmpty);
  683. assertSame(HorizontalAlignment.RIGHT, orig.getAlignment());
  684. assertSame(fnt, orig.getFont());
  685. assertEquals(fmt.getFormat("Test##"), orig.getDataFormat());
  686. assertEquals(2, wbOrig.getNumberOfFonts());
  687. assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
  688. // Now a style on another workbook
  689. XSSFWorkbook wbClone = new XSSFWorkbook();
  690. assertEquals(1, wbClone.getNumberOfFonts());
  691. assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
  692. assertEquals(1, wbClone.getNumCellStyles());
  693. XSSFDataFormat fmtClone = wbClone.createDataFormat();
  694. XSSFCellStyle clone = wbClone.createCellStyle();
  695. assertEquals(1, wbClone.getNumberOfFonts());
  696. assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
  697. assertNotSame(HorizontalAlignment.RIGHT, clone.getAlignment());
  698. assertNotEquals("TestingFont", clone.getFont().getFontName());
  699. clone.cloneStyleFrom(orig);
  700. assertEquals(2, wbClone.getNumberOfFonts());
  701. assertEquals(2, wbClone.getNumCellStyles());
  702. assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
  703. assertEquals(HorizontalAlignment.RIGHT, clone.getAlignment());
  704. assertEquals("TestingFont", clone.getFont().getFontName());
  705. assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
  706. assertNotEquals(fmtClone.getFormat("Test##"), fmt.getFormat("Test##"));
  707. assertEquals(FillPatternType.SOLID_FOREGROUND, clone.getFillPattern());
  708. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), clone.getFillForegroundColor());
  709. // Save it and re-check
  710. XSSFWorkbook wbReload = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
  711. assertEquals(2, wbReload.getNumberOfFonts());
  712. assertEquals(2, wbReload.getNumCellStyles());
  713. assertEquals(1, wbReload.getStylesSource().getNumberFormats().size());
  714. XSSFCellStyle reload = wbReload.getCellStyleAt((short)1);
  715. assertEquals(HorizontalAlignment.RIGHT, reload.getAlignment());
  716. assertEquals("TestingFont", reload.getFont().getFontName());
  717. assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
  718. assertNotEquals(fmtClone.getFormat("Test##"), fmt.getFormat("Test##"));
  719. assertEquals(FillPatternType.SOLID_FOREGROUND, clone.getFillPattern());
  720. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), clone.getFillForegroundColor());
  721. XSSFWorkbook wbOrig2 = XSSFTestDataSamples.writeOutAndReadBack(wbOrig);
  722. assertNotNull(wbOrig2);
  723. wbOrig2.close();
  724. XSSFWorkbook wbClone2 = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
  725. assertNotNull(wbClone2);
  726. wbClone2.close();
  727. wbReload.close();
  728. wbClone.close();
  729. wbOrig.close();
  730. }
  731. /**
  732. * Avoid ArrayIndexOutOfBoundsException when creating cell style
  733. * in a workbook that has an empty xf table.
  734. */
  735. @Test
  736. void testBug52348() throws IOException {
  737. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
  738. StylesTable st = workbook.getStylesSource();
  739. assertEquals(0, st._getStyleXfsSize());
  740. XSSFCellStyle style = workbook.createCellStyle(); // no exception at this point
  741. assertNull(style.getStyleXf());
  742. XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
  743. assertNotNull(wb2);
  744. wb2.close();
  745. workbook.close();
  746. }
  747. /**
  748. * Avoid ArrayIndexOutOfBoundsException when getting cell style
  749. * in a workbook that has an empty xf table.
  750. */
  751. @Test
  752. void testBug55650() throws IOException {
  753. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
  754. StylesTable st = workbook.getStylesSource();
  755. assertEquals(0, st._getStyleXfsSize());
  756. // no exception at this point
  757. XSSFCellStyle style = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
  758. assertNull(style.getStyleXf());
  759. XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
  760. assertNotNull(wb2);
  761. wb2.close();
  762. workbook.close();
  763. }
  764. @Test
  765. void testShrinkToFit() throws IOException {
  766. // Existing file
  767. XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("ShrinkToFit.xlsx");
  768. Sheet s = wb1.getSheetAt(0);
  769. Row r = s.getRow(0);
  770. CellStyle cs = r.getCell(0).getCellStyle();
  771. assertTrue(cs.getShrinkToFit());
  772. // New file
  773. XSSFWorkbook wb2 = new XSSFWorkbook();
  774. s = wb2.createSheet();
  775. r = s.createRow(0);
  776. cs = wb2.createCellStyle();
  777. cs.setShrinkToFit(false);
  778. r.createCell(0).setCellStyle(cs);
  779. cs = wb2.createCellStyle();
  780. cs.setShrinkToFit(true);
  781. r.createCell(1).setCellStyle(cs);
  782. // Write out, read, and check
  783. XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
  784. s = wb3.getSheetAt(0);
  785. r = s.getRow(0);
  786. assertFalse(r.getCell(0).getCellStyle().getShrinkToFit());
  787. assertTrue(r.getCell(1).getCellStyle().getShrinkToFit());
  788. XSSFWorkbook wb4 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
  789. assertNotNull(wb4);
  790. wb4.close();
  791. XSSFWorkbook wb5 = XSSFTestDataSamples.writeOutAndReadBack(wb3);
  792. assertNotNull(wb5);
  793. wb5.close();
  794. wb3.close();
  795. wb2.close();
  796. wb1.close();
  797. }
  798. @Test
  799. void testSetColor() throws IOException {
  800. try(Workbook wb = new XSSFWorkbook()) {
  801. Sheet sheet = wb.createSheet();
  802. Row row = sheet.createRow(0);
  803. DataFormat format = wb.createDataFormat();
  804. Cell cell = row.createCell(1);
  805. cell.setCellValue("somevalue");
  806. CellStyle cellStyle2 = wb.createCellStyle();
  807. cellStyle2.setDataFormat(format.getFormat("###0"));
  808. cellStyle2.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex());
  809. cellStyle2.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
  810. cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  811. cellStyle2.setAlignment(HorizontalAlignment.RIGHT);
  812. cellStyle2.setVerticalAlignment(VerticalAlignment.TOP);
  813. cell.setCellStyle(cellStyle2);
  814. try (Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
  815. Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(1);
  816. assertNotNull(cellBack);
  817. CellStyle styleBack = cellBack.getCellStyle();
  818. assertEquals(IndexedColors.DARK_BLUE.getIndex(), styleBack.getFillBackgroundColor());
  819. assertEquals(IndexedColors.DARK_BLUE.getIndex(), styleBack.getFillForegroundColor());
  820. assertEquals(HorizontalAlignment.RIGHT, styleBack.getAlignment());
  821. assertEquals(VerticalAlignment.TOP, styleBack.getVerticalAlignment());
  822. assertEquals(FillPatternType.SOLID_FOREGROUND, styleBack.getFillPattern());
  823. }
  824. }
  825. }
  826. public static void copyStyles(Workbook reference, Workbook target) {
  827. final int numberOfStyles = reference.getNumCellStyles();
  828. // don't copy default style (style index 0)
  829. for (int i = 1; i < numberOfStyles; i++) {
  830. final CellStyle referenceStyle = reference.getCellStyleAt(i);
  831. final CellStyle targetStyle = target.createCellStyle();
  832. targetStyle.cloneStyleFrom(referenceStyle);
  833. }
  834. }
  835. @Test
  836. void test58084() throws IOException {
  837. Workbook reference = XSSFTestDataSamples.openSampleWorkbook("template.xlsx");
  838. Workbook target = new XSSFWorkbook();
  839. copyStyles(reference, target);
  840. assertEquals(reference.getNumCellStyles(), target.getNumCellStyles());
  841. final Sheet sheet = target.createSheet();
  842. final Row row = sheet.createRow(0);
  843. int col = 0;
  844. for (short i = 1; i < target.getNumCellStyles(); i++) {
  845. final Cell cell = row.createCell(col++);
  846. cell.setCellValue("Coucou"+i);
  847. cell.setCellStyle(target.getCellStyleAt(i));
  848. }
  849. Workbook copy = XSSFTestDataSamples.writeOutAndReadBack(target);
  850. // previously this failed because the border-element was not copied over
  851. copy.getCellStyleAt((short)1).getBorderBottom();
  852. copy.close();
  853. target.close();
  854. reference.close();
  855. }
  856. @Test
  857. void test58043() {
  858. assertEquals(0, cellStyle.getRotation());
  859. cellStyle.setRotation((short)89);
  860. assertEquals(89, cellStyle.getRotation());
  861. cellStyle.setRotation((short)90);
  862. assertEquals(90, cellStyle.getRotation());
  863. cellStyle.setRotation((short)179);
  864. assertEquals(179, cellStyle.getRotation());
  865. cellStyle.setRotation((short)180);
  866. assertEquals(180, cellStyle.getRotation());
  867. // negative values are mapped to the correct values for compatibility between HSSF and XSSF
  868. cellStyle.setRotation((short)-1);
  869. assertEquals(91, cellStyle.getRotation());
  870. cellStyle.setRotation((short)-89);
  871. assertEquals(179, cellStyle.getRotation());
  872. cellStyle.setRotation((short)-90);
  873. assertEquals(180, cellStyle.getRotation());
  874. }
  875. @Test
  876. void bug58996_UsedToWorkIn3_11_ButNotIn3_13() throws IOException {
  877. XSSFWorkbook workbook = new XSSFWorkbook();
  878. XSSFCellStyle cellStyle = workbook.createCellStyle();
  879. cellStyle.setFillForegroundColor(null);
  880. assertNull(cellStyle.getFillForegroundColorColor());
  881. cellStyle.setFillBackgroundColor(null);
  882. assertNull(cellStyle.getFillBackgroundColorColor());
  883. cellStyle.setFillPattern(FillPatternType.NO_FILL);
  884. assertEquals(FillPatternType.NO_FILL, cellStyle.getFillPattern());
  885. cellStyle.setBottomBorderColor(null);
  886. assertNull(cellStyle.getBottomBorderXSSFColor());
  887. cellStyle.setTopBorderColor(null);
  888. assertNull(cellStyle.getTopBorderXSSFColor());
  889. cellStyle.setLeftBorderColor(null);
  890. assertNull(cellStyle.getLeftBorderXSSFColor());
  891. cellStyle.setRightBorderColor(null);
  892. assertNull(cellStyle.getRightBorderXSSFColor());
  893. workbook.close();
  894. }
  895. }