diff options
Diffstat (limited to 'src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java')
-rw-r--r-- | src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java | 95 |
1 files changed, 76 insertions, 19 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 0098e03a9d..f33570c10c 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -153,6 +153,7 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { int sloc = sheet.getLoc(); RowRecord row = sheet.getNextRow(); + boolean rowRecordsAlreadyPresent = row!=null; while (row != null) { @@ -177,6 +178,18 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet if ( ( lastrow == null ) || ( lastrow.getRowNum() != cval.getRow() ) ) { hrow = getRow( cval.getRow() ); + if (hrow == null) { + // Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords + // Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too. + if (rowRecordsAlreadyPresent) { + // if at least one row record is present, all should be present. + throw new RuntimeException("Unexpected missing row when some rows already present"); + } + // create the row record on the fly now. + RowRecord rowRec = new RowRecord(cval.getRow()); + sheet.addRow(rowRec); + hrow = createRowFromRecord(rowRec); + } } if ( hrow != null ) { @@ -983,12 +996,33 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet } /** + * Note - this is not the same as whether the sheet is focused (isActive) + * @return <code>true</code> if this sheet is currently selected + */ + public boolean isSelected() { + return getSheet().getWindowTwo().getSelected(); + } + /** * Sets whether sheet is selected. * @param sel Whether to select the sheet or deselect the sheet. */ public void setSelected( boolean sel ) { - getSheet().setSelected( sel ); + getSheet().getWindowTwo().setSelected(sel); + } + /** + * @return <code>true</code> if this sheet is currently focused + */ + public boolean isActive() { + return getSheet().getWindowTwo().isActive(); + } + /** + * Sets whether sheet is selected. + * @param sel Whether to select the sheet or deselect the sheet. + */ + public void setActive(boolean sel ) + { + getSheet().getWindowTwo().setActive(sel); } /** @@ -1690,6 +1724,23 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet * @param column the column index */ public void autoSizeColumn(short column) { + autoSizeColumn(column, false); + } + + /** + * Adjusts the column width to fit the contents. + * + * This process can be relatively slow on large sheets, so this should + * normally only be called once per column, at the end of your + * processing. + * + * You can specify whether the content of merged cells should be considered or ignored. + * Default is to ignore merged cells. + * + * @param column the column index + * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column + */ + public void autoSizeColumn(short column, boolean useMergedCells) { AttributedString str; TextLayout layout; /** @@ -1698,13 +1749,13 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet * '0' looks to be a good choice. */ char defaultChar = '0'; - + /** * This is the multiple that the font height is scaled by when determining the * boundary of rotated text. */ double fontHeightMultiple = 2.0; - + FontRenderContext frc = new FontRenderContext(null, true, true); HSSFWorkbook wb = new HSSFWorkbook(book); @@ -1716,21 +1767,27 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet int defaultCharWidth = (int)layout.getAdvance(); double width = -1; + rows: for (Iterator it = rowIterator(); it.hasNext();) { HSSFRow row = (HSSFRow) it.next(); HSSFCell cell = row.getCell(column); - boolean isCellInMergedRegion = false; - for (int i = 0 ; i < getNumMergedRegions() && ! isCellInMergedRegion; i++) { - isCellInMergedRegion = getMergedRegionAt(i).contains(row.getRowNum(), column); + if (cell == null) continue; + + int colspan = 1; + for (int i = 0 ; i < getNumMergedRegions(); i++) { + if (getMergedRegionAt(i).contains(row.getRowNum(), column)) { + if (!useMergedCells) { + // If we're not using merged cells, skip this one and move on to the next. + continue rows; + } + cell = row.getCell(getMergedRegionAt(i).getColumnFrom()); + colspan = 1+ getMergedRegionAt(i).getColumnTo() - getMergedRegionAt(i).getColumnFrom(); + } } - if (cell == null | isCellInMergedRegion) continue; - HSSFCellStyle style = cell.getCellStyle(); HSSFFont font = wb.getFontAt(style.getFontIndex()); - //the number of spaces to indent the text in the cell - int indention = style.getIndention(); if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { HSSFRichTextString rt = cell.getRichStringCellValue(); @@ -1763,9 +1820,9 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet trans.concatenate( AffineTransform.getScaleInstance(1, fontHeightMultiple) ); - width = Math.max(width, layout.getOutline(trans).getBounds().getWidth() / defaultCharWidth + indention); + width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } else { - width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth + indention); + width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } } } else { @@ -1808,19 +1865,19 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet trans.concatenate( AffineTransform.getScaleInstance(1, fontHeightMultiple) ); - width = Math.max(width, layout.getOutline(trans).getBounds().getWidth() / defaultCharWidth + indention); + width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } else { - width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth + indention); + width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } } } - if (width != -1) { - if (width > Short.MAX_VALUE) { //calculated width can be greater that Short.MAX_VALUE! - width = Short.MAX_VALUE; - } - sheet.setColumnWidth(column, (short) (width * 256)); + } + if (width != -1) { + if (width > Short.MAX_VALUE) { //width can be bigger that Short.MAX_VALUE! + width = Short.MAX_VALUE; } + sheet.setColumnWidth(column, (short) (width * 256)); } } |