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 43KB

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