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.

XSSFCellStyle.java 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import org.apache.poi.common.Duplicatable;
  18. import org.apache.poi.ooxml.POIXMLException;
  19. import org.apache.poi.ss.usermodel.BorderStyle;
  20. import org.apache.poi.ss.usermodel.CellStyle;
  21. import org.apache.poi.ss.usermodel.FillPatternType;
  22. import org.apache.poi.ss.usermodel.Font;
  23. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  24. import org.apache.poi.ss.usermodel.IndexedColors;
  25. import org.apache.poi.ss.usermodel.ReadingOrder;
  26. import org.apache.poi.ss.usermodel.VerticalAlignment;
  27. import org.apache.poi.util.Internal;
  28. import org.apache.poi.util.Removal;
  29. import org.apache.poi.xssf.model.StylesTable;
  30. import org.apache.poi.xssf.model.ThemesTable;
  31. import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment;
  32. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
  33. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  34. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  35. import org.apache.xmlbeans.XmlException;
  36. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
  37. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
  38. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  42. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
  43. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
  44. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  46. /**
  47. *
  48. * High level representation of the possible formatting information for the contents of the cells on a sheet in a
  49. * SpreadsheetML document.
  50. *
  51. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#createCellStyle()
  52. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getCellStyleAt(int)
  53. * @see org.apache.poi.xssf.usermodel.XSSFCell#setCellStyle(org.apache.poi.ss.usermodel.CellStyle)
  54. */
  55. public class XSSFCellStyle implements CellStyle, Duplicatable {
  56. private int _cellXfId;
  57. private final StylesTable _stylesSource;
  58. private CTXf _cellXf;
  59. private final CTXf _cellStyleXf;
  60. private XSSFFont _font;
  61. private XSSFCellAlignment _cellAlignment;
  62. private ThemesTable _theme;
  63. /**
  64. * Creates a Cell Style from the supplied parts
  65. * @param cellXfId The main XF for the cell. Must be a valid 0-based index into the XF table
  66. * @param cellStyleXfId Optional, style xf. A value of <code>-1</code> means no xf.
  67. * @param stylesSource Styles Source to work off
  68. */
  69. public XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource, ThemesTable theme) {
  70. _cellXfId = cellXfId;
  71. _stylesSource = stylesSource;
  72. _cellXf = stylesSource.getCellXfAt(this._cellXfId);
  73. _cellStyleXf = cellStyleXfId == -1 ? null : stylesSource.getCellStyleXfAt(cellStyleXfId);
  74. _theme = theme;
  75. }
  76. /**
  77. * Used so that StylesSource can figure out our location
  78. */
  79. @Internal
  80. public CTXf getCoreXf() {
  81. return _cellXf;
  82. }
  83. /**
  84. * Used so that StylesSource can figure out our location
  85. */
  86. @Internal
  87. public CTXf getStyleXf() {
  88. return _cellStyleXf;
  89. }
  90. /**
  91. * Creates an empty Cell Style
  92. */
  93. public XSSFCellStyle(StylesTable stylesSource) {
  94. _stylesSource = stylesSource;
  95. // We need a new CTXf for the main styles
  96. // TODO decide on a style ctxf
  97. _cellXf = CTXf.Factory.newInstance();
  98. _cellStyleXf = null;
  99. }
  100. /**
  101. * Verifies that this style belongs to the supplied Workbook
  102. * Styles Source.
  103. * Will throw an exception if it belongs to a different one.
  104. * This is normally called when trying to assign a style to a
  105. * cell, to ensure the cell and the style are from the same
  106. * workbook (if they're not, it won't work)
  107. * @throws IllegalArgumentException if there's a workbook mis-match
  108. */
  109. public void verifyBelongsToStylesSource(StylesTable src) {
  110. if(this._stylesSource != src) {
  111. throw new IllegalArgumentException("This Style does not belong to the supplied Workbook Styles Source. Are you trying to assign a style from one workbook to the cell of a different workbook?");
  112. }
  113. }
  114. /**
  115. * Clones all the style information from another
  116. * XSSFCellStyle, onto this one. This
  117. * XSSFCellStyle will then have all the same
  118. * properties as the source, but the two may
  119. * be edited independently.
  120. * Any stylings on this XSSFCellStyle will be lost!
  121. *
  122. * The source XSSFCellStyle could be from another
  123. * XSSFWorkbook if you like. This allows you to
  124. * copy styles from one XSSFWorkbook to another.
  125. */
  126. @Override
  127. public void cloneStyleFrom(CellStyle source) {
  128. if(source instanceof XSSFCellStyle) {
  129. XSSFCellStyle src = (XSSFCellStyle)source;
  130. // Is it on our Workbook?
  131. if(src._stylesSource == _stylesSource) {
  132. // Nice and easy
  133. _cellXf.set(src.getCoreXf());
  134. _cellStyleXf.set(src.getStyleXf());
  135. } else {
  136. // Copy the style
  137. try {
  138. // Remove any children off the current style, to
  139. // avoid orphaned nodes
  140. if(_cellXf.isSetAlignment())
  141. _cellXf.unsetAlignment();
  142. if(_cellXf.isSetExtLst())
  143. _cellXf.unsetExtLst();
  144. // Create a new Xf with the same contents
  145. _cellXf = CTXf.Factory.parse(
  146. src.getCoreXf().toString(), DEFAULT_XML_OPTIONS
  147. );
  148. // bug 56295: ensure that the fills is available and set correctly
  149. CTFill fill = CTFill.Factory.parse(
  150. src.getCTFill().toString(), DEFAULT_XML_OPTIONS
  151. );
  152. addFill(fill);
  153. // bug 58084: set borders correctly
  154. CTBorder border = CTBorder.Factory.parse(
  155. src.getCTBorder().toString(), DEFAULT_XML_OPTIONS
  156. );
  157. addBorder(border);
  158. // Swap it over
  159. _stylesSource.replaceCellXfAt(_cellXfId, _cellXf);
  160. } catch(XmlException e) {
  161. throw new POIXMLException(e);
  162. }
  163. // Copy the format
  164. String fmt = src.getDataFormatString();
  165. setDataFormat(
  166. (new XSSFDataFormat(_stylesSource)).getFormat(fmt)
  167. );
  168. // Copy the font
  169. try {
  170. CTFont ctFont = CTFont.Factory.parse(
  171. src.getFont().getCTFont().toString(), DEFAULT_XML_OPTIONS
  172. );
  173. XSSFFont font = new XSSFFont(ctFont);
  174. font.registerTo(_stylesSource);
  175. setFont(font);
  176. } catch(XmlException e) {
  177. throw new POIXMLException(e);
  178. }
  179. }
  180. // Clear out cached details
  181. _font = null;
  182. _cellAlignment = null;
  183. } else {
  184. throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
  185. }
  186. }
  187. private void addFill(CTFill fill) {
  188. int idx = _stylesSource.putFill(new XSSFCellFill(fill,_stylesSource.getIndexedColors()));
  189. _cellXf.setFillId(idx);
  190. _cellXf.setApplyFill(true);
  191. }
  192. private void addBorder(CTBorder border) {
  193. int idx = _stylesSource.putBorder(new XSSFCellBorder(border, _theme,_stylesSource.getIndexedColors()));
  194. _cellXf.setBorderId(idx);
  195. _cellXf.setApplyBorder(true);
  196. }
  197. @Override
  198. public HorizontalAlignment getAlignment() {
  199. if(!_cellXf.getApplyAlignment()) return HorizontalAlignment.GENERAL;
  200. CTCellAlignment align = _cellXf.getAlignment();
  201. if(align != null && align.isSetHorizontal()) {
  202. return HorizontalAlignment.forInt(align.getHorizontal().intValue()-1);
  203. }
  204. return HorizontalAlignment.GENERAL;
  205. }
  206. @Override
  207. public BorderStyle getBorderBottom() {
  208. if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
  209. int idx = Math.toIntExact(_cellXf.getBorderId());
  210. CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
  211. STBorderStyle.Enum ptrn = ct.isSetBottom() ? ct.getBottom().getStyle() : null;
  212. if (ptrn == null) {
  213. return BorderStyle.NONE;
  214. }
  215. return BorderStyle.valueOf((short)(ptrn.intValue() - 1));
  216. }
  217. @Override
  218. public BorderStyle getBorderLeft() {
  219. if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
  220. int idx = Math.toIntExact(_cellXf.getBorderId());
  221. CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
  222. STBorderStyle.Enum ptrn = ct.isSetLeft() ? ct.getLeft().getStyle() : null;
  223. if (ptrn == null) {
  224. return BorderStyle.NONE;
  225. }
  226. return BorderStyle.valueOf((short)(ptrn.intValue() - 1));
  227. }
  228. @Override
  229. public BorderStyle getBorderRight() {
  230. if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
  231. int idx = Math.toIntExact(_cellXf.getBorderId());
  232. CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
  233. STBorderStyle.Enum ptrn = ct.isSetRight() ? ct.getRight().getStyle() : null;
  234. if (ptrn == null) {
  235. return BorderStyle.NONE;
  236. }
  237. return BorderStyle.valueOf((short)(ptrn.intValue() - 1));
  238. }
  239. @Override
  240. public BorderStyle getBorderTop() {
  241. if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
  242. int idx = Math.toIntExact(_cellXf.getBorderId());
  243. CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
  244. STBorderStyle.Enum ptrn = ct.isSetTop() ? ct.getTop().getStyle() : null;
  245. if (ptrn == null) {
  246. return BorderStyle.NONE;
  247. }
  248. return BorderStyle.valueOf((short) (ptrn.intValue() - 1));
  249. }
  250. /**
  251. * Get the color to use for the bottom border
  252. * <br>
  253. * Color is optional. When missing, IndexedColors.AUTOMATIC is implied.
  254. * @return the index of the color definition, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#AUTOMATIC}
  255. * @see org.apache.poi.ss.usermodel.IndexedColors
  256. */
  257. @Override
  258. public short getBottomBorderColor() {
  259. XSSFColor clr = getBottomBorderXSSFColor();
  260. return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
  261. }
  262. /**
  263. * Get the color to use for the bottom border as a {@link XSSFColor}
  264. *
  265. * @return the used color or <code>null</code> if not set
  266. */
  267. public XSSFColor getBottomBorderXSSFColor() {
  268. if(!_cellXf.getApplyBorder()) return null;
  269. int idx = Math.toIntExact(_cellXf.getBorderId());
  270. XSSFCellBorder border = _stylesSource.getBorderAt(idx);
  271. return border.getBorderColor(BorderSide.BOTTOM);
  272. }
  273. /**
  274. * Get the index of the number format (numFmt) record used by this cell format.
  275. *
  276. * @return the index of the number format
  277. */
  278. @Override
  279. public short getDataFormat() {
  280. return (short)_cellXf.getNumFmtId();
  281. }
  282. /**
  283. * Get the contents of the format string, by looking up
  284. * the StylesSource
  285. *
  286. * @return the number format string
  287. */
  288. @Override
  289. public String getDataFormatString() {
  290. int idx = getDataFormat();
  291. return new XSSFDataFormat(_stylesSource).getFormat((short)idx);
  292. }
  293. /**
  294. * Get the background fill color.
  295. * <p>
  296. * Note - many cells are actually filled with a foreground
  297. * fill, not a background fill - see {@link #getFillForegroundColor()}
  298. * </p>
  299. * @return fill color, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#AUTOMATIC}
  300. * @see org.apache.poi.ss.usermodel.IndexedColors
  301. */
  302. @Override
  303. public short getFillBackgroundColor() {
  304. XSSFColor clr = getFillBackgroundXSSFColor();
  305. return clr == null ? IndexedColors.AUTOMATIC.getIndex() : clr.getIndexed();
  306. }
  307. @Override
  308. public XSSFColor getFillBackgroundColorColor() {
  309. return getFillBackgroundXSSFColor();
  310. }
  311. /**
  312. * Get the background fill color.
  313. * <p>
  314. * Note - many cells are actually filled with a foreground
  315. * fill, not a background fill - see {@link #getFillForegroundColor()}
  316. * </p>
  317. * @see org.apache.poi.xssf.usermodel.XSSFColor#getRGB()
  318. * @return XSSFColor - fill color or <code>null</code> if not set
  319. */
  320. public XSSFColor getFillBackgroundXSSFColor() {
  321. // bug 56295: handle missing applyFill attribute as "true" because Excel does as well
  322. if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return null;
  323. int fillIndex = (int)_cellXf.getFillId();
  324. XSSFCellFill fg = _stylesSource.getFillAt(fillIndex);
  325. XSSFColor fillBackgroundColor = fg.getFillBackgroundColor();
  326. if (fillBackgroundColor != null && _theme != null) {
  327. _theme.inheritFromThemeAsRequired(fillBackgroundColor);
  328. }
  329. return fillBackgroundColor;
  330. }
  331. /**
  332. * Get the foreground fill color.
  333. * <p>
  334. * Many cells are filled with this, instead of a
  335. * background color ({@link #getFillBackgroundColor()})
  336. * </p>
  337. * @see IndexedColors
  338. * @return fill color, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#AUTOMATIC}
  339. */
  340. @Override
  341. public short getFillForegroundColor() {
  342. XSSFColor clr = getFillForegroundXSSFColor();
  343. return clr == null ? IndexedColors.AUTOMATIC.getIndex() : clr.getIndexed();
  344. }
  345. @Override
  346. public XSSFColor getFillForegroundColorColor() {
  347. return getFillForegroundXSSFColor();
  348. }
  349. /**
  350. * Get the foreground fill color.
  351. *
  352. * @return XSSFColor - fill color or <code>null</code> if not set
  353. */
  354. public XSSFColor getFillForegroundXSSFColor() {
  355. // bug 56295: handle missing applyFill attribute as "true" because Excel does as well
  356. if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return null;
  357. int fillIndex = (int)_cellXf.getFillId();
  358. XSSFCellFill fg = _stylesSource.getFillAt(fillIndex);
  359. XSSFColor fillForegroundColor = fg.getFillForegroundColor();
  360. if (fillForegroundColor != null && _theme != null) {
  361. _theme.inheritFromThemeAsRequired(fillForegroundColor);
  362. }
  363. return fillForegroundColor;
  364. }
  365. @Override
  366. public FillPatternType getFillPattern() {
  367. // bug 56295: handle missing applyFill attribute as "true" because Excel does as well
  368. if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return FillPatternType.NO_FILL;
  369. int fillIndex = (int)_cellXf.getFillId();
  370. XSSFCellFill fill = _stylesSource.getFillAt(fillIndex);
  371. STPatternType.Enum ptrn = fill.getPatternType();
  372. if(ptrn == null) return FillPatternType.NO_FILL;
  373. return FillPatternType.forInt(ptrn.intValue() - 1);
  374. }
  375. /**
  376. * Gets the font for this style
  377. * @return Font - font
  378. */
  379. public XSSFFont getFont() {
  380. if (_font == null) {
  381. _font = _stylesSource.getFontAt(getFontId());
  382. }
  383. return _font;
  384. }
  385. /**
  386. * Gets the index of the font for this style
  387. *
  388. * @return font index
  389. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(int)
  390. * @since 5.0.0 (used to return a short value)
  391. */
  392. @Override
  393. public int getFontIndex() {
  394. return getFontId();
  395. }
  396. /**
  397. * Gets the index of the font for this style
  398. *
  399. * @return font index
  400. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(int)
  401. * @deprecated use {@link #getFontIndex()} instead
  402. * @since 4.0.0
  403. */
  404. @Deprecated
  405. @Removal(version = "6.0.0")
  406. @Override
  407. public int getFontIndexAsInt() {
  408. return getFontId();
  409. }
  410. /**
  411. * Get whether the cell's using this style are to be hidden
  412. *
  413. * @return boolean - whether the cell using this style is hidden
  414. */
  415. @Override
  416. public boolean getHidden() {
  417. return _cellXf.isSetProtection() && _cellXf.getProtection().isSetHidden() && _cellXf.getProtection().getHidden();
  418. }
  419. /**
  420. * Get the number of spaces to indent the text in the cell
  421. *
  422. * @return indent - number of spaces
  423. */
  424. @Override
  425. public short getIndention() {
  426. CTCellAlignment align = _cellXf.getAlignment();
  427. return (short)(align == null ? 0 : align.getIndent());
  428. }
  429. /**
  430. * Get the index within the StylesTable (sequence within the collection of CTXf elements)
  431. *
  432. * @return unique index number of the underlying record this style represents, as a short (may wrap)
  433. */
  434. @Override
  435. public short getIndex() {
  436. return (short)this._cellXfId;
  437. }
  438. /**
  439. * Workaround for places where we need to support more than 32767 cell styles, ideally
  440. * the main getIndex() and others would return int, not short, but that would affect some
  441. * public APIs
  442. *
  443. * @return unique index number of the underlying record this style represents, as an int (always positive)
  444. */
  445. protected int getUIndex() {
  446. return this._cellXfId;
  447. }
  448. /**
  449. * Get the color to use for the left border
  450. *
  451. * @return the index of the color definition, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#BLACK}
  452. * @see org.apache.poi.ss.usermodel.IndexedColors
  453. */
  454. @Override
  455. public short getLeftBorderColor() {
  456. XSSFColor clr = getLeftBorderXSSFColor();
  457. return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
  458. }
  459. /**
  460. * Get the color to use for the left border
  461. *
  462. * @return the index of the color definition or <code>null</code> if not set
  463. * @see org.apache.poi.ss.usermodel.IndexedColors
  464. */
  465. public XSSFColor getLeftBorderXSSFColor() {
  466. if(!_cellXf.getApplyBorder()) return null;
  467. int idx = Math.toIntExact(_cellXf.getBorderId());
  468. XSSFCellBorder border = _stylesSource.getBorderAt(idx);
  469. return border.getBorderColor(BorderSide.LEFT);
  470. }
  471. /**
  472. * Get whether the cell's using this style are locked
  473. *
  474. * @return whether the cell using this style are locked
  475. */
  476. @Override
  477. public boolean getLocked() {
  478. return !_cellXf.isSetProtection() || !_cellXf.getProtection().isSetLocked() || _cellXf.getProtection().getLocked();
  479. }
  480. /**
  481. * Is "Quote Prefix" or "123 Prefix" enabled for the cell?
  482. */
  483. @Override
  484. public boolean getQuotePrefixed() {
  485. return _cellXf.getQuotePrefix();
  486. }
  487. /**
  488. * Get the color to use for the right border
  489. *
  490. * @return the index of the color definition, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#BLACK}
  491. * @see org.apache.poi.ss.usermodel.IndexedColors
  492. */
  493. @Override
  494. public short getRightBorderColor() {
  495. XSSFColor clr = getRightBorderXSSFColor();
  496. return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
  497. }
  498. /**
  499. * Get the color to use for the right border
  500. *
  501. * @return the used color or <code>null</code> if not set
  502. */
  503. public XSSFColor getRightBorderXSSFColor() {
  504. if(!_cellXf.getApplyBorder()) return null;
  505. int idx = Math.toIntExact(_cellXf.getBorderId());
  506. XSSFCellBorder border = _stylesSource.getBorderAt(idx);
  507. return border.getBorderColor(BorderSide.RIGHT);
  508. }
  509. /**
  510. * Get the degree of rotation for the text in the cell
  511. * <p>
  512. * Expressed in degrees. Values range from 0 to 180. The first letter of
  513. * the text is considered the center-point of the arc.
  514. * <br>
  515. * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
  516. * horizon is calculated as:
  517. * <br>
  518. * <code>[degrees below horizon] = 90 - textRotation.</code>
  519. * </p>
  520. *
  521. * @return rotation degrees (between 0 and 180 degrees)
  522. */
  523. @Override
  524. public short getRotation() {
  525. CTCellAlignment align = _cellXf.getAlignment();
  526. return align == null || align.getTextRotation() == null ? 0 : align.getTextRotation().shortValue();
  527. }
  528. @Override
  529. public boolean getShrinkToFit() {
  530. CTCellAlignment align = _cellXf.getAlignment();
  531. return align != null && align.getShrinkToFit();
  532. }
  533. /**
  534. * Get the color to use for the top border
  535. *
  536. * @return the index of the color definition, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#BLACK}
  537. * @see org.apache.poi.ss.usermodel.IndexedColors
  538. */
  539. @Override
  540. public short getTopBorderColor() {
  541. XSSFColor clr = getTopBorderXSSFColor();
  542. return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
  543. }
  544. /**
  545. * Get the color to use for the top border
  546. *
  547. * @return the used color or <code>null</code> if not set
  548. */
  549. public XSSFColor getTopBorderXSSFColor() {
  550. if(!_cellXf.getApplyBorder()) return null;
  551. int idx = Math.toIntExact(_cellXf.getBorderId());
  552. XSSFCellBorder border = _stylesSource.getBorderAt(idx);
  553. return border.getBorderColor(BorderSide.TOP);
  554. }
  555. @Override
  556. public VerticalAlignment getVerticalAlignment() {
  557. if(!_cellXf.getApplyAlignment()) return VerticalAlignment.BOTTOM;
  558. CTCellAlignment align = _cellXf.getAlignment();
  559. if(align != null && align.isSetVertical()) {
  560. return VerticalAlignment.forInt(align.getVertical().intValue()-1);
  561. }
  562. return VerticalAlignment.BOTTOM;
  563. }
  564. /**
  565. * Whether the text should be wrapped
  566. *
  567. * @return a boolean value indicating if the text in a cell should be line-wrapped within the cell.
  568. */
  569. @Override
  570. public boolean getWrapText() {
  571. CTCellAlignment align = _cellXf.getAlignment();
  572. return align != null && align.getWrapText();
  573. }
  574. /**
  575. * Set the type of horizontal alignment for the cell
  576. *
  577. * @param align - the type of alignment
  578. */
  579. @Override
  580. public void setAlignment(HorizontalAlignment align) {
  581. _cellXf.setApplyAlignment(true);
  582. getCellAlignment().setHorizontal(align);
  583. }
  584. /**
  585. * Set the type of border to use for the bottom border of the cell
  586. *
  587. * @param border - type of border to use
  588. * @see org.apache.poi.ss.usermodel.BorderStyle
  589. * @since POI 3.15
  590. */
  591. @Override
  592. public void setBorderBottom(BorderStyle border) {
  593. CTBorder ct = getCTBorder();
  594. CTBorderPr pr = ct.isSetBottom() ? ct.getBottom() : ct.addNewBottom();
  595. if(border == BorderStyle.NONE) ct.unsetBottom();
  596. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  597. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme, _stylesSource.getIndexedColors()));
  598. _cellXf.setBorderId(idx);
  599. _cellXf.setApplyBorder(true);
  600. }
  601. /**
  602. * Set the type of border to use for the left border of the cell
  603. *
  604. * @param border the type of border to use
  605. * @since POI 3.15
  606. */
  607. @Override
  608. public void setBorderLeft(BorderStyle border) {
  609. CTBorder ct = getCTBorder();
  610. CTBorderPr pr = ct.isSetLeft() ? ct.getLeft() : ct.addNewLeft();
  611. if(border == BorderStyle.NONE) ct.unsetLeft();
  612. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  613. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme, _stylesSource.getIndexedColors()));
  614. _cellXf.setBorderId(idx);
  615. _cellXf.setApplyBorder(true);
  616. }
  617. /**
  618. * Set the type of border to use for the right border of the cell
  619. *
  620. * @param border the type of border to use
  621. * @since POI 3.15
  622. */
  623. @Override
  624. public void setBorderRight(BorderStyle border) {
  625. CTBorder ct = getCTBorder();
  626. CTBorderPr pr = ct.isSetRight() ? ct.getRight() : ct.addNewRight();
  627. if(border == BorderStyle.NONE) ct.unsetRight();
  628. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  629. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,_stylesSource.getIndexedColors()));
  630. _cellXf.setBorderId(idx);
  631. _cellXf.setApplyBorder(true);
  632. }
  633. /**
  634. * Set the type of border to use for the top border of the cell
  635. *
  636. * @param border the type of border to use
  637. * @since POI 3.15
  638. */
  639. @Override
  640. public void setBorderTop(BorderStyle border) {
  641. CTBorder ct = getCTBorder();
  642. CTBorderPr pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
  643. if(border == BorderStyle.NONE) ct.unsetTop();
  644. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  645. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,_stylesSource.getIndexedColors()));
  646. _cellXf.setBorderId(idx);
  647. _cellXf.setApplyBorder(true);
  648. }
  649. /**
  650. * Set the color to use for the bottom border
  651. * @param color the index of the color definition
  652. * @see org.apache.poi.ss.usermodel.IndexedColors
  653. */
  654. @Override
  655. public void setBottomBorderColor(short color) {
  656. XSSFColor clr = XSSFColor.from(CTColor.Factory.newInstance(), _stylesSource.getIndexedColors());
  657. clr.setIndexed(color);
  658. setBottomBorderColor(clr);
  659. }
  660. /**
  661. * Set the color to use for the bottom border
  662. *
  663. * @param color the color to use, null means no color
  664. */
  665. public void setBottomBorderColor(XSSFColor color) {
  666. CTBorder ct = getCTBorder();
  667. if(color == null && !ct.isSetBottom()) return;
  668. CTBorderPr pr = ct.isSetBottom() ? ct.getBottom() : ct.addNewBottom();
  669. if(color != null) pr.setColor(color.getCTColor());
  670. else pr.unsetColor();
  671. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,_stylesSource.getIndexedColors()));
  672. _cellXf.setBorderId(idx);
  673. _cellXf.setApplyBorder(true);
  674. }
  675. /**
  676. * Set the index of a data format
  677. *
  678. * @param fmt the index of a data format
  679. */
  680. @Override
  681. public void setDataFormat(short fmt) {
  682. // XSSF supports >32,767 formats
  683. setDataFormat(fmt&0xffff);
  684. }
  685. /**
  686. * Set the index of a data format
  687. *
  688. * @param fmt the index of a data format
  689. */
  690. public void setDataFormat(int fmt) {
  691. _cellXf.setApplyNumberFormat(true);
  692. _cellXf.setNumFmtId(fmt);
  693. }
  694. /**
  695. * Set the background fill color represented as a {@link XSSFColor} value.
  696. * <p>
  697. * For example:
  698. * <pre>
  699. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  700. * cs.setFillBackgroundXSSFColor(new XSSFColor(java.awt.Color.RED));
  701. * </pre>
  702. * optionally a Foreground and background fill can be applied:
  703. * <i>Note: Ensure Foreground color is set prior to background</i>
  704. * <pre>
  705. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  706. * cs.setFillForegroundColor(new XSSFColor(java.awt.Color.BLUE));
  707. * cs.setFillBackgroundColor(new XSSFColor(java.awt.Color.GREEN));
  708. * </pre>
  709. * or, for the special case of SOLID_FILL:
  710. * <pre>
  711. * cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND );
  712. * cs.setFillForegroundColor(new XSSFColor(java.awt.Color.GREEN));
  713. * </pre>
  714. * It is necessary to set the fill style in order
  715. * for the color to be shown in the cell.
  716. *
  717. * @param color - the color to use
  718. */
  719. public void setFillBackgroundColor(XSSFColor color) {
  720. CTFill ct = getCTFill();
  721. CTPatternFill ptrn = ct.getPatternFill();
  722. if(color == null) {
  723. if(ptrn != null && ptrn.isSetBgColor()) ptrn.unsetBgColor();
  724. } else {
  725. if(ptrn == null) ptrn = ct.addNewPatternFill();
  726. ptrn.setBgColor(color.getCTColor());
  727. }
  728. addFill(ct);
  729. }
  730. /**
  731. * Set the background fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value.
  732. * <br>
  733. * @param color org.apache.poi.ss.usermodel.Color to set
  734. * @throws IllegalArgumentException if you provide a <code>Color</code> instance that is not a {@link XSSFColor}
  735. * @since POI 5.2.3
  736. */
  737. @Override
  738. public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color) {
  739. if (color == null || color instanceof XSSFColor) {
  740. setFillBackgroundColor((XSSFColor)color);
  741. } else {
  742. throw new IllegalArgumentException("XSSFCellStyle only accepts XSSFColor instances");
  743. }
  744. }
  745. /**
  746. * Set the background fill color represented as a indexed color value.
  747. * <p>
  748. * For example:
  749. * <pre>
  750. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  751. * cs.setFillBackgroundXSSFColor(IndexedColors.RED.getIndex());
  752. * </pre>
  753. * optionally a Foreground and background fill can be applied:
  754. * <i>Note: Ensure Foreground color is set prior to background</i>
  755. * <pre>
  756. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  757. * cs.setFillForegroundColor(IndexedColors.BLUE.getIndex());
  758. * cs.setFillBackgroundColor(IndexedColors.RED.getIndex());
  759. * </pre>
  760. * or, for the special case of SOLID_FILL:
  761. * <pre>
  762. * cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND );
  763. * cs.setFillForegroundColor(IndexedColors.RED.getIndex());
  764. * </pre>
  765. * It is necessary to set the fill style in order
  766. * for the color to be shown in the cell.
  767. *
  768. * @param bg - the color to use
  769. * @see org.apache.poi.ss.usermodel.IndexedColors
  770. */
  771. @Override
  772. public void setFillBackgroundColor(short bg) {
  773. XSSFColor clr = XSSFColor.from(CTColor.Factory.newInstance(), _stylesSource.getIndexedColors());
  774. clr.setIndexed(bg);
  775. setFillBackgroundColor(clr);
  776. }
  777. /**
  778. * Set the foreground fill color represented as a {@link XSSFColor} value.
  779. * <br>
  780. * <i>Note: Ensure Foreground color is set prior to background color.</i>
  781. * @param color the color to use
  782. * @see #setFillBackgroundColor(org.apache.poi.xssf.usermodel.XSSFColor) )
  783. */
  784. public void setFillForegroundColor(XSSFColor color) {
  785. CTFill ct = getCTFill();
  786. CTPatternFill ptrn = ct.getPatternFill();
  787. if(color == null) {
  788. if(ptrn != null && ptrn.isSetFgColor()) ptrn.unsetFgColor();
  789. } else {
  790. if(ptrn == null) ptrn = ct.addNewPatternFill();
  791. ptrn.setFgColor(color.getCTColor());
  792. }
  793. addFill(ct);
  794. }
  795. /**
  796. * Set the foreground fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value.
  797. * <br>
  798. * @param color the color to use
  799. * @throws IllegalArgumentException if you provide a <code>Color</code> instance that is not a {@link XSSFColor}
  800. * @since POI 5.2.3
  801. */
  802. @Override
  803. public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color) {
  804. if (color == null || color instanceof XSSFColor) {
  805. setFillForegroundColor((XSSFColor)color);
  806. } else {
  807. throw new IllegalArgumentException("XSSFCellStyle only accepts XSSFColor instances");
  808. }
  809. }
  810. /**
  811. * Set the foreground fill color as a indexed color value
  812. * <br>
  813. * <i>Note: Ensure Foreground color is set prior to background color.</i>
  814. * @param fg the color to use
  815. * @see org.apache.poi.ss.usermodel.IndexedColors
  816. */
  817. @Override
  818. public void setFillForegroundColor(short fg) {
  819. XSSFColor clr = XSSFColor.from(CTColor.Factory.newInstance(), _stylesSource.getIndexedColors());
  820. clr.setIndexed(fg);
  821. setFillForegroundColor(clr);
  822. }
  823. /**
  824. * Get a <b>copy</b> of the currently used CTFill, if none is used, return a new instance.
  825. */
  826. private CTFill getCTFill(){
  827. CTFill ct;
  828. // bug 56295: handle missing applyFill attribute as "true" because Excel does as well
  829. if(!_cellXf.isSetApplyFill() || _cellXf.getApplyFill()) {
  830. int fillIndex = (int)_cellXf.getFillId();
  831. XSSFCellFill cf = _stylesSource.getFillAt(fillIndex);
  832. ct = (CTFill)cf.getCTFill().copy();
  833. } else {
  834. ct = CTFill.Factory.newInstance();
  835. }
  836. return ct;
  837. }
  838. /**
  839. * Set reading order for the cell
  840. *
  841. * @param order - the reading order
  842. */
  843. public void setReadingOrder(ReadingOrder order) {
  844. getCellAlignment().setReadingOrder(order);
  845. }
  846. /**
  847. * Get reading order of the cell
  848. *
  849. * @return ReadingOrder - the reading order
  850. */
  851. public ReadingOrder getReadingOrder() {
  852. return getCellAlignment().getReadingOrder();
  853. }
  854. /**
  855. * Get a <b>copy</b> of the currently used CTBorder, if none is used, return a new instance.
  856. */
  857. private CTBorder getCTBorder(){
  858. CTBorder ct;
  859. if(_cellXf.getApplyBorder()) {
  860. int idx = Math.toIntExact(_cellXf.getBorderId());
  861. XSSFCellBorder cf = _stylesSource.getBorderAt(idx);
  862. ct = (CTBorder)cf.getCTBorder().copy();
  863. } else {
  864. ct = CTBorder.Factory.newInstance();
  865. }
  866. return ct;
  867. }
  868. /**
  869. * This element is used to specify cell fill information for pattern and solid color cell fills. For solid cell fills (no pattern),
  870. * foreground color is used is used. For cell fills with patterns specified, then the cell fill color is specified by the background color element.
  871. *
  872. * @param pattern the fill pattern to use
  873. * @see #setFillBackgroundColor(XSSFColor)
  874. * @see #setFillForegroundColor(XSSFColor)
  875. * @see org.apache.poi.ss.usermodel.FillPatternType
  876. */
  877. @Override
  878. public void setFillPattern(FillPatternType pattern) {
  879. CTFill ct = getCTFill();
  880. CTPatternFill ctptrn = ct.isSetPatternFill() ? ct.getPatternFill() : ct.addNewPatternFill();
  881. if (pattern == FillPatternType.NO_FILL && ctptrn.isSetPatternType()) {
  882. ctptrn.unsetPatternType();
  883. } else {
  884. ctptrn.setPatternType(STPatternType.Enum.forInt(pattern.getCode() + 1));
  885. }
  886. addFill(ct);
  887. }
  888. /**
  889. * Set the font for this style
  890. *
  891. * @param font a font object created or retrieved from the XSSFWorkbook object
  892. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#createFont()
  893. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(int)
  894. */
  895. @Override
  896. public void setFont(Font font) {
  897. if(font != null){
  898. long index = font.getIndex();
  899. this._cellXf.setFontId(index);
  900. this._cellXf.setApplyFont(true);
  901. } else {
  902. this._cellXf.setApplyFont(false);
  903. }
  904. }
  905. /**
  906. * Set the cell's using this style to be hidden
  907. *
  908. * @param hidden - whether the cell using this style should be hidden
  909. */
  910. @Override
  911. public void setHidden(boolean hidden) {
  912. if (!_cellXf.isSetProtection()) {
  913. _cellXf.addNewProtection();
  914. }
  915. _cellXf.getProtection().setHidden(hidden);
  916. }
  917. /**
  918. * Set the number of spaces to indent the text in the cell
  919. *
  920. * @param indent - number of spaces
  921. */
  922. @Override
  923. public void setIndention(short indent) {
  924. getCellAlignment().setIndent(indent);
  925. }
  926. /**
  927. * Set the color to use for the left border as a indexed color value
  928. *
  929. * @param color the index of the color definition
  930. * @see org.apache.poi.ss.usermodel.IndexedColors
  931. */
  932. @Override
  933. public void setLeftBorderColor(short color) {
  934. XSSFColor clr = XSSFColor.from(CTColor.Factory.newInstance(), _stylesSource.getIndexedColors());
  935. clr.setIndexed(color);
  936. setLeftBorderColor(clr);
  937. }
  938. /**
  939. * Set the color to use for the left border as a {@link XSSFColor} value
  940. *
  941. * @param color the color to use
  942. */
  943. public void setLeftBorderColor(XSSFColor color) {
  944. CTBorder ct = getCTBorder();
  945. if(color == null && !ct.isSetLeft()) return;
  946. CTBorderPr pr = ct.isSetLeft() ? ct.getLeft() : ct.addNewLeft();
  947. if(color != null) pr.setColor(color.getCTColor());
  948. else pr.unsetColor();
  949. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,_stylesSource.getIndexedColors()));
  950. _cellXf.setBorderId(idx);
  951. _cellXf.setApplyBorder(true);
  952. }
  953. /**
  954. * Set the cell's using this style to be locked
  955. *
  956. * @param locked - whether the cell using this style should be locked
  957. */
  958. @Override
  959. public void setLocked(boolean locked) {
  960. if (!_cellXf.isSetProtection()) {
  961. _cellXf.addNewProtection();
  962. }
  963. _cellXf.getProtection().setLocked(locked);
  964. }
  965. /**
  966. * Turn on or off "Quote Prefix" or "123 Prefix" for the style,
  967. * which is used to tell Excel that the thing which looks like
  968. * a number or a formula shouldn't be treated as on.
  969. */
  970. @Override
  971. public void setQuotePrefixed(boolean quotePrefix) {
  972. _cellXf.setQuotePrefix(quotePrefix);
  973. }
  974. /**
  975. * Set the color to use for the right border
  976. *
  977. * @param color the index of the color definition
  978. * @see org.apache.poi.ss.usermodel.IndexedColors
  979. */
  980. @Override
  981. public void setRightBorderColor(short color) {
  982. XSSFColor clr = XSSFColor.from(CTColor.Factory.newInstance(), _stylesSource.getIndexedColors());
  983. clr.setIndexed(color);
  984. setRightBorderColor(clr);
  985. }
  986. /**
  987. * Set the color to use for the right border as a {@link XSSFColor} value
  988. *
  989. * @param color the color to use
  990. */
  991. public void setRightBorderColor(XSSFColor color) {
  992. CTBorder ct = getCTBorder();
  993. if(color == null && !ct.isSetRight()) return;
  994. CTBorderPr pr = ct.isSetRight() ? ct.getRight() : ct.addNewRight();
  995. if(color != null) pr.setColor(color.getCTColor());
  996. else pr.unsetColor();
  997. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,_stylesSource.getIndexedColors()));
  998. _cellXf.setBorderId(idx);
  999. _cellXf.setApplyBorder(true);
  1000. }
  1001. /**
  1002. * Set the degree of rotation for the text in the cell
  1003. * <p>
  1004. * Expressed in degrees. Values range from 0 to 180. The first letter of
  1005. * the text is considered the center-point of the arc.
  1006. * <br>
  1007. * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
  1008. * horizon is calculated as:
  1009. * <br>
  1010. * <code>[degrees below horizon] = 90 - textRotation.</code>
  1011. * </p>
  1012. *
  1013. * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
  1014. * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
  1015. * accordingly, however the corresponding getter is returning values in the range mandated by the current type
  1016. * of Excel file-format that this CellStyle is applied to.
  1017. *
  1018. * @param rotation - the rotation degrees (between 0 and 180 degrees)
  1019. */
  1020. @Override
  1021. public void setRotation(short rotation) {
  1022. getCellAlignment().setTextRotation(rotation);
  1023. }
  1024. /**
  1025. * Set the color to use for the top border
  1026. *
  1027. * @param color the index of the color definition
  1028. * @see org.apache.poi.ss.usermodel.IndexedColors
  1029. */
  1030. @Override
  1031. public void setTopBorderColor(short color) {
  1032. XSSFColor clr = XSSFColor.from(CTColor.Factory.newInstance(), _stylesSource.getIndexedColors());
  1033. clr.setIndexed(color);
  1034. setTopBorderColor(clr);
  1035. }
  1036. /**
  1037. * Set the color to use for the top border as a {@link XSSFColor} value
  1038. *
  1039. * @param color the color to use
  1040. */
  1041. public void setTopBorderColor(XSSFColor color) {
  1042. CTBorder ct = getCTBorder();
  1043. if(color == null && !ct.isSetTop()) return;
  1044. CTBorderPr pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
  1045. if(color != null) pr.setColor(color.getCTColor());
  1046. else pr.unsetColor();
  1047. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,_stylesSource.getIndexedColors()));
  1048. _cellXf.setBorderId(idx);
  1049. _cellXf.setApplyBorder(true);
  1050. }
  1051. /**
  1052. * Set the type of vertical alignment for the cell
  1053. *
  1054. * @param align - the type of alignment
  1055. */
  1056. public void setVerticalAlignment(VerticalAlignment align) {
  1057. _cellXf.setApplyAlignment(true);
  1058. getCellAlignment().setVertical(align);
  1059. }
  1060. /**
  1061. * Set whether the text should be wrapped.
  1062. * <p>
  1063. * Setting this flag to <code>true</code> make all content visible
  1064. * within a cell by displaying it on multiple lines
  1065. * </p>
  1066. *
  1067. * @param wrapped a boolean value indicating if the text in a cell should be line-wrapped within the cell.
  1068. */
  1069. @Override
  1070. public void setWrapText(boolean wrapped) {
  1071. getCellAlignment().setWrapText(wrapped);
  1072. }
  1073. /**
  1074. * Gets border color
  1075. *
  1076. * @param side the border side
  1077. * @return the used color
  1078. */
  1079. public XSSFColor getBorderColor(BorderSide side) {
  1080. switch(side){
  1081. case BOTTOM:
  1082. return getBottomBorderXSSFColor();
  1083. case RIGHT:
  1084. return getRightBorderXSSFColor();
  1085. case TOP:
  1086. return getTopBorderXSSFColor();
  1087. case LEFT:
  1088. return getLeftBorderXSSFColor();
  1089. default:
  1090. throw new IllegalArgumentException("Unknown border: " + side);
  1091. }
  1092. }
  1093. /**
  1094. * Set the color to use for the selected border
  1095. *
  1096. * @param side - where to apply the color definition
  1097. * @param color - the color to use
  1098. */
  1099. public void setBorderColor(BorderSide side, XSSFColor color) {
  1100. switch(side){
  1101. case BOTTOM:
  1102. setBottomBorderColor(color);
  1103. break;
  1104. case RIGHT:
  1105. setRightBorderColor(color);
  1106. break;
  1107. case TOP:
  1108. setTopBorderColor(color);
  1109. break;
  1110. case LEFT:
  1111. setLeftBorderColor(color);
  1112. break;
  1113. }
  1114. }
  1115. @Override
  1116. public void setShrinkToFit(boolean shrinkToFit) {
  1117. getCellAlignment().setShrinkToFit(shrinkToFit);
  1118. }
  1119. private int getFontId() {
  1120. if (_cellXf.isSetFontId()) {
  1121. return (int) _cellXf.getFontId();
  1122. }
  1123. return (int) _cellStyleXf.getFontId();
  1124. }
  1125. /**
  1126. * get the cellAlignment object to use for manage alignment
  1127. * @return XSSFCellAlignment - cell alignment
  1128. */
  1129. protected XSSFCellAlignment getCellAlignment() {
  1130. if (this._cellAlignment == null) {
  1131. this._cellAlignment = new XSSFCellAlignment(getCTCellAlignment());
  1132. }
  1133. return this._cellAlignment;
  1134. }
  1135. /**
  1136. * Return the CTCellAlignment instance for alignment
  1137. *
  1138. * @return CTCellAlignment
  1139. */
  1140. private CTCellAlignment getCTCellAlignment() {
  1141. if (_cellXf.getAlignment() == null) {
  1142. _cellXf.setAlignment(CTCellAlignment.Factory.newInstance());
  1143. }
  1144. return _cellXf.getAlignment();
  1145. }
  1146. /**
  1147. * Returns a hash code value for the object. The hash is derived from the underlying CTXf bean.
  1148. *
  1149. * @return the hash code value for this style
  1150. */
  1151. @Override
  1152. public int hashCode(){
  1153. return _cellXf.toString().hashCode();
  1154. }
  1155. /**
  1156. * Checks is the supplied style is equal to this style
  1157. *
  1158. * @param o the style to check
  1159. * @return true if the supplied style is equal to this style
  1160. */
  1161. @Override
  1162. public boolean equals(Object o){
  1163. if(!(o instanceof XSSFCellStyle)) return false;
  1164. XSSFCellStyle cf = (XSSFCellStyle)o;
  1165. return _cellXf.toString().equals(cf.getCoreXf().toString());
  1166. }
  1167. /**
  1168. * Make a copy of this style. The underlying CTXf bean is cloned,
  1169. * the references to fills and borders remain.
  1170. *
  1171. * @return a copy of this style
  1172. */
  1173. @Override
  1174. public XSSFCellStyle copy(){
  1175. CTXf xf = (CTXf)_cellXf.copy();
  1176. int xfSize = _stylesSource._getStyleXfsSize();
  1177. int indexXf = _stylesSource.putCellXf(xf);
  1178. return new XSSFCellStyle(indexXf-1, xfSize-1, _stylesSource, _theme);
  1179. }
  1180. }