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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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.ss.util;
  16. import java.awt.font.FontRenderContext;
  17. import java.awt.font.TextAttribute;
  18. import java.awt.font.TextLayout;
  19. import java.awt.geom.AffineTransform;
  20. import java.awt.geom.Rectangle2D;
  21. import java.text.AttributedString;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import java.util.Map;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellStyle;
  27. import org.apache.poi.ss.usermodel.CellType;
  28. import org.apache.poi.ss.usermodel.CellValue;
  29. import org.apache.poi.ss.usermodel.DataFormatter;
  30. import org.apache.poi.ss.usermodel.Font;
  31. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  32. import org.apache.poi.ss.usermodel.RichTextString;
  33. import org.apache.poi.ss.usermodel.Row;
  34. import org.apache.poi.ss.usermodel.Sheet;
  35. import org.apache.poi.ss.usermodel.Workbook;
  36. import org.apache.poi.util.Internal;
  37. /**
  38. * Helper methods for when working with Usermodel sheets
  39. *
  40. * @author Yegor Kozlov
  41. */
  42. public class SheetUtil {
  43. /**
  44. * Excel measures columns in units of 1/256th of a character width
  45. * but the docs say nothing about what particular character is used.
  46. * '0' looks to be a good choice.
  47. */
  48. private static final char defaultChar = '0';
  49. /**
  50. * This is the multiple that the font height is scaled by when determining the
  51. * boundary of rotated text.
  52. */
  53. private static final double fontHeightMultiple = 2.0;
  54. /**
  55. * Dummy formula evaluator that does nothing.
  56. * YK: The only reason of having this class is that
  57. * {@link org.apache.poi.ss.usermodel.DataFormatter#formatCellValue(org.apache.poi.ss.usermodel.Cell)}
  58. * returns formula string for formula cells. Dummy evaluator makes it to format the cached formula result.
  59. *
  60. * See Bugzilla #50021
  61. */
  62. private static final FormulaEvaluator dummyEvaluator = new FormulaEvaluator() {
  63. @Override
  64. public void clearAllCachedResultValues(){}
  65. @Override
  66. public void notifySetFormula(Cell cell) {}
  67. @Override
  68. public void notifyDeleteCell(Cell cell) {}
  69. @Override
  70. public void notifyUpdateCell(Cell cell) {}
  71. @Override
  72. public CellValue evaluate(Cell cell) {return null; }
  73. @Override
  74. public Cell evaluateInCell(Cell cell) { return null; }
  75. @Override
  76. public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> workbooks) {}
  77. @Override
  78. public void setDebugEvaluationOutputForNextEval(boolean value) {}
  79. @Override
  80. public void setIgnoreMissingWorkbooks(boolean ignore) {}
  81. @Override
  82. public void evaluateAll() {}
  83. @Override
  84. public CellType evaluateFormulaCell(Cell cell) { return cell.getCachedFormulaResultType(); }
  85. };
  86. /**
  87. * drawing context to measure text
  88. */
  89. private static final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
  90. /**
  91. * Compute width of a single cell
  92. *
  93. * @param cell the cell whose width is to be calculated
  94. * @param defaultCharWidth the width of a single character
  95. * @param formatter formatter used to prepare the text to be measured
  96. * @param useMergedCells whether to use merged cells
  97. * @return the width in pixels or -1 if cell is empty
  98. */
  99. public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
  100. List<CellRangeAddress> mergedRegions = cell.getSheet().getMergedRegions();
  101. return getCellWidth(cell, defaultCharWidth, formatter, useMergedCells, mergedRegions);
  102. }
  103. /**
  104. * Compute width of a single cell
  105. *
  106. * This method receives the list of merged regions as querying it from the cell/sheet
  107. * is time-consuming and thus caching the list across cells speeds up certain operations
  108. * considerably.
  109. *
  110. * @param cell the cell whose width is to be calculated
  111. * @param defaultCharWidth the width of a single character
  112. * @param formatter formatter used to prepare the text to be measured
  113. * @param useMergedCells whether to use merged cells
  114. * @param mergedRegions The list of merged regions as received via cell.getSheet().getMergedRegions()
  115. * @return the width in pixels or -1 if cell is empty
  116. */
  117. public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells,
  118. List<CellRangeAddress> mergedRegions) {
  119. Sheet sheet = cell.getSheet();
  120. Workbook wb = sheet.getWorkbook();
  121. Row row = cell.getRow();
  122. int column = cell.getColumnIndex();
  123. // FIXME: this looks very similar to getCellWithMerges below. Consider consolidating.
  124. // We should only be checking merged regions if useMergedCells is true. Why are we doing this for-loop?
  125. int colspan = 1;
  126. for (CellRangeAddress region : mergedRegions) {
  127. if (region.isInRange(row.getRowNum(), column)) {
  128. if (!useMergedCells) {
  129. // If we're not using merged cells, skip this one and move on to the next.
  130. return -1;
  131. }
  132. cell = row.getCell(region.getFirstColumn());
  133. colspan = 1 + region.getLastColumn() - region.getFirstColumn();
  134. }
  135. }
  136. CellStyle style = cell.getCellStyle();
  137. CellType cellType = cell.getCellType();
  138. // for formula cells we compute the cell width for the cached formula result
  139. if (cellType == CellType.FORMULA)
  140. cellType = cell.getCachedFormulaResultType();
  141. Font font = wb.getFontAt(style.getFontIndex());
  142. double width = -1;
  143. if (cellType == CellType.STRING) {
  144. RichTextString rt = cell.getRichStringCellValue();
  145. if (rt != null && rt.getString() != null) {
  146. String[] lines = rt.getString().split("\\n");
  147. for (String line : lines) {
  148. String txt = line + defaultChar;
  149. AttributedString str = new AttributedString(txt);
  150. copyAttributes(font, str, 0, txt.length());
  151. /*if (rt.numFormattingRuns() > 0) {
  152. // TODO: support rich text fragments
  153. }*/
  154. width = getCellWidth(defaultCharWidth, colspan, style, width, str);
  155. }
  156. }
  157. } else {
  158. String sval = null;
  159. if (cellType == CellType.NUMERIC) {
  160. // Try to get it formatted to look the same as excel
  161. try {
  162. sval = formatter.formatCellValue(cell, dummyEvaluator);
  163. } catch (Exception e) {
  164. sval = String.valueOf(cell.getNumericCellValue());
  165. }
  166. } else if (cellType == CellType.BOOLEAN) {
  167. sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(Locale.ROOT);
  168. }
  169. if(sval != null) {
  170. String txt = sval + defaultChar;
  171. AttributedString str = new AttributedString(txt);
  172. copyAttributes(font, str, 0, txt.length());
  173. width = getCellWidth(defaultCharWidth, colspan, style, width, str);
  174. }
  175. }
  176. return width;
  177. }
  178. /**
  179. * Calculate the best-fit width for a cell
  180. * If a merged cell spans multiple columns, evenly distribute the column width among those columns
  181. *
  182. * @param defaultCharWidth the width of a character using the default font in a workbook
  183. * @param colspan the number of columns that is spanned by the cell (1 if the cell is not part of a merged region)
  184. * @param style the cell style, which contains text rotation and indention information needed to compute the cell width
  185. * @param minWidth the minimum best-fit width. This algorithm will only return values greater than or equal to the minimum width.
  186. * @param str the text contained in the cell
  187. * @return the best fit cell width
  188. */
  189. private static double getCellWidth(int defaultCharWidth, int colspan,
  190. CellStyle style, double minWidth, AttributedString str) {
  191. TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
  192. final Rectangle2D bounds;
  193. if(style.getRotation() != 0){
  194. /*
  195. * Transform the text using a scale so that it's height is increased by a multiple of the leading,
  196. * and then rotate the text before computing the bounds. The scale results in some whitespace around
  197. * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
  198. * is added by the standard Excel autosize.
  199. */
  200. AffineTransform trans = new AffineTransform();
  201. trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
  202. trans.concatenate(
  203. AffineTransform.getScaleInstance(1, fontHeightMultiple)
  204. );
  205. bounds = layout.getOutline(trans).getBounds();
  206. } else {
  207. bounds = layout.getBounds();
  208. }
  209. // frameWidth accounts for leading spaces which is excluded from bounds.getWidth()
  210. final double frameWidth = bounds.getX() + bounds.getWidth();
  211. return Math.max(minWidth, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention());
  212. }
  213. /**
  214. * Compute width of a column and return the result
  215. *
  216. * @param sheet the sheet to calculate
  217. * @param column 0-based index of the column
  218. * @param useMergedCells whether to use merged cells
  219. * @return the width in pixels or -1 if all cells are empty
  220. */
  221. public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells) {
  222. return getColumnWidth(sheet, column, useMergedCells, sheet.getFirstRowNum(), sheet.getLastRowNum());
  223. }
  224. /**
  225. * Compute width of a column based on a subset of the rows and return the result
  226. *
  227. * @param sheet the sheet to calculate
  228. * @param column 0-based index of the column
  229. * @param useMergedCells whether to use merged cells
  230. * @param firstRow 0-based index of the first row to consider (inclusive)
  231. * @param lastRow 0-based index of the last row to consider (inclusive)
  232. * @return the width in pixels or -1 if cell is empty
  233. */
  234. public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){
  235. DataFormatter formatter = new DataFormatter();
  236. int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());
  237. List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
  238. double width = -1;
  239. for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) {
  240. Row row = sheet.getRow(rowIdx);
  241. if( row != null ) {
  242. double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells, mergedRegions);
  243. width = Math.max(width, cellWidth);
  244. }
  245. }
  246. return width;
  247. }
  248. /**
  249. * Get default character width using the Workbook's default font
  250. *
  251. * @param wb the workbook to get the default character width from
  252. * @return default character width in pixels
  253. */
  254. @Internal
  255. public static int getDefaultCharWidth(final Workbook wb) {
  256. Font defaultFont = wb.getFontAt( 0);
  257. AttributedString str = new AttributedString(String.valueOf(defaultChar));
  258. copyAttributes(defaultFont, str, 0, 1);
  259. TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
  260. return (int) layout.getAdvance();
  261. }
  262. /**
  263. * Compute width of a single cell in a row
  264. * Convenience method for {@link #getCellWidth}
  265. *
  266. * @param row the row that contains the cell of interest
  267. * @param column the column number of the cell whose width is to be calculated
  268. * @param defaultCharWidth the width of a single character
  269. * @param formatter formatter used to prepare the text to be measured
  270. * @param useMergedCells whether to use merged cells
  271. * @return the width in pixels or -1 if cell is empty
  272. */
  273. private static double getColumnWidthForRow(
  274. Row row, int column, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells,
  275. List<CellRangeAddress> mergedRegions) {
  276. if( row == null ) {
  277. return -1;
  278. }
  279. Cell cell = row.getCell(column);
  280. if (cell == null) {
  281. return -1;
  282. }
  283. return getCellWidth(cell, defaultCharWidth, formatter, useMergedCells, mergedRegions);
  284. }
  285. /**
  286. * Check if the Fonts are installed correctly so that Java can compute the size of
  287. * columns.
  288. *
  289. * If a Cell uses a Font which is not available on the operating system then Java may
  290. * fail to return useful Font metrics and thus lead to an auto-computed size of 0.
  291. *
  292. * This method allows to check if computing the sizes for a given Font will succeed or not.
  293. *
  294. * @param font The Font that is used in the Cell
  295. * @return true if computing the size for this Font will succeed, false otherwise
  296. */
  297. public static boolean canComputeColumnWidth(Font font) {
  298. // not sure what is the best value sample-here, only "1" did not work on some platforms...
  299. AttributedString str = new AttributedString("1w");
  300. copyAttributes(font, str, 0, "1w".length());
  301. TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
  302. return (layout.getBounds().getWidth() > 0);
  303. }
  304. /**
  305. * Copy text attributes from the supplied Font to Java2D AttributedString
  306. */
  307. private static void copyAttributes(Font font, AttributedString str, @SuppressWarnings("SameParameterValue") int startIdx, int endIdx) {
  308. str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
  309. str.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints());
  310. if (font.getBold()) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
  311. if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
  312. if (font.getUnderline() == Font.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
  313. }
  314. /**
  315. * Return the cell, without taking account of merged regions.
  316. * <p>
  317. * Use {@link #getCellWithMerges(Sheet, int, int)} if you want the top left
  318. * cell from merged regions instead when the reference is a merged cell.
  319. * <p>
  320. * Use this where you want to know if the given cell is explicitly defined
  321. * or not.
  322. *
  323. * @param sheet The workbook sheet to look at.
  324. * @param rowIx The 0-based index of the row.
  325. * @param colIx The 0-based index of the cell.
  326. * @return cell at the given location, or null if not defined
  327. * @throws NullPointerException if sheet is null
  328. */
  329. public static Cell getCell(Sheet sheet, int rowIx, int colIx) {
  330. Row r = sheet.getRow(rowIx);
  331. if (r != null) {
  332. return r.getCell(colIx);
  333. }
  334. return null;
  335. }
  336. /**
  337. * Return the cell, taking account of merged regions. Allows you to find the
  338. * cell who's contents are shown in a given position in the sheet.
  339. *
  340. * <p>If the cell at the given co-ordinates is a merged cell, this will
  341. * return the primary (top-left) most cell of the merged region.
  342. * <p>If the cell at the given co-ordinates is not in a merged region,
  343. * then will return the cell itself.
  344. * <p>If there is no cell defined at the given co-ordinates, will return
  345. * null.
  346. *
  347. * @param sheet The workbook sheet to look at.
  348. * @param rowIx The 0-based index of the row.
  349. * @param colIx The 0-based index of the cell.
  350. * @return cell at the given location, its base merged cell, or null if not defined
  351. * @throws NullPointerException if sheet is null
  352. */
  353. public static Cell getCellWithMerges(Sheet sheet, int rowIx, int colIx) {
  354. final Cell c = getCell(sheet, rowIx, colIx);
  355. if (c != null) return c;
  356. for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) {
  357. if (mergedRegion.isInRange(rowIx, colIx)) {
  358. // The cell wanted is in this merged range
  359. // Return the primary (top-left) cell for the range
  360. Row r = sheet.getRow(mergedRegion.getFirstRow());
  361. if (r != null) {
  362. return r.getCell(mergedRegion.getFirstColumn());
  363. }
  364. }
  365. }
  366. // If we get here, then the cell isn't defined, and doesn't
  367. // live within any merged regions
  368. return null;
  369. }
  370. }