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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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));
  186. _cellXf.setFillId(idx);
  187. _cellXf.setApplyFill(true);
  188. }
  189. private void addBorder(CTBorder border) {
  190. int idx = _stylesSource.putBorder(new XSSFCellBorder(border, _theme));
  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. * Get the color to use for the right border
  591. *
  592. * @return the index of the color definition, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#BLACK}
  593. * @see org.apache.poi.ss.usermodel.IndexedColors
  594. */
  595. @Override
  596. public short getRightBorderColor() {
  597. XSSFColor clr = getRightBorderXSSFColor();
  598. return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
  599. }
  600. /**
  601. * Get the color to use for the right border
  602. *
  603. * @return the used color or <code>null</code> if not set
  604. */
  605. public XSSFColor getRightBorderXSSFColor() {
  606. if(!_cellXf.getApplyBorder()) return null;
  607. int idx = (int)_cellXf.getBorderId();
  608. XSSFCellBorder border = _stylesSource.getBorderAt(idx);
  609. return border.getBorderColor(BorderSide.RIGHT);
  610. }
  611. /**
  612. * Get the degree of rotation for the text in the cell
  613. * <p>
  614. * Expressed in degrees. Values range from 0 to 180. The first letter of
  615. * the text is considered the center-point of the arc.
  616. * <br/>
  617. * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
  618. * horizon is calculated as:
  619. * <br/>
  620. * <code>[degrees below horizon] = 90 - textRotation.</code>
  621. * </p>
  622. *
  623. * @return rotation degrees (between 0 and 180 degrees)
  624. */
  625. @Override
  626. public short getRotation() {
  627. CTCellAlignment align = _cellXf.getAlignment();
  628. return (short)(align == null ? 0 : align.getTextRotation());
  629. }
  630. @Override
  631. public boolean getShrinkToFit() {
  632. CTCellAlignment align = _cellXf.getAlignment();
  633. return align != null && align.getShrinkToFit();
  634. }
  635. /**
  636. * Get the color to use for the top border
  637. *
  638. * @return the index of the color definition, default value is {@link org.apache.poi.ss.usermodel.IndexedColors#BLACK}
  639. * @see org.apache.poi.ss.usermodel.IndexedColors
  640. */
  641. @Override
  642. public short getTopBorderColor() {
  643. XSSFColor clr = getTopBorderXSSFColor();
  644. return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
  645. }
  646. /**
  647. * Get the color to use for the top border
  648. *
  649. * @return the used color or <code>null</code> if not set
  650. */
  651. public XSSFColor getTopBorderXSSFColor() {
  652. if(!_cellXf.getApplyBorder()) return null;
  653. int idx = (int)_cellXf.getBorderId();
  654. XSSFCellBorder border = _stylesSource.getBorderAt(idx);
  655. return border.getBorderColor(BorderSide.TOP);
  656. }
  657. /**
  658. * Get the type of vertical alignment for the cell
  659. *
  660. * @return align the type of alignment, default value is {@link org.apache.poi.ss.usermodel.CellStyle#VERTICAL_BOTTOM}
  661. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_TOP
  662. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_CENTER
  663. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_BOTTOM
  664. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_JUSTIFY
  665. * @deprecated POI 3.15 beta 3. Use {@link #getVerticalAlignmentEnum()} instead.
  666. */
  667. @Override
  668. public short getVerticalAlignment() {
  669. return getVerticalAlignmentEnum().getCode();
  670. }
  671. /**
  672. * Get the type of vertical alignment for the cell
  673. *
  674. * @return the type of alignment, default value is {@link VerticalAlignment#BOTTOM}
  675. */
  676. @Override
  677. public VerticalAlignment getVerticalAlignmentEnum() {
  678. CTCellAlignment align = _cellXf.getAlignment();
  679. if(align != null && align.isSetVertical()) {
  680. return VerticalAlignment.forInt(align.getVertical().intValue()-1);
  681. }
  682. return VerticalAlignment.BOTTOM;
  683. }
  684. /**
  685. * Whether the text should be wrapped
  686. *
  687. * @return a boolean value indicating if the text in a cell should be line-wrapped within the cell.
  688. */
  689. @Override
  690. public boolean getWrapText() {
  691. CTCellAlignment align = _cellXf.getAlignment();
  692. return align != null && align.getWrapText();
  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.CellStyle#ALIGN_GENERAL
  699. * @see org.apache.poi.ss.usermodel.CellStyle#ALIGN_LEFT
  700. * @see org.apache.poi.ss.usermodel.CellStyle#ALIGN_CENTER
  701. * @see org.apache.poi.ss.usermodel.CellStyle#ALIGN_RIGHT
  702. * @see org.apache.poi.ss.usermodel.CellStyle#ALIGN_FILL
  703. * @see org.apache.poi.ss.usermodel.CellStyle#ALIGN_JUSTIFY
  704. * @see org.apache.poi.ss.usermodel.CellStyle#ALIGN_CENTER_SELECTION
  705. * @deprecated POI 3.15 beta 3. Use {@link #setAlignment(HorizontalAlignment)} instead.
  706. */
  707. @Removal(version="3.17")
  708. @Override
  709. public void setAlignment(short align) {
  710. setAlignment(HorizontalAlignment.forInt(align));
  711. }
  712. /**
  713. * Set the type of horizontal alignment for the cell
  714. *
  715. * @param align - the type of alignment
  716. */
  717. @Override
  718. public void setAlignment(HorizontalAlignment align) {
  719. getCellAlignment().setHorizontal(align);
  720. }
  721. /**
  722. * Set the type of border to use for the bottom border of the cell
  723. *
  724. * @param border the type of border to use
  725. * @deprecated 3.15 beta 2. Use {@link #setBorderBottom(BorderStyle)}
  726. */
  727. @Removal(version="3.17")
  728. @Override
  729. public void setBorderBottom(short border) {
  730. setBorderBottom(BorderStyle.valueOf(border));
  731. }
  732. /**
  733. * Set the type of border to use for the bottom border of the cell
  734. *
  735. * @param border - type of border to use
  736. * @see org.apache.poi.ss.usermodel.BorderStyle
  737. * @since POI 3.15
  738. */
  739. @Override
  740. public void setBorderBottom(BorderStyle border) {
  741. CTBorder ct = getCTBorder();
  742. CTBorderPr pr = ct.isSetBottom() ? ct.getBottom() : ct.addNewBottom();
  743. if(border == BorderStyle.NONE) ct.unsetBottom();
  744. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  745. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  746. _cellXf.setBorderId(idx);
  747. _cellXf.setApplyBorder(true);
  748. }
  749. /**
  750. * Set the type of border to use for the left border of the cell
  751. * @param border the type of border to use
  752. * @deprecated 3.15 beta 2. Use {@link #setBorderLeft(BorderStyle)}
  753. */
  754. @Removal(version="3.17")
  755. @Override
  756. public void setBorderLeft(short border) {
  757. setBorderLeft(BorderStyle.valueOf(border));
  758. }
  759. /**
  760. * Set the type of border to use for the left border of the cell
  761. *
  762. * @param border the type of border to use
  763. * @since POI 3.15
  764. */
  765. @Override
  766. public void setBorderLeft(BorderStyle border) {
  767. CTBorder ct = getCTBorder();
  768. CTBorderPr pr = ct.isSetLeft() ? ct.getLeft() : ct.addNewLeft();
  769. if(border == BorderStyle.NONE) ct.unsetLeft();
  770. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  771. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  772. _cellXf.setBorderId(idx);
  773. _cellXf.setApplyBorder(true);
  774. }
  775. /**
  776. * Set the type of border to use for the right border of the cell
  777. *
  778. * @param border the type of border to use
  779. * @deprecated 3.15 beta 2. Use {@link #setBorderRight(BorderStyle)}
  780. */
  781. @Removal(version="3.17")
  782. @Override
  783. public void setBorderRight(short border) {
  784. setBorderRight(BorderStyle.valueOf(border));
  785. }
  786. /**
  787. * Set the type of border to use for the right border of the cell
  788. *
  789. * @param border the type of border to use
  790. * @since POI 3.15
  791. */
  792. @Override
  793. public void setBorderRight(BorderStyle border) {
  794. CTBorder ct = getCTBorder();
  795. CTBorderPr pr = ct.isSetRight() ? ct.getRight() : ct.addNewRight();
  796. if(border == BorderStyle.NONE) ct.unsetRight();
  797. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  798. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  799. _cellXf.setBorderId(idx);
  800. _cellXf.setApplyBorder(true);
  801. }
  802. /**
  803. * Set the type of border to use for the top border of the cell
  804. *
  805. * @param border the type of border to use
  806. * @deprecated 3.15 beta 2. Use {@link #setBorderTop(BorderStyle)}
  807. */
  808. @Removal(version="3.17")
  809. @Override
  810. public void setBorderTop(short border) {
  811. setBorderTop(BorderStyle.valueOf(border));
  812. }
  813. /**
  814. * Set the type of border to use for the top border of the cell
  815. *
  816. * @param border the type of border to use
  817. * @since POI 3.15
  818. */
  819. @Override
  820. public void setBorderTop(BorderStyle border) {
  821. CTBorder ct = getCTBorder();
  822. CTBorderPr pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
  823. if(border == BorderStyle.NONE) ct.unsetTop();
  824. else pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
  825. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  826. _cellXf.setBorderId(idx);
  827. _cellXf.setApplyBorder(true);
  828. }
  829. /**
  830. * Set the color to use for the bottom border
  831. * @param color the index of the color definition
  832. * @see org.apache.poi.ss.usermodel.IndexedColors
  833. */
  834. @Override
  835. public void setBottomBorderColor(short color) {
  836. XSSFColor clr = new XSSFColor();
  837. clr.setIndexed(color);
  838. setBottomBorderColor(clr);
  839. }
  840. /**
  841. * Set the color to use for the bottom border
  842. *
  843. * @param color the color to use, null means no color
  844. */
  845. public void setBottomBorderColor(XSSFColor color) {
  846. CTBorder ct = getCTBorder();
  847. if(color == null && !ct.isSetBottom()) return;
  848. CTBorderPr pr = ct.isSetBottom() ? ct.getBottom() : ct.addNewBottom();
  849. if(color != null) pr.setColor(color.getCTColor());
  850. else pr.unsetColor();
  851. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  852. _cellXf.setBorderId(idx);
  853. _cellXf.setApplyBorder(true);
  854. }
  855. /**
  856. * Set the index of a data format
  857. *
  858. * @param fmt the index of a data format
  859. */
  860. @Override
  861. public void setDataFormat(short fmt) {
  862. // XSSF supports >32,767 formats
  863. setDataFormat(fmt&0xffff);
  864. }
  865. /**
  866. * Set the index of a data format
  867. *
  868. * @param fmt the index of a data format
  869. */
  870. public void setDataFormat(int fmt) {
  871. _cellXf.setApplyNumberFormat(true);
  872. _cellXf.setNumFmtId(fmt);
  873. }
  874. /**
  875. * Set the background fill color represented as a {@link XSSFColor} value.
  876. * <p>
  877. * For example:
  878. * <pre>
  879. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  880. * cs.setFillBackgroundXSSFColor(new XSSFColor(java.awt.Color.RED));
  881. * </pre>
  882. * optionally a Foreground and background fill can be applied:
  883. * <i>Note: Ensure Foreground color is set prior to background</i>
  884. * <pre>
  885. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  886. * cs.setFillForegroundColor(new XSSFColor(java.awt.Color.BLUE));
  887. * cs.setFillBackgroundColor(new XSSFColor(java.awt.Color.GREEN));
  888. * </pre>
  889. * or, for the special case of SOLID_FILL:
  890. * <pre>
  891. * cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND );
  892. * cs.setFillForegroundColor(new XSSFColor(java.awt.Color.GREEN));
  893. * </pre>
  894. * It is necessary to set the fill style in order
  895. * for the color to be shown in the cell.
  896. *
  897. * @param color - the color to use
  898. */
  899. public void setFillBackgroundColor(XSSFColor color) {
  900. CTFill ct = getCTFill();
  901. CTPatternFill ptrn = ct.getPatternFill();
  902. if(color == null) {
  903. if(ptrn != null && ptrn.isSetBgColor()) ptrn.unsetBgColor();
  904. } else {
  905. if(ptrn == null) ptrn = ct.addNewPatternFill();
  906. ptrn.setBgColor(color.getCTColor());
  907. }
  908. addFill(ct);
  909. }
  910. /**
  911. * Set the background fill color represented as a indexed color value.
  912. * <p>
  913. * For example:
  914. * <pre>
  915. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  916. * cs.setFillBackgroundXSSFColor(IndexedColors.RED.getIndex());
  917. * </pre>
  918. * optionally a Foreground and background fill can be applied:
  919. * <i>Note: Ensure Foreground color is set prior to background</i>
  920. * <pre>
  921. * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
  922. * cs.setFillForegroundColor(IndexedColors.BLUE.getIndex());
  923. * cs.setFillBackgroundColor(IndexedColors.RED.getIndex());
  924. * </pre>
  925. * or, for the special case of SOLID_FILL:
  926. * <pre>
  927. * cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND );
  928. * cs.setFillForegroundColor(IndexedColors.RED.getIndex());
  929. * </pre>
  930. * It is necessary to set the fill style in order
  931. * for the color to be shown in the cell.
  932. *
  933. * @param bg - the color to use
  934. * @see org.apache.poi.ss.usermodel.IndexedColors
  935. */
  936. @Override
  937. public void setFillBackgroundColor(short bg) {
  938. XSSFColor clr = new XSSFColor();
  939. clr.setIndexed(bg);
  940. setFillBackgroundColor(clr);
  941. }
  942. /**
  943. * Set the foreground fill color represented as a {@link XSSFColor} value.
  944. * <br/>
  945. * <i>Note: Ensure Foreground color is set prior to background color.</i>
  946. * @param color the color to use
  947. * @see #setFillBackgroundColor(org.apache.poi.xssf.usermodel.XSSFColor) )
  948. */
  949. public void setFillForegroundColor(XSSFColor color) {
  950. CTFill ct = getCTFill();
  951. CTPatternFill ptrn = ct.getPatternFill();
  952. if(color == null) {
  953. if(ptrn != null && ptrn.isSetFgColor()) ptrn.unsetFgColor();
  954. } else {
  955. if(ptrn == null) ptrn = ct.addNewPatternFill();
  956. ptrn.setFgColor(color.getCTColor());
  957. }
  958. addFill(ct);
  959. }
  960. /**
  961. * Set the foreground fill color as a indexed color value
  962. * <br/>
  963. * <i>Note: Ensure Foreground color is set prior to background color.</i>
  964. * @param fg the color to use
  965. * @see org.apache.poi.ss.usermodel.IndexedColors
  966. */
  967. @Override
  968. public void setFillForegroundColor(short fg) {
  969. XSSFColor clr = new XSSFColor();
  970. clr.setIndexed(fg);
  971. setFillForegroundColor(clr);
  972. }
  973. /**
  974. * Get a <b>copy</b> of the currently used CTFill, if none is used, return a new instance.
  975. */
  976. private CTFill getCTFill(){
  977. CTFill ct;
  978. // bug 56295: handle missing applyFill attribute as "true" because Excel does as well
  979. if(!_cellXf.isSetApplyFill() || _cellXf.getApplyFill()) {
  980. int fillIndex = (int)_cellXf.getFillId();
  981. XSSFCellFill cf = _stylesSource.getFillAt(fillIndex);
  982. ct = (CTFill)cf.getCTFill().copy();
  983. } else {
  984. ct = CTFill.Factory.newInstance();
  985. }
  986. return ct;
  987. }
  988. /**
  989. * Get a <b>copy</b> of the currently used CTBorder, if none is used, return a new instance.
  990. */
  991. private CTBorder getCTBorder(){
  992. CTBorder ct;
  993. if(_cellXf.getApplyBorder()) {
  994. int idx = (int)_cellXf.getBorderId();
  995. XSSFCellBorder cf = _stylesSource.getBorderAt(idx);
  996. ct = (CTBorder)cf.getCTBorder().copy();
  997. } else {
  998. ct = CTBorder.Factory.newInstance();
  999. }
  1000. return ct;
  1001. }
  1002. /**
  1003. * This element is used to specify cell fill information for pattern and solid color cell fills.
  1004. * For solid cell fills (no pattern), foreground color is used.
  1005. * For cell fills with patterns specified, then the cell fill color is specified by the background color.
  1006. *
  1007. * @see org.apache.poi.ss.usermodel.CellStyle#NO_FILL
  1008. * @see org.apache.poi.ss.usermodel.CellStyle#SOLID_FOREGROUND
  1009. * @see org.apache.poi.ss.usermodel.CellStyle#FINE_DOTS
  1010. * @see org.apache.poi.ss.usermodel.CellStyle#ALT_BARS
  1011. * @see org.apache.poi.ss.usermodel.CellStyle#SPARSE_DOTS
  1012. * @see org.apache.poi.ss.usermodel.CellStyle#THICK_HORZ_BANDS
  1013. * @see org.apache.poi.ss.usermodel.CellStyle#THICK_VERT_BANDS
  1014. * @see org.apache.poi.ss.usermodel.CellStyle#THICK_BACKWARD_DIAG
  1015. * @see org.apache.poi.ss.usermodel.CellStyle#THICK_FORWARD_DIAG
  1016. * @see org.apache.poi.ss.usermodel.CellStyle#BIG_SPOTS
  1017. * @see org.apache.poi.ss.usermodel.CellStyle#BRICKS
  1018. * @see org.apache.poi.ss.usermodel.CellStyle#THIN_HORZ_BANDS
  1019. * @see org.apache.poi.ss.usermodel.CellStyle#THIN_VERT_BANDS
  1020. * @see org.apache.poi.ss.usermodel.CellStyle#THIN_BACKWARD_DIAG
  1021. * @see org.apache.poi.ss.usermodel.CellStyle#THIN_FORWARD_DIAG
  1022. * @see org.apache.poi.ss.usermodel.CellStyle#SQUARES
  1023. * @see org.apache.poi.ss.usermodel.CellStyle#DIAMONDS
  1024. * @see #setFillBackgroundColor(short)
  1025. * @see #setFillForegroundColor(short)
  1026. * @param fp fill pattern (set to {@link org.apache.poi.ss.usermodel.CellStyle#SOLID_FOREGROUND} to fill w/foreground color)
  1027. * @deprecated POI 3.15. Use {@link #setFillPattern(FillPatternType)} instead.
  1028. */
  1029. @Removal(version="3.17")
  1030. @Override
  1031. public void setFillPattern(short fp) {
  1032. setFillPattern(FillPatternType.forInt(fp));
  1033. }
  1034. /**
  1035. * This element is used to specify cell fill information for pattern and solid color cell fills. For solid cell fills (no pattern),
  1036. * foreground color is used is used. For cell fills with patterns specified, then the cell fill color is specified by the background color element.
  1037. *
  1038. * @param pattern the fill pattern to use
  1039. * @see #setFillBackgroundColor(XSSFColor)
  1040. * @see #setFillForegroundColor(XSSFColor)
  1041. * @see org.apache.poi.ss.usermodel.FillPatternType
  1042. */
  1043. @Override
  1044. public void setFillPattern(FillPatternType pattern) {
  1045. CTFill ct = getCTFill();
  1046. CTPatternFill ctptrn = ct.isSetPatternFill() ? ct.getPatternFill() : ct.addNewPatternFill();
  1047. if (pattern == FillPatternType.NO_FILL && ctptrn.isSetPatternType()) {
  1048. ctptrn.unsetPatternType();
  1049. } else {
  1050. ctptrn.setPatternType(STPatternType.Enum.forInt(pattern.getCode() + 1));
  1051. }
  1052. addFill(ct);
  1053. }
  1054. /**
  1055. * Set the font for this style
  1056. *
  1057. * @param font a font object created or retrieved from the XSSFWorkbook object
  1058. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#createFont()
  1059. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(short)
  1060. */
  1061. @Override
  1062. public void setFont(Font font) {
  1063. if(font != null){
  1064. long index = font.getIndex();
  1065. this._cellXf.setFontId(index);
  1066. this._cellXf.setApplyFont(true);
  1067. } else {
  1068. this._cellXf.setApplyFont(false);
  1069. }
  1070. }
  1071. /**
  1072. * Set the cell's using this style to be hidden
  1073. *
  1074. * @param hidden - whether the cell using this style should be hidden
  1075. */
  1076. @Override
  1077. public void setHidden(boolean hidden) {
  1078. if (!_cellXf.isSetProtection()) {
  1079. _cellXf.addNewProtection();
  1080. }
  1081. _cellXf.getProtection().setHidden(hidden);
  1082. }
  1083. /**
  1084. * Set the number of spaces to indent the text in the cell
  1085. *
  1086. * @param indent - number of spaces
  1087. */
  1088. @Override
  1089. public void setIndention(short indent) {
  1090. getCellAlignment().setIndent(indent);
  1091. }
  1092. /**
  1093. * Set the color to use for the left border as a indexed color value
  1094. *
  1095. * @param color the index of the color definition
  1096. * @see org.apache.poi.ss.usermodel.IndexedColors
  1097. */
  1098. @Override
  1099. public void setLeftBorderColor(short color) {
  1100. XSSFColor clr = new XSSFColor();
  1101. clr.setIndexed(color);
  1102. setLeftBorderColor(clr);
  1103. }
  1104. /**
  1105. * Set the color to use for the left border as a {@link XSSFColor} value
  1106. *
  1107. * @param color the color to use
  1108. */
  1109. public void setLeftBorderColor(XSSFColor color) {
  1110. CTBorder ct = getCTBorder();
  1111. if(color == null && !ct.isSetLeft()) return;
  1112. CTBorderPr pr = ct.isSetLeft() ? ct.getLeft() : ct.addNewLeft();
  1113. if(color != null) pr.setColor(color.getCTColor());
  1114. else pr.unsetColor();
  1115. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  1116. _cellXf.setBorderId(idx);
  1117. _cellXf.setApplyBorder(true);
  1118. }
  1119. /**
  1120. * Set the cell's using this style to be locked
  1121. *
  1122. * @param locked - whether the cell using this style should be locked
  1123. */
  1124. @Override
  1125. public void setLocked(boolean locked) {
  1126. if (!_cellXf.isSetProtection()) {
  1127. _cellXf.addNewProtection();
  1128. }
  1129. _cellXf.getProtection().setLocked(locked);
  1130. }
  1131. /**
  1132. * Set the color to use for the right border
  1133. *
  1134. * @param color the index of the color definition
  1135. * @see org.apache.poi.ss.usermodel.IndexedColors
  1136. */
  1137. @Override
  1138. public void setRightBorderColor(short color) {
  1139. XSSFColor clr = new XSSFColor();
  1140. clr.setIndexed(color);
  1141. setRightBorderColor(clr);
  1142. }
  1143. /**
  1144. * Set the color to use for the right border as a {@link XSSFColor} value
  1145. *
  1146. * @param color the color to use
  1147. */
  1148. public void setRightBorderColor(XSSFColor color) {
  1149. CTBorder ct = getCTBorder();
  1150. if(color == null && !ct.isSetRight()) return;
  1151. CTBorderPr pr = ct.isSetRight() ? ct.getRight() : ct.addNewRight();
  1152. if(color != null) pr.setColor(color.getCTColor());
  1153. else pr.unsetColor();
  1154. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  1155. _cellXf.setBorderId(idx);
  1156. _cellXf.setApplyBorder(true);
  1157. }
  1158. /**
  1159. * Set the degree of rotation for the text in the cell
  1160. * <p>
  1161. * Expressed in degrees. Values range from 0 to 180. The first letter of
  1162. * the text is considered the center-point of the arc.
  1163. * <br/>
  1164. * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
  1165. * horizon is calculated as:
  1166. * <br/>
  1167. * <code>[degrees below horizon] = 90 - textRotation.</code>
  1168. * </p>
  1169. *
  1170. * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
  1171. * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
  1172. * accordingly, however the corresponding getter is returning values in the range mandated by the current type
  1173. * of Excel file-format that this CellStyle is applied to.
  1174. *
  1175. * @param rotation - the rotation degrees (between 0 and 180 degrees)
  1176. */
  1177. @Override
  1178. public void setRotation(short rotation) {
  1179. getCellAlignment().setTextRotation(rotation);
  1180. }
  1181. /**
  1182. * Set the color to use for the top border
  1183. *
  1184. * @param color the index of the color definition
  1185. * @see org.apache.poi.ss.usermodel.IndexedColors
  1186. */
  1187. @Override
  1188. public void setTopBorderColor(short color) {
  1189. XSSFColor clr = new XSSFColor();
  1190. clr.setIndexed(color);
  1191. setTopBorderColor(clr);
  1192. }
  1193. /**
  1194. * Set the color to use for the top border as a {@link XSSFColor} value
  1195. *
  1196. * @param color the color to use
  1197. */
  1198. public void setTopBorderColor(XSSFColor color) {
  1199. CTBorder ct = getCTBorder();
  1200. if(color == null && !ct.isSetTop()) return;
  1201. CTBorderPr pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
  1202. if(color != null) pr.setColor(color.getCTColor());
  1203. else pr.unsetColor();
  1204. int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
  1205. _cellXf.setBorderId(idx);
  1206. _cellXf.setApplyBorder(true);
  1207. }
  1208. /**
  1209. * Set the type of vertical alignment for the cell
  1210. *
  1211. * @param align - align the type of alignment
  1212. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_TOP
  1213. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_CENTER
  1214. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_BOTTOM
  1215. * @see org.apache.poi.ss.usermodel.CellStyle#VERTICAL_JUSTIFY
  1216. * @see org.apache.poi.ss.usermodel.VerticalAlignment
  1217. * @deprecated POI 3.15 beta 3. Use {@link #setVerticalAlignment(VerticalAlignment)} instead.
  1218. */
  1219. @Removal(version="3.17")
  1220. @Override
  1221. public void setVerticalAlignment(short align) {
  1222. setVerticalAlignment(VerticalAlignment.forInt(align));
  1223. }
  1224. /**
  1225. * Set the type of vertical alignment for the cell
  1226. *
  1227. * @param align - the type of alignment
  1228. */
  1229. public void setVerticalAlignment(VerticalAlignment align) {
  1230. getCellAlignment().setVertical(align);
  1231. }
  1232. /**
  1233. * Set whether the text should be wrapped.
  1234. * <p>
  1235. * Setting this flag to <code>true</code> make all content visible
  1236. * whithin a cell by displaying it on multiple lines
  1237. * </p>
  1238. *
  1239. * @param wrapped a boolean value indicating if the text in a cell should be line-wrapped within the cell.
  1240. */
  1241. @Override
  1242. public void setWrapText(boolean wrapped) {
  1243. getCellAlignment().setWrapText(wrapped);
  1244. }
  1245. /**
  1246. * Gets border color
  1247. *
  1248. * @param side the border side
  1249. * @return the used color
  1250. */
  1251. public XSSFColor getBorderColor(BorderSide side) {
  1252. switch(side){
  1253. case BOTTOM:
  1254. return getBottomBorderXSSFColor();
  1255. case RIGHT:
  1256. return getRightBorderXSSFColor();
  1257. case TOP:
  1258. return getTopBorderXSSFColor();
  1259. case LEFT:
  1260. return getLeftBorderXSSFColor();
  1261. default:
  1262. throw new IllegalArgumentException("Unknown border: " + side);
  1263. }
  1264. }
  1265. /**
  1266. * Set the color to use for the selected border
  1267. *
  1268. * @param side - where to apply the color definition
  1269. * @param color - the color to use
  1270. */
  1271. public void setBorderColor(BorderSide side, XSSFColor color) {
  1272. switch(side){
  1273. case BOTTOM:
  1274. setBottomBorderColor(color);
  1275. break;
  1276. case RIGHT:
  1277. setRightBorderColor(color);
  1278. break;
  1279. case TOP:
  1280. setTopBorderColor(color);
  1281. break;
  1282. case LEFT:
  1283. setLeftBorderColor(color);
  1284. break;
  1285. }
  1286. }
  1287. @Override
  1288. public void setShrinkToFit(boolean shrinkToFit) {
  1289. getCellAlignment().setShrinkToFit(shrinkToFit);
  1290. }
  1291. private int getFontId() {
  1292. if (_cellXf.isSetFontId()) {
  1293. return (int) _cellXf.getFontId();
  1294. }
  1295. return (int) _cellStyleXf.getFontId();
  1296. }
  1297. /**
  1298. * get the cellAlignment object to use for manage alignment
  1299. * @return XSSFCellAlignment - cell alignment
  1300. */
  1301. protected XSSFCellAlignment getCellAlignment() {
  1302. if (this._cellAlignment == null) {
  1303. this._cellAlignment = new XSSFCellAlignment(getCTCellAlignment());
  1304. }
  1305. return this._cellAlignment;
  1306. }
  1307. /**
  1308. * Return the CTCellAlignment instance for alignment
  1309. *
  1310. * @return CTCellAlignment
  1311. */
  1312. private CTCellAlignment getCTCellAlignment() {
  1313. if (_cellXf.getAlignment() == null) {
  1314. _cellXf.setAlignment(CTCellAlignment.Factory.newInstance());
  1315. }
  1316. return _cellXf.getAlignment();
  1317. }
  1318. /**
  1319. * Returns a hash code value for the object. The hash is derived from the underlying CTXf bean.
  1320. *
  1321. * @return the hash code value for this style
  1322. */
  1323. @Override
  1324. public int hashCode(){
  1325. return _cellXf.toString().hashCode();
  1326. }
  1327. /**
  1328. * Checks is the supplied style is equal to this style
  1329. *
  1330. * @param o the style to check
  1331. * @return true if the supplied style is equal to this style
  1332. */
  1333. @Override
  1334. public boolean equals(Object o){
  1335. if(o == null || !(o instanceof XSSFCellStyle)) return false;
  1336. XSSFCellStyle cf = (XSSFCellStyle)o;
  1337. return _cellXf.toString().equals(cf.getCoreXf().toString());
  1338. }
  1339. /**
  1340. * Make a copy of this style. The underlying CTXf bean is cloned,
  1341. * the references to fills and borders remain.
  1342. *
  1343. * @return a copy of this style
  1344. */
  1345. @Override
  1346. public Object clone(){
  1347. CTXf xf = (CTXf)_cellXf.copy();
  1348. int xfSize = _stylesSource._getStyleXfsSize();
  1349. int indexXf = _stylesSource.putCellXf(xf);
  1350. return new XSSFCellStyle(indexXf-1, xfSize-1, _stylesSource, _theme);
  1351. }
  1352. }