Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XSSFCellStyle.java 52KB

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