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

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