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

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