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

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