diff options
author | Nick Burch <nick@apache.org> | 2008-09-23 17:35:31 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-09-23 17:35:31 +0000 |
commit | 3901d88e01ce7a26eab5c18071363e3bca6cfaaf (patch) | |
tree | db186fd0c01f90b2190184b6bb0704477a947898 /src/java/org | |
parent | 301a628efb9a4e456daa7b997c3e869f36e1b570 (diff) | |
download | poi-3901d88e01ce7a26eab5c18071363e3bca6cfaaf.tar.gz poi-3901d88e01ce7a26eab5c18071363e3bca6cfaaf.zip |
Merged revisions 697562,697580,697584,697589,697595,697599 via svnmerge from
https://svn.apache.org/repos/asf/poi/trunk
........
r697562 | nick | 2008-09-21 18:49:20 +0100 (Sun, 21 Sep 2008) | 1 line
Fix bug #45784 - Support long chart titles in SeriesTextRecords
........
r697580 | nick | 2008-09-21 19:43:49 +0100 (Sun, 21 Sep 2008) | 1 line
Include the sheet name in the output of examples.XLS2CSVmra
........
r697584 | nick | 2008-09-21 19:56:32 +0100 (Sun, 21 Sep 2008) | 1 line
Test to show that bug #45492 is invalid
........
r697589 | nick | 2008-09-21 20:17:41 +0100 (Sun, 21 Sep 2008) | 1 line
Update hssf.extractor.ExcelExtractor to optionally output blank cells too
........
r697595 | nick | 2008-09-21 20:31:34 +0100 (Sun, 21 Sep 2008) | 1 line
Improved tests to show that bugs #45062 and #44292 are fixed
........
r697599 | nick | 2008-09-21 20:50:21 +0100 (Sun, 21 Sep 2008) | 1 line
Apply part of patch from bug #16936, with the rest made more HSSFCell like - Initial support for whole-row cell styling
........
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@698257 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
6 files changed, 166 insertions, 58 deletions
diff --git a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java index 17bde6da5f..e5e1bf9bc1 100644 --- a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java @@ -45,6 +45,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p private boolean includeSheetNames = true; private boolean formulasNotResults = false; private boolean includeCellComments = false; + private boolean includeBlankCells = false; public ExcelExtractor(HSSFWorkbook wb) { super(wb); @@ -77,13 +78,26 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p public void setIncludeCellComments(boolean includeCellComments) { this.includeCellComments = includeCellComments; } + /** + * Should blank cells be output? Default is to only + * output cells that are present in the file and are + * non-blank. + */ + public void setIncludeBlankCells(boolean includeBlankCells) { + this.includeBlankCells = includeBlankCells; + } /** * Retreives the text contents of the file */ public String getText() { StringBuffer text = new StringBuffer(); + + // We don't care about the differnce between + // null (missing) and blank cells + wb.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL); + // Process each sheet in turn for(int i=0;i<wb.getNumberOfSheets();i++) { HSSFSheet sheet = wb.getSheetAt(i); if(sheet == null) { continue; } @@ -112,63 +126,68 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p // Check each cell in turn int firstCell = row.getFirstCellNum(); int lastCell = row.getLastCellNum(); + if(includeBlankCells) { + firstCell = 0; + } + for(int k=firstCell;k<lastCell;k++) { HSSFCell cell = row.getCell(k); - if(cell == null) { continue; } boolean outputContents = true; - - switch(cell.getCellType()) { - case HSSFCell.CELL_TYPE_BLANK: - outputContents = false; - break; - case HSSFCell.CELL_TYPE_STRING: - text.append(cell.getRichStringCellValue().getString()); - break; - case HSSFCell.CELL_TYPE_NUMERIC: - // Note - we don't apply any formatting! - text.append(cell.getNumericCellValue()); - break; - case HSSFCell.CELL_TYPE_BOOLEAN: - text.append(cell.getBooleanCellValue()); - break; - case HSSFCell.CELL_TYPE_ERROR: - text.append(ErrorEval.getText(cell.getErrorCellValue())); - break; - case HSSFCell.CELL_TYPE_FORMULA: - if(formulasNotResults) { - text.append(cell.getCellFormula()); - } else { - switch(cell.getCachedFormulaResultType()) { - case HSSFCell.CELL_TYPE_STRING: - HSSFRichTextString str = cell.getRichStringCellValue(); - if(str != null && str.length() > 0) { - text.append(str.toString()); - } - break; - case HSSFCell.CELL_TYPE_NUMERIC: - text.append(cell.getNumericCellValue()); - break; - case HSSFCell.CELL_TYPE_BOOLEAN: - text.append(cell.getBooleanCellValue()); - break; - case HSSFCell.CELL_TYPE_ERROR: - text.append(ErrorEval.getText(cell.getErrorCellValue())); - break; - + + if(cell == null) { + // Only output if requested + outputContents = includeBlankCells; + } else { + switch(cell.getCellType()) { + case HSSFCell.CELL_TYPE_STRING: + text.append(cell.getRichStringCellValue().getString()); + break; + case HSSFCell.CELL_TYPE_NUMERIC: + // Note - we don't apply any formatting! + text.append(cell.getNumericCellValue()); + break; + case HSSFCell.CELL_TYPE_BOOLEAN: + text.append(cell.getBooleanCellValue()); + break; + case HSSFCell.CELL_TYPE_ERROR: + text.append(ErrorEval.getText(cell.getErrorCellValue())); + break; + case HSSFCell.CELL_TYPE_FORMULA: + if(formulasNotResults) { + text.append(cell.getCellFormula()); + } else { + switch(cell.getCachedFormulaResultType()) { + case HSSFCell.CELL_TYPE_STRING: + HSSFRichTextString str = cell.getRichStringCellValue(); + if(str != null && str.length() > 0) { + text.append(str.toString()); + } + break; + case HSSFCell.CELL_TYPE_NUMERIC: + text.append(cell.getNumericCellValue()); + break; + case HSSFCell.CELL_TYPE_BOOLEAN: + text.append(cell.getBooleanCellValue()); + break; + case HSSFCell.CELL_TYPE_ERROR: + text.append(ErrorEval.getText(cell.getErrorCellValue())); + break; + + } } - } - break; - default: - throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")"); - } - - // Output the comment, if requested and exists - HSSFComment comment = cell.getCellComment(); - if(includeCellComments && comment != null) { - // Replace any newlines with spaces, otherwise it - // breaks the output - String commentText = comment.getString().getString().replace('\n', ' '); - text.append(" Comment by "+comment.getAuthor()+": "+commentText); + break; + default: + throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")"); + } + + // Output the comment, if requested and exists + HSSFComment comment = cell.getCellComment(); + if(includeCellComments && comment != null) { + // Replace any newlines with spaces, otherwise it + // breaks the output + String commentText = comment.getString().getString().replace('\n', ' '); + text.append(" Comment by "+comment.getAuthor()+": "+commentText); + } } // Output a tab if we're not on the last cell diff --git a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java index 1f7106ad72..412e991d26 100644 --- a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java +++ b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java @@ -17,6 +17,10 @@ package org.apache.poi.hssf.record; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.LittleEndian; @@ -329,4 +333,31 @@ public final class BoundSheetRecord extends Record { public void setVeryHidden(boolean veryHidden) { field_2_option_flags = veryHiddenFlag.setShortBoolean(field_2_option_flags, veryHidden); } + + /** + * Takes a list of BoundSheetRecords, and returns the all + * ordered by the position of their BOFs. + */ + public static BoundSheetRecord[] orderByBofPosition(List boundSheetRecords) { + BoundSheetRecord[] bsrs = (BoundSheetRecord[])boundSheetRecords.toArray( + new BoundSheetRecord[boundSheetRecords.size()]); + + // Sort + Arrays.sort(bsrs, new BOFComparator()); + + // All done + return bsrs; + } + private static class BOFComparator implements Comparator { + public int compare(Object bsr1, Object bsr2) { + return compare((BoundSheetRecord)bsr1, (BoundSheetRecord)bsr2); + } + public int compare(BoundSheetRecord bsr1, BoundSheetRecord bsr2) { + if(bsr1.field_1_position_of_BOF < bsr2.field_1_position_of_BOF) + return -1; + if(bsr1.field_1_position_of_BOF == bsr2.field_1_position_of_BOF) + return 0; + return 1; + } + } } diff --git a/src/java/org/apache/poi/hssf/record/SeriesTextRecord.java b/src/java/org/apache/poi/hssf/record/SeriesTextRecord.java index 1271ef8764..66c26b3214 100644 --- a/src/java/org/apache/poi/hssf/record/SeriesTextRecord.java +++ b/src/java/org/apache/poi/hssf/record/SeriesTextRecord.java @@ -75,7 +75,8 @@ public class SeriesTextRecord field_1_id = in.readShort(); field_2_textLength = in.readByte(); field_3_undocumented = in.readByte(); - field_4_text = in.readUnicodeLEString(field_2_textLength); + field_4_text = in.readUnicodeLEString( + LittleEndian.ubyteToInt(field_2_textLength)); } public String toString() @@ -163,18 +164,34 @@ public class SeriesTextRecord /** * Get the text length field for the SeriesText record. */ - public byte getTextLength() + public int getTextLength() { - return field_2_textLength; + return LittleEndian.ubyteToInt(field_2_textLength); } /** * Set the text length field for the SeriesText record. + * Needs to be wrapped. */ public void setTextLength(byte field_2_textLength) { this.field_2_textLength = field_2_textLength; } + /** + * Set the text length field for the SeriesText record. + */ + public void setTextLength(int field_2_textLength) + { + if(field_2_textLength > 255) { + throw new IllegalArgumentException("Length must be 0-255"); + } + if(field_2_textLength > 127) { + this.field_2_textLength = (byte) + (field_2_textLength-256); + } else { + this.field_2_textLength = (byte)field_2_textLength; + } + } /** * Get the undocumented field for the SeriesText record. diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 76be1f9e59..84525881a7 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -926,7 +926,9 @@ public class HSSFCellStyle implements CellStyle } /** - * get the background fill color + * Get the background fill color. + * Note - many cells are actually filled with a foreground + * fill, not a background fill - see {@link #getFillForegroundColor()} * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short) * @return fill color */ @@ -952,7 +954,9 @@ public class HSSFCellStyle implements CellStyle } /** - * get the foreground fill color + * Get the foreground fill color. + * Many cells are filled with this, instead of a + * background color ({@link #getFillBackgroundColor()}) * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short) * @return fill color */ diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java index 0a31728899..4ec8e0dd02 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java @@ -58,6 +58,15 @@ public class HSSFPalette implements Palette } return null; } + /** + * Retrieves the color at a given index + * + * @param index the palette index, between 0x8 to 0x40 inclusive + * @return the color, or null if the index is not populated + */ + public HSSFColor getColor(int index) { + return getColor((short)index); + } /** * Finds the first occurance of a given color diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java index 26fd999efc..1e211cced9 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java @@ -21,6 +21,7 @@ import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.poi.hssf.record.CellValueRecordInterface; +import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -525,6 +526,33 @@ public final class HSSFRow implements Comparable, Row { return -1; return cellnum; } + + /** + * Is this row formatted? Most aren't, but some rows + * do have whole-row styles. For those that do, you + * can get the formatting from {@link #getRowStyle()} + */ + public boolean isFormatted() { + return row.getFormatted(); + } + /** + * Returns the whole-row cell styles. Most rows won't + * have one of these, so will return null. Call + * {@link #isFormatted()} to check first. + */ + public HSSFCellStyle getRowStyle() { + if(!isFormatted()) { return null; } + short styleIndex = row.getXFIndex(); + ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex); + return new HSSFCellStyle(styleIndex, xf, book); + } + /** + * Applies a whole-row cell styling to the row. + */ + public void setRowStyle(HSSFCellStyle style) { + row.setFormatted(true); + row.setXFIndex(style.getIndex()); + } /** * @return cell iterator of the physically defined cells. |