diff options
author | Yegor Kozlov <yegor@apache.org> | 2008-09-26 06:02:38 +0000 |
---|---|---|
committer | Yegor Kozlov <yegor@apache.org> | 2008-09-26 06:02:38 +0000 |
commit | 0bb6110bd1ba80610457794657da37b36dfcdcc1 (patch) | |
tree | 94ef6daa12ae9f69094ede17f5df95d9b757fb7a /src/ooxml/java | |
parent | 75b8b893e520dc6c32f3bca1298841531abf3f25 (diff) | |
download | poi-0bb6110bd1ba80610457794657da37b36dfcdcc1.tar.gz poi-0bb6110bd1ba80610457794657da37b36dfcdcc1.zip |
applied patches #45894 and #45892, also major cleanup of XSSFSheet and related classes
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@699185 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java')
6 files changed, 548 insertions, 195 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java index b4705d207c..388fda66ba 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java @@ -61,7 +61,7 @@ public class CommentsTable implements CommentsSource, XSSFModel { options.setUseDefaultNamespace(); // Requests use of whitespace for easier reading - options.setSavePrettyPrint(); + //options.setSavePrettyPrint(); CommentsDocument doc = CommentsDocument.Factory.newInstance(options); doc.setComments(comments); diff --git a/src/ooxml/java/org/apache/poi/xssf/model/Drawing.java b/src/ooxml/java/org/apache/poi/xssf/model/Drawing.java index b38f7fc08f..58dc805708 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/Drawing.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/Drawing.java @@ -56,7 +56,7 @@ public class Drawing implements XSSFChildContainingModel { options.setSaveOuter(); options.setUseDefaultNamespace(); // Requests use of whitespace for easier reading - options.setSavePrettyPrint(); + //options.setSavePrettyPrint(); drawing.save(out, options); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index a027686621..d272bb4fa8 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -297,10 +297,6 @@ public final class XSSFCell implements Cell { public void setCellFormula(String formula) { - if (this.cell.getT() != STCellType.STR) - { - this.cell.setT(STCellType.STR); - } CTCellFormula f = CTCellFormula.Factory.newInstance(); f.setStringValue(formula); this.cell.setF(f); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index ebe69964b9..5c90429dd2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -186,7 +186,7 @@ public class XSSFRow implements Row { public short getHeight() { if (this.row.getHt() > 0) { - return (short) (this.row.getHt() * 20); + return (short) (this.row.getHt()); } return -1; } @@ -198,12 +198,31 @@ public class XSSFRow implements Row { return -1; } + /** + * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also + * happens to be the 1-based column number of the last cell. This value can be used as a + * standard upper bound when iterating over cells: + * <pre> + * short minColIx = row.getFirstCellNum(); + * short maxColIx = row.getLastCellNum(); + * for(short colIx=minColIx; colIx<maxColIx; colIx++) { + * XSSFCell cell = row.getCell(colIx); + * if(cell == null) { + * continue; + * } + * //... do something with cell + * } + * </pre> + * + * @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the + * row does not contain any cells. + */ public short getLastCellNum() { short lastCellNum = -1; for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) { Cell cell = it.next(); if (cell != null) { - lastCellNum = cell.getCellNum(); + lastCellNum = (short)(cell.getCellNum() + 1); } } return lastCellNum; @@ -241,11 +260,17 @@ public class XSSFRow implements Row { } public void setHeight(short height) { - this.row.setHt((double) height / 20); + this.row.setHt((double) height); + this.row.setCustomHeight(true); } - public void setHeightInPoints(float height) { + public void setHeight(double height) { this.row.setHt((double) height); + this.row.setCustomHeight(true); + } + + public void setHeightInPoints(float height) { + setHeight((short)height); } public void setRowNum(int rowNum) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index dc95e74175..257e83c958 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -19,10 +19,7 @@ package org.apache.poi.xssf.usermodel; import java.io.IOException; import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import org.apache.poi.hssf.util.PaneInformation; import org.apache.poi.ss.usermodel.CellStyle; @@ -80,6 +77,15 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState; +/** + * High level representation of a worksheet. + * + * <p> + * Sheets are the central structures within a workbook, and are where a user does most of his spreadsheet work. + * The most common type of sheet is the worksheet, which is represented as a grid of cells. Worksheet cells can + * contain text, numbers, dates, and formulas. Cells can also be formatted. A workbook usually contains more than one sheet. + * </p> + */ public class XSSFSheet implements Sheet { protected CTSheet sheet; protected CTWorksheet worksheet; @@ -139,12 +145,17 @@ public class XSSFSheet implements Sheet { hyperlinks = new ArrayList<XSSFHyperlink>(); } - public XSSFSheet(XSSFWorkbook workbook) { + protected XSSFSheet(XSSFWorkbook workbook) { this.workbook = workbook; hyperlinks = new ArrayList<XSSFHyperlink>(); } + /** + * Returns the parent XSSFWorkbook + * + * @return the parent XSSFWorkbook + */ public XSSFWorkbook getWorkbook() { return this.workbook; } @@ -154,7 +165,7 @@ public class XSSFSheet implements Sheet { * * @return a new instance */ - protected static CTWorksheet newSheetInstance(){ + protected static CTWorksheet newInstance(){ CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); CTSheetFormatPr ctFormat = worksheet.addNewSheetFormatPr(); ctFormat.setDefaultRowHeight(15.0); @@ -214,8 +225,13 @@ public class XSSFSheet implements Sheet { worksheet.save(out, xmlOptions); out.close(); } - - protected CTWorksheet getWorksheet() { + + /** + * Provide access to the underlying XML bean + * + * @return the underlying CTWorksheet bean + */ + public CTWorksheet getWorksheet() { return this.worksheet; } @@ -265,8 +281,35 @@ public class XSSFSheet implements Sheet { return ctMergeCells.sizeOfMergeCellArray(); } - public void autoSizeColumn(short column) { - columnHelper.setColBestFit(column, true); + /** + * 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. + * + * @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) { + //TODO: + columnHelper.setColBestFit(column, true); } public Patriarch createDrawingPatriarch() { @@ -274,11 +317,23 @@ public class XSSFSheet implements Sheet { return null; } + /** + * Creates a split (freezepane). Any existing freezepane or split pane is overwritten. + * @param colSplit Horizonatal position of split. + * @param rowSplit Vertical position of split. + * @param topRow Top row visible in bottom pane + * @param leftmostColumn Left column visible in right pane. + */ public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow) { this.createFreezePane(colSplit, rowSplit); this.showInPane((short)topRow, (short)leftmostColumn); } + /** + * Creates a split (freezepane). Any existing freezepane or split pane is overwritten. + * @param colSplit Horizonatal position of split. + * @param rowSplit Vertical position of split. + */ public void createFreezePane(int colSplit, int rowSplit) { getPane().setXSplit(colSplit); getPane().setYSplit(rowSplit); @@ -290,8 +345,8 @@ public class XSSFSheet implements Sheet { * Creates a new comment for this sheet. You still * need to assign it to a cell though */ - public Comment createComment() { - return getComments().addComment(); + public XSSFComment createComment() { + return (XSSFComment)getComments().addComment(); } protected XSSFRow addRow(int index, int rownum) { @@ -301,37 +356,52 @@ public class XSSFSheet implements Sheet { return xrow; } + /** + * Create a new row within the sheet and return the high level representation + * + * @param rownum row number + * @return High level {@link XSSFRow} object representing a row in the sheet + * @see #removeRow(org.apache.poi.ss.usermodel.Row) + */ public XSSFRow createRow(int rownum) { int index = 0; for (Row r : this.rows) { - if (r.getRowNum() == rownum) { - // Replace r with new row + if (r.getRowNum() == rownum) { + // Replace r with new row + XSSFRow xrow = addRow(index, rownum); + rows.set(index, xrow); + return xrow; + } + if (r.getRowNum() > rownum) { XSSFRow xrow = addRow(index, rownum); - rows.set(index, xrow); - return xrow; - } - if (r.getRowNum() > rownum) { - XSSFRow xrow = addRow(index, rownum); - rows.add(index, xrow); - return xrow; - } - ++index; + rows.add(index, xrow); + return xrow; + } + ++index; } XSSFRow xrow = addRow(index, rownum); rows.add(xrow); return xrow; } + /** + * Creates a split pane. Any existing freezepane or split pane is overwritten. + * @param xSplitPos Horizonatal position of split (in 1/20th of a point). + * @param ySplitPos Vertical position of split (in 1/20th of a point). + * @param topRow Top row visible in bottom pane + * @param leftmostColumn Left column visible in right pane. + * @param activePane Active pane. One of: PANE_LOWER_RIGHT, + * PANE_UPPER_RIGHT, PANE_LOWER_LEFT, PANE_UPPER_LEFT + * @see #PANE_LOWER_LEFT + * @see #PANE_LOWER_RIGHT + * @see #PANE_UPPER_LEFT + * @see #PANE_UPPER_RIGHT + */ public void createSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane) { createFreezePane(xSplitPos, ySplitPos, leftmostColumn, topRow); getPane().setActivePane(STPane.Enum.forInt(activePane)); } - public void dumpDrawingRecords(boolean fat) { - // TODO Auto-generated method stub - - } - public boolean getAlternateExpression() { // TODO Auto-generated method stub return false; @@ -342,15 +412,11 @@ public class XSSFSheet implements Sheet { return false; } - public boolean getAutobreaks() { - return getSheetTypePageSetUpPr().getAutoPageBreaks(); - } - - public Comment getCellComment(int row, int column) { - return getComments().findCellComment(row, column); + public XSSFComment getCellComment(int row, int column) { + return (XSSFComment)getComments().findCellComment(row, column); } - public Hyperlink getHyperlink(int row, int column) { + public XSSFHyperlink getHyperlink(int row, int column) { String ref = new CellReference(row, column).formatAsString(); for(XSSFHyperlink hyperlink : hyperlinks) { if(hyperlink.getCellRef().equals(ref)) { @@ -360,11 +426,18 @@ public class XSSFSheet implements Sheet { return null; } + /** + * Vertical page break information used for print layout view, page layout view, drawing print breaks + * in normal view, and for printing the worksheet. + * + * @return column indexes of all the vertical page breaks, never <code>null</code> + */ public int[] getColumnBreaks() { - CTBreak[] brkArray = getSheetTypeColumnBreaks().getBrkArray(); - if (brkArray.length == 0) { - return null; + if (!worksheet.isSetColBreaks() || worksheet.getColBreaks().sizeOfBrkArray() == 0) { + return new int[0]; } + + CTBreak[] brkArray = worksheet.getColBreaks().getBrkArray(); int[] breaks = new int[brkArray.length]; for (int i = 0 ; i < brkArray.length ; i++) { CTBreak brk = brkArray[i]; @@ -418,6 +491,11 @@ public class XSSFSheet implements Sheet { return false; } + /** + * Gets the first row on the sheet + * + * @return the number of the first logical row on the sheet, zero based + */ public int getFirstRowNum() { for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) { Row row = it.next(); @@ -428,18 +506,34 @@ public class XSSFSheet implements Sheet { return -1; } + /** + * Flag indicating whether the Fit to Page print option is enabled. + * + * @return <code>true</code> + */ public boolean getFitToPage() { - return getSheetTypePageSetUpPr().getFitToPage(); + CTSheetPr sheetPr = getSheetTypeSheetPr(); + CTPageSetUpPr psSetup = (sheetPr == null || !sheetPr.isSetPageSetUpPr()) ? + CTPageSetUpPr.Factory.newInstance() : sheetPr.getPageSetUpPr(); + return psSetup.getFitToPage(); + } + + protected CTSheetPr getSheetTypeSheetPr() { + if (worksheet.getSheetPr() == null) { + worksheet.setSheetPr(CTSheetPr.Factory.newInstance()); + } + return worksheet.getSheetPr(); } - protected CTHeaderFooter getSheetTypeHeaderFooter() { if (worksheet.getHeaderFooter() == null) { worksheet.setHeaderFooter(CTHeaderFooter.Factory.newInstance()); } return worksheet.getHeaderFooter(); } - + + + /** * Returns the default footer for the sheet, * creating one as needed. @@ -617,49 +711,133 @@ public class XSSFSheet implements Sheet { return false; } + /** + * Returns the logical row ( 0-based). If you ask for a row that is not + * defined you get a null. This is to say row 4 represents the fifth row on a sheet. + * + * @param rownum row to get + * @return <code>XSSFRow</code> representing the rownumber or <code>null</code> if its not defined on the sheet + */ public XSSFRow getRow(int rownum) { + //TODO current implemenation is expensive, it should take O(1), not O(N) for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) { - Row row = it.next(); - if (row.getRowNum() == rownum) { - return (XSSFRow)row; - } + Row row = it.next(); + if (row.getRowNum() == rownum) { + return (XSSFRow)row; + } } return null; } + /** + * Horizontal page break information used for print layout view, page layout view, drawing print breaks in normal + * view, and for printing the worksheet. + * + * @return row indexes of all the horizontal page breaks, never <code>null</code> + */ public int[] getRowBreaks() { - CTPageBreak rowBreaks = getSheetTypeRowBreaks(); - int breaksCount = rowBreaks.getBrkArray().length; - if (breaksCount == 0) { - return null; + if (!worksheet.isSetRowBreaks() || worksheet.getRowBreaks().sizeOfBrkArray() == 0) { + return new int[0]; } - int[] breaks = new int[breaksCount]; - for (int i = 0 ; i < breaksCount ; i++) { - CTBreak brk = rowBreaks.getBrkArray(i); - breaks[i] = (int) brk.getId(); + + CTBreak[] brkArray = worksheet.getRowBreaks().getBrkArray(); + int[] breaks = new int[brkArray.length]; + for (int i = 0 ; i < brkArray.length ; i++) { + CTBreak brk = brkArray[i]; + breaks[i] = (int)brk.getId(); } return breaks; } - protected CTPageBreak getSheetTypeRowBreaks() { - if (worksheet.getRowBreaks() == null) { - worksheet.setRowBreaks(CTPageBreak.Factory.newInstance()); - } - return worksheet.getRowBreaks(); - } - + /** + * Flag indicating whether summary rows appear below detail in an outline, when applying an outline. + * + * <p> + * When true a summary row is inserted below the detailed data being summarized and a + * new outline level is established on that row. + * </p> + * <p> + * When false a summary row is inserted above the detailed data being summarized and a new outline level + * is established on that row. + * </p> + * @return <code>true</code> if row summaries appear below detail in the outline + */ public boolean getRowSumsBelow() { - CTSheetPr sheetPr = getSheetTypeSheetPr(); - CTOutlinePr outLinePr = sheetPr.getOutlinePr(); - return outLinePr.getSummaryBelow(); + CTSheetPr sheetPr = worksheet.getSheetPr(); + CTOutlinePr outlinePr = (sheetPr != null && sheetPr.isSetOutlinePr()) + ? sheetPr.getOutlinePr() : CTOutlinePr.Factory.newInstance(); + return outlinePr.getSummaryBelow(); } + /** + * Flag indicating whether summary rows appear below detail in an outline, when applying an outline. + * + * <p> + * When true a summary row is inserted below the detailed data being summarized and a + * new outline level is established on that row. + * </p> + * <p> + * When false a summary row is inserted above the detailed data being summarized and a new outline level + * is established on that row. + * </p> + * @param value <code>true</code> if row summaries appear below detail in the outline + */ + public void setRowSumsBelow(boolean value) { + ensureOutlinePr().setSummaryBelow(value); + } + + /** + * Flag indicating whether summary columns appear to the right of detail in an outline, when applying an outline. + * + * <p> + * When true a summary column is inserted to the right of the detailed data being summarized + * and a new outline level is established on that column. + * </p> + * <p> + * When false a summary column is inserted to the left of the detailed data being + * summarized and a new outline level is established on that column. + * </p> + * @return <code>true</code> if col summaries appear right of the detail in the outline + */ public boolean getRowSumsRight() { - CTSheetPr sheetPr = getSheetTypeSheetPr(); - CTOutlinePr outLinePr = sheetPr.getOutlinePr(); - return outLinePr.getSummaryRight(); + CTSheetPr sheetPr = worksheet.getSheetPr(); + CTOutlinePr outlinePr = (sheetPr != null && sheetPr.isSetOutlinePr()) + ? sheetPr.getOutlinePr() : CTOutlinePr.Factory.newInstance(); + return outlinePr.getSummaryRight(); + } + + /** + * Flag indicating whether summary columns appear to the right of detail in an outline, when applying an outline. + * + * <p> + * When true a summary column is inserted to the right of the detailed data being summarized + * and a new outline level is established on that column. + * </p> + * <p> + * When false a summary column is inserted to the left of the detailed data being + * summarized and a new outline level is established on that column. + * </p> + * @param value <code>true</code> if col summaries appear right of the detail in the outline + */ + public void setRowSumsRight(boolean value) { + ensureOutlinePr().setSummaryRight(value); } + + /** + * Ensure CTWorksheet.CTSheetPr.CTOutlinePr + */ + private CTOutlinePr ensureOutlinePr(){ + CTSheetPr sheetPr = worksheet.isSetSheetPr() ? worksheet.getSheetPr() : worksheet.addNewSheetPr(); + CTOutlinePr outlinePr = sheetPr.isSetOutlinePr() ? sheetPr.getOutlinePr() : sheetPr.addNewOutlinePr(); + return outlinePr; + } + + /** + * A flag indicating whether scenarios are locked when the sheet is protected. + * + * @return true => protection enabled; false => protection disabled + */ public boolean getScenarioProtect() { return getSheetTypeProtection().getScenarios(); } @@ -671,19 +849,25 @@ public class XSSFSheet implements Sheet { return worksheet.getSheetProtection(); } + /** + * The top row in the visible view when the sheet is + * first viewed after opening it in a viewer + * + * @return integer indicating the rownum (0 based) of the top row + */ public short getTopRow() { String cellRef = getSheetTypeSheetView().getTopLeftCell(); CellReference cellReference = new CellReference(cellRef); return (short) cellReference.getRow(); } - // Right signature method. Remove the wrong one when it will be removed in HSSFSheet (and interface) + /** + * Determine whether printed output for this sheet will be vertically centered. + * + * @return whether printed output for this sheet will be vertically centered. + */ public boolean getVerticallyCenter() { - return getVerticallyCenter(true); - } - - public boolean getVerticallyCenter(boolean value) { - return getSheetTypePrintOptions().getVerticalCentered(); + return getSheetTypePrintOptions().getVerticalCentered(); } /** @@ -711,9 +895,9 @@ public class XSSFSheet implements Sheet { public void groupRow(int fromRow, int toRow) { for(int i=fromRow;i<=toRow;i++){ - XSSFRow xrow=(XSSFRow)getRow(i-1); - if(xrow==null){//create a new Row - xrow=(XSSFRow)createRow(i-1); + XSSFRow xrow = getRow(i-1); + if(xrow == null){//create a new Row + xrow = createRow(i-1); } CTRow ctrow=xrow.getCTRow(); short outlineLevel=ctrow.getOutlineLevel(); @@ -742,10 +926,13 @@ public class XSSFSheet implements Sheet { return outlineLevel; } + /** + * Determines if there is a page break at the indicated column + */ public boolean isColumnBroken(short column) { - CTBreak[] brkArray = getSheetTypeColumnBreaks().getBrkArray(); - for (int i = 0 ; i < brkArray.length ; i++) { - if (brkArray[i].getId() == column) { + int[] colBreaks = getColumnBreaks(); + for (int i = 0 ; i < colBreaks.length ; i++) { + if (colBreaks[i] == column) { return true; } } @@ -779,11 +966,14 @@ public class XSSFSheet implements Sheet { return getSheetTypePrintOptions().getGridLines(); } + /** + * Tests if there is a page break at the indicated row + * + * @param row index of the row to test + * @return <code>true</code> if there is a page break at the indicated row + */ public boolean isRowBroken(int row) { int[] rowBreaks = getRowBreaks(); - if (rowBreaks == null) { - return false; - } for (int i = 0 ; i < rowBreaks.length ; i++) { if (rowBreaks[i] == row) { return true; @@ -792,21 +982,34 @@ public class XSSFSheet implements Sheet { return false; } - public void protectSheet(String password) { - // TODO Auto-generated method stub - + /** + * Sets a page break at the indicated row + */ + public void setRowBreak(int row) { + CTPageBreak pgBreak = worksheet.isSetRowBreaks() ? worksheet.getRowBreaks() : worksheet.addNewRowBreaks(); + if (! isRowBroken(row)) { + CTBreak brk = pgBreak.addNewBrk(); + brk.setId(row); + } } + /** + * Removes a page break at the indicated column + */ public void removeColumnBreak(short column) { CTBreak[] brkArray = getSheetTypeColumnBreaks().getBrkArray(); for (int i = 0 ; i < brkArray.length ; i++) { if (brkArray[i].getId() == column) { getSheetTypeColumnBreaks().removeBrk(i); - continue; } } } + public void protectSheet(String password) { + // TODO Auto-generated method stub + + } + public void removeMergedRegion(int index) { CTMergeCell[] mergeCellsArray = new CTMergeCell[getMergedCells().sizeOfMergeCellArray() - 1]; for (int i = 0 ; i < getMergedCells().sizeOfMergeCellArray() ; i++) { @@ -833,12 +1036,15 @@ public class XSSFSheet implements Sheet { } } + /** + * Removes the page break at the indicated row + */ public void removeRowBreak(int row) { - CTBreak[] brkArray = getSheetTypeRowBreaks().getBrkArray(); + CTPageBreak pgBreak = worksheet.isSetRowBreaks() ? worksheet.getRowBreaks() : worksheet.addNewRowBreaks(); + CTBreak[] brkArray = pgBreak.getBrkArray(); for (int i = 0 ; i < brkArray.length ; i++) { if (brkArray[i].getId() == row) { - getSheetTypeRowBreaks().removeBrk(i); - continue; + pgBreak.removeBrk(i); } } } @@ -846,6 +1052,7 @@ public class XSSFSheet implements Sheet { public Iterator<Row> rowIterator() { return rows.iterator(); } + /** * Alias for {@link #rowIterator()} to * allow foreach loops @@ -864,8 +1071,27 @@ public class XSSFSheet implements Sheet { } - public void setAutobreaks(boolean b) { - getSheetTypePageSetUpPr().setAutoPageBreaks(b); + /** + * Flag indicating whether the sheet displays Automatic Page Breaks. + * + * @return <code>true</code> if the sheet displays Automatic Page Breaks. + */ + public boolean getAutobreaks() { + CTSheetPr sheetPr = getSheetTypeSheetPr(); + CTPageSetUpPr psSetup = (sheetPr == null || !sheetPr.isSetPageSetUpPr()) ? + CTPageSetUpPr.Factory.newInstance() : sheetPr.getPageSetUpPr(); + return psSetup.getAutoPageBreaks(); + } + + /** + * Flag indicating whether the sheet displays Automatic Page Breaks. + * + * @param value <code>true</code> if the sheet displays Automatic Page Breaks. + */ + public void setAutobreaks(boolean value) { + CTSheetPr sheetPr = getSheetTypeSheetPr(); + CTPageSetUpPr psSetup = sheetPr.isSetPageSetUpPr() ? sheetPr.getPageSetUpPr() : sheetPr.addNewPageSetUpPr(); + psSetup.setAutoPageBreaks(value); } public void setColumnBreak(short column) { @@ -982,36 +1208,15 @@ public class XSSFSheet implements Sheet { getSheetTypePrintOptions().setGridLines(newPrintGridlines); } - public void setRowBreak(int row) { - CTPageBreak pageBreak = getSheetTypeRowBreaks(); - if (! isRowBroken(row)) { - CTBreak brk = pageBreak.addNewBrk(); - brk.setId(row); - } - } - public void setRowGroupCollapsed(int row, boolean collapse) { // TODO Auto-generated method stub } - public void setRowSumsBelow(boolean b) { - CTSheetPr sheetPr = getSheetTypeSheetPr(); - CTOutlinePr outLinePr = sheetPr.getOutlinePr(); - outLinePr.setSummaryBelow(b); - } - - public void setRowSumsRight(boolean b) { - CTSheetPr sheetPr = getSheetTypeSheetPr(); - CTOutlinePr outLinePr = sheetPr.getOutlinePr(); - outLinePr.setSummaryRight(b); - } - public void setVerticallyCenter(boolean value) { getSheetTypePrintOptions().setVerticalCentered(value); } - // HSSFSheet compatibility methods. See also the following zoom related methods /** * Sets the zoom magnication for the sheet. The zoom is expressed as a * fraction. For example to express a zoom of 75% use 3 for the numerator @@ -1019,25 +1224,98 @@ public class XSSFSheet implements Sheet { * * @param numerator The numerator for the zoom magnification. * @param denominator The denominator for the zoom magnification. + * @see #setZoom(int) */ public void setZoom(int numerator, int denominator) { Float result = new Float(numerator)/new Float(denominator)*100; setZoom(result.intValue()); } - public void setZoom(long scale) { + /** + * Window zoom magnification for current view representing percent values. + * Valid values range from 10 to 400. Horizontal & Vertical scale together. + * + * For example: + * <pre> + * 10 - 10% + * 20 - 20% + * � + * 100 - 100% + * � + * 400 - 400% + * </pre> + * + * Current view can be Normal, Page Layout, or Page Break Preview. + * + * @param scale window zoom magnification + */ + public void setZoom(int scale) { getSheetTypeSheetView().setZoomScale(scale); } - public void setZoomNormal(long scale) { + /** + * Zoom magnification to use when in normal view, representing percent values. + * Valid values range from 10 to 400. Horizontal & Vertical scale together. + * + * For example: + * <pre> + * 10 - 10% + * 20 - 20% + * � + * 100 - 100% + * � + * 400 - 400% + * </pre> + * + * Applies for worksheet sheet type only; zero implies the automatic setting. + * + * @param scale window zoom magnification + */ + public void setZoomNormal(int scale) { getSheetTypeSheetView().setZoomScaleNormal(scale); } - public void setZoomPageLayoutView(long scale) { + /** + * Zoom magnification to use when in page layout view, representing percent values. + * Valid values range from 10 to 400. Horizontal & Vertical scale together. + * + * For example: + * <pre> + * 10 - 10% + * 20 - 20% + * � + * 100 - 100% + * � + * 400 - 400% + * </pre> + * + * Applies for worksheet sheet type only; zero implies the automatic setting. + * + * @param scale + */ + public void setZoomPageLayoutView(int scale) { getSheetTypeSheetView().setZoomScalePageLayoutView(scale); } - public void setZoomSheetLayoutView(long scale) { + /** + * Zoom magnification to use when in page break preview, representing percent values. + * Valid values range from 10 to 400. Horizontal & Vertical scale together. + * + * For example: + * <pre> + * 10 - 10% + * 20 - 20% + * � + * 100 - 100% + * � + * 400 - 400% + * </pre> + * + * Applies for worksheet only; zero implies the automatic setting. + * + * @param scale + */ + public void setZoomSheetLayoutView(int scale) { getSheetTypeSheetView().setZoomScaleSheetLayoutView(scale); } @@ -1063,13 +1341,20 @@ public class XSSFSheet implements Sheet { } } + /** + * Location of the top left visible cell Location of the top left visible cell in the bottom right + * pane (when in Left-to-Right mode). + * + * @param toprow the top row to show in desktop window pane + * @param leftcol the left column to show in desktop window pane + */ public void showInPane(short toprow, short leftcol) { CellReference cellReference = new CellReference(toprow, leftcol); String cellRef = cellReference.formatAsString(); getSheetTypeSheetView().setTopLeftCell(cellRef); } -public void ungroupColumn(short fromColumn, short toColumn) { + public void ungroupColumn(short fromColumn, short toColumn) { CTCols cols=worksheet.getColsArray(0); for(int index=fromColumn;index<=toColumn;index++){ CTCol col=columnHelper.getColumn(index, false); @@ -1114,13 +1399,6 @@ public void ungroupColumn(short fromColumn, short toColumn) { getSheetTypeSheetFormatPr().setOutlineLevelCol((short)(maxLevelCol)); } - public void setSelected(boolean flag) { - CTSheetViews views = getSheetTypeSheetViews(); - for (CTSheetView view : views.getSheetViewArray()) { - view.setTabSelected(flag); - } - } - protected CTSheetViews getSheetTypeSheetViews() { if (worksheet.getSheetViews() == null) { worksheet.setSheetViews(CTSheetViews.Factory.newInstance()); @@ -1128,30 +1406,76 @@ public void ungroupColumn(short fromColumn, short toColumn) { } return worksheet.getSheetViews(); } - + + /** + * Returns a flag indicating whether this sheet is selected. + * <p> + * When only 1 sheet is selected and active, this value should be in synch with the activeTab value. + * In case of a conflict, the Start Part setting wins and sets the active sheet tab. + * </p> + * Note: multiple sheets can be selected, but only one sheet can be active at one time. + * + * @return <code>true</code> if this sheet is selected + */ public boolean isSelected() { CTSheetView view = getDefaultSheetView(); return view != null && view.getTabSelected(); } + /** + * Sets a flag indicating whether this sheet is selected. + * + * <p> + * When only 1 sheet is selected and active, this value should be in synch with the activeTab value. + * In case of a conflict, the Start Part setting wins and sets the active sheet tab. + * </p> + * Note: multiple sheets can be selected, but only one sheet can be active at one time. + * + * @param value <code>true</code> if this sheet is selected + */ + public void setSelected(boolean value) { + CTSheetViews views = getSheetTypeSheetViews(); + for (CTSheetView view : views.getSheetViewArray()) { + view.setTabSelected(value); + } + } + + /** + * Assign a cell comment to a cell region in this worksheet + * + * @param cellRef cell region + * @param comment the comment to assign + */ public void setCellComment(String cellRef, XSSFComment comment) { CellReference cellReference = new CellReference(cellRef); comment.setRow(cellReference.getRow()); - comment.setColumn((short)cellReference.getCol()); + comment.setColumn(cellReference.getCol()); } - public void setCellHyperlink(XSSFHyperlink hyperlink) { + protected void setCellHyperlink(XSSFHyperlink hyperlink) { hyperlinks.add(hyperlink); } - + + /** + * Return location of the active cell, e.g. <code>A1</code>. + * + * @return the location of the active cell. + */ public String getActiveCell() { return getSheetTypeSelection().getActiveCell(); } + /** + * Sets location of the active cell + * + * @param cellRef the location of the active cell, e.g. <code>A1</code>.. + */ public void setActiveCell(String cellRef) { - getSheetTypeSelection().setActiveCell(cellRef); - } + CTSelection ctsel = getSheetTypeSelection(); + ctsel.setActiveCell(cellRef); + ctsel.setSqref(Arrays.asList(cellRef)); + } /** * Does this sheet have any comments on it? We need to know, @@ -1228,19 +1552,10 @@ public void ungroupColumn(short fromColumn, short toColumn) { } private CTPageSetUpPr getSheetTypePageSetUpPr() { - if (getSheetTypeSheetPr().getPageSetUpPr() == null) { - getSheetTypeSheetPr().setPageSetUpPr(CTPageSetUpPr.Factory.newInstance()); - } - return getSheetTypeSheetPr().getPageSetUpPr(); + CTSheetPr sheetPr = getSheetTypeSheetPr(); + return sheetPr.isSetPageSetUpPr() ? sheetPr.getPageSetUpPr() : sheetPr.addNewPageSetUpPr(); } - protected CTSheetPr getSheetTypeSheetPr() { - if (worksheet.getSheetPr() == null) { - worksheet.setSheetPr(CTSheetPr.Factory.newInstance()); - } - return worksheet.getSheetPr(); - } - private boolean removeRow(int startRow, int endRow, int n, int rownum) { if (rownum >= (startRow + n) && rownum <= (endRow + n)) { if (n > 0 && rownum > endRow) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 7ff8be2538..62743c6183 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -129,7 +129,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { throw new IOException("Unable to load styles - " + e.toString()); } try { - // Load shared strings + // Load themes this.themes = XSSFRelation.THEME.loadAll(getCorePart()); } catch(Exception e) { throw new IOException("Unable to load shared strings - " + e.toString()); @@ -195,7 +195,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } } - protected CTWorkbook getWorkbook() { + /** + * Return the underlying XML bean + * + * @return the underlying CTWorkbook bean + */ + public CTWorkbook getWorkbook() { return this.workbook; } @@ -265,8 +270,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return style; } - public DataFormat createDataFormat() { - return getCreationHelper().createDataFormat(); + public XSSFDataFormat createDataFormat() { + return (XSSFDataFormat)getCreationHelper().createDataFormat(); } public XSSFFont createFont() { @@ -287,7 +292,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } public XSSFSheet createSheet(String sheetname) { - return createSheet(sheetname, XSSFSheet.newSheetInstance()); + if (doesContainsSheetName( sheetname, sheets.size() )) + throw new IllegalArgumentException( "The workbook already contains a sheet of this name" ); + return createSheet(sheetname, XSSFSheet.newInstance()); } public XSSFSheet createSheet(String sheetname, CTWorksheet worksheet) { @@ -298,25 +305,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } public XSSFSheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) { - CTSheet sheet = addSheet(sheetname); - XSSFDialogsheet wrapper = new XSSFDialogsheet(sheet, dialogsheet, this); - this.sheets.add(wrapper); - return wrapper; + CTSheet sheet = addSheet(sheetname); + XSSFDialogsheet wrapper = new XSSFDialogsheet(sheet, dialogsheet, this); + this.sheets.add(wrapper); + return wrapper; } private CTSheet addSheet(String sheetname) { CTSheet sheet = workbook.getSheets().addNewSheet(); - if (sheetname != null) { - sheet.setName(sheetname); - } + sheet.setName(sheetname); return sheet; } - public void dumpDrawingGroupRecords(boolean fat) { - // TODO Auto-generated method stub - - } - public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { short fontNum=getNumberOfFonts(); for (short i = 0; i < fontNum; i++) { @@ -373,13 +373,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return false; } - public byte[] getBytes() { - // TODO Auto-generated method stub - return null; - } - - public CellStyle getCellStyleAt(short idx) { - return stylesSource.getStyleAt(idx); + public XSSFCellStyle getCellStyleAt(short idx) { + return (XSSFCellStyle)stylesSource.getStyleAt(idx); } public Palette getCustomPalette() { @@ -387,22 +382,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return null; } - /** - * get the first tab that is displayed in the list of tabs in excel. - */ - public int getFirstVisibleTab() { - CTBookViews bookViews = workbook.getBookViews(); - CTBookView bookView = bookViews.getWorkbookViewArray(0); - return (short) bookView.getActiveTab(); - } - /** - * deprecated Aug 2008 - * @deprecated - Misleading name - use getFirstVisibleTab() - */ - public short getDisplayedTab() { - return (short) getFirstVisibleTab(); - } - public XSSFFont getFontAt(short idx) { return (XSSFFont)stylesSource.getFontAt(idx); } @@ -545,18 +524,44 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } /** - * sets the first tab that is displayed in the list of tabs - * in excel. - * @param index + * Gets the first tab that is displayed in the list of tabs in excel. + * + * @return integer that contains the index to the active sheet in this book view. + */ + public int getFirstVisibleTab() { + CTBookViews bookViews = workbook.getBookViews(); + CTBookView bookView = bookViews.getWorkbookViewArray(0); + return (short) bookView.getActiveTab(); + } + + /** + * Sets the first tab that is displayed in the list of tabs in excel. + * + * @param index integer that contains the index to the active sheet in this book view. */ public void setFirstVisibleTab(short index) { CTBookViews bookViews = workbook.getBookViews(); CTBookView bookView= bookViews.getWorkbookViewArray(0); bookView.setActiveTab(index); } + /** - * deprecated Aug 2008 - * @deprecated - Misleading name - use setFirstVisibleTab() + * Gets the first tab that is displayed in the list of tabs + * in excel. + * @return an integer that contains the index to the active sheet in this book view. + * + * @deprecated Aug 2008 - Misleading name - use #getFirstVisibleTab() + */ + public short getDisplayedTab() { + return (short) getFirstVisibleTab(); + } + + /** + * sets the first tab that is displayed in the list of tabs + * in excel. + * @param index integer that contains the index to the active sheet in this book view. + * + * @deprecated Aug 2008 - Misleading name - use #setFirstVisibleTab() */ public void setDisplayedTab(short index) { setFirstVisibleTab(index); @@ -588,6 +593,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } public void setSheetName(int sheet, String name) { + if (doesContainsSheetName(name, sheet )) + throw new IllegalArgumentException( "The workbook already contains a sheet of this name" ); this.workbook.getSheets().getSheetArray(sheet).setName(name); } @@ -765,4 +772,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public CreationHelper getCreationHelper() { return new XSSFCreationHelper(this); } + + private boolean doesContainsSheetName(String name, int excludeSheetIdx) { + CTSheet[] ctSheetArray = workbook.getSheets().getSheetArray(); + for (int i = 0; i < ctSheetArray.length; i++) { + if (excludeSheetIdx != i && name.equalsIgnoreCase(ctSheetArray[i].getName())) + return true; + } + return false; + } + } |