From 26c6241c8a10ad0dc8620c936a536b5e004a64da Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Fri, 24 Jun 2011 12:15:16 +0000 Subject: [PATCH] Bug 48314 - Fixed setting column and row breaks in XSSF, also updated javadocs git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1139266 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/usermodel/HSSFSheet.java | 20 +++++- .../org/apache/poi/ss/usermodel/Sheet.java | 20 +++++- .../apache/poi/xssf/usermodel/XSSFSheet.java | 65 +++++++++++++------ 4 files changed, 81 insertions(+), 25 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 31ad081831..5e268167d7 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 48314 - Fixed setting column and row breaks in XSSF 51424 - Ignore exceptions in ParagraphSprmUncompressor 51415 - Fixed Workbook.createSheet(sheetName) to truncate names longer than 31 characters 51332 - Fixed internal IDs of shapes generated by HSSFPatriarch when there are more than 1023 drawing objects diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 92b5220356..59ef00cfe4 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -1506,7 +1506,14 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { /** * Sets a page break at the indicated row - * @param row FIXME: Document this! + * Breaks occur above the specified row and left of the specified column inclusive. + * + * For example, sheet.setColumnBreak(2); breaks the sheet into two parts + * with columns A,B,C in the first and D,E,... in the second. Simuilar, sheet.setRowBreak(2); + * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part + * and rows starting with rownum=4 in the second. + * + * @param row the row to break, inclusive */ public void setRowBreak(int row) { validateRow(row); @@ -1545,8 +1552,15 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { /** - * Sets a page break at the indicated column - * @param column + * Sets a page break at the indicated column. + * Breaks occur above the specified row and left of the specified column inclusive. + * + * For example, sheet.setColumnBreak(2); breaks the sheet into two parts + * with columns A,B,C in the first and D,E,... in the second. Simuilar, sheet.setRowBreak(2); + * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part + * and rows starting with rownum=4 in the second. + * + * @param column the column to break, inclusive */ public void setColumnBreak(int column) { validateColumn((short)column); diff --git a/src/java/org/apache/poi/ss/usermodel/Sheet.java b/src/java/org/apache/poi/ss/usermodel/Sheet.java index 090c25150d..4b82c08c8c 100644 --- a/src/java/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/java/org/apache/poi/ss/usermodel/Sheet.java @@ -673,7 +673,14 @@ public interface Sheet extends Iterable { /** * Sets a page break at the indicated row - * @param row FIXME: Document this! + * Breaks occur above the specified row and left of the specified column inclusive. + * + * For example, sheet.setColumnBreak(2); breaks the sheet into two parts + * with columns A,B,C in the first and D,E,... in the second. Simuilar, sheet.setRowBreak(2); + * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part + * and rows starting with rownum=4 in the second. + * + * @param row the row to break, inclusive */ void setRowBreak(int row); @@ -703,8 +710,15 @@ public interface Sheet extends Iterable { int[] getColumnBreaks(); /** - * Sets a page break at the indicated column - * @param column + * Sets a page break at the indicated column. + * Breaks occur above the specified row and left of the specified column inclusive. + * + * For example, sheet.setColumnBreak(2); breaks the sheet into two parts + * with columns A,B,C in the first and D,E,... in the second. Simuilar, sheet.setRowBreak(2); + * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part + * and rows starting with rownum=4 in the second. + * + * @param column the column to break, inclusive */ void setColumnBreak(int column); 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 6ebd49432e..df3d9e455e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -622,18 +622,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { int[] breaks = new int[brkArray.length]; for (int i = 0 ; i < brkArray.length ; i++) { CTBreak brk = brkArray[i]; - breaks[i] = (int)brk.getId(); + breaks[i] = (int)brk.getId() - 1; } return breaks; } - private CTPageBreak getSheetTypeColumnBreaks() { - if (worksheet.getColBreaks() == null) { - worksheet.setColBreaks(CTPageBreak.Factory.newInstance()); - } - return worksheet.getColBreaks(); - } - /** * Get the actual column width (in units of 1/256th of a character width ) * @@ -1077,7 +1070,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { int[] breaks = new int[brkArray.length]; for (int i = 0 ; i < brkArray.length ; i++) { CTBreak brk = brkArray[i]; - breaks[i] = (int)brk.getId(); + breaks[i] = (int)brk.getId() - 1; } return breaks; } @@ -1383,12 +1376,25 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Sets a page break at the indicated row + * Breaks occur above the specified row and left of the specified column inclusive. + * + * For example, sheet.setColumnBreak(2); breaks the sheet into two parts + * with columns A,B,C in the first and D,E,... in the second. Simuilar, sheet.setRowBreak(2); + * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part + * and rows starting with rownum=4 in the second. + * + * @param row the row to break, inclusive */ public void setRowBreak(int row) { CTPageBreak pgBreak = worksheet.isSetRowBreaks() ? worksheet.getRowBreaks() : worksheet.addNewRowBreaks(); if (! isRowBroken(row)) { CTBreak brk = pgBreak.addNewBrk(); - brk.setId(row); + brk.setId(row + 1); // this is id of the row element which is 1-based: + brk.setMan(true); + brk.setMax(SpreadsheetVersion.EXCEL2007.getLastColumnIndex()); //end column of the break + + pgBreak.setCount(pgBreak.sizeOfBrkArray()); + pgBreak.setManualBreakCount(pgBreak.sizeOfBrkArray()); } } @@ -1397,10 +1403,16 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { */ @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support public void removeColumnBreak(int column) { - CTBreak[] brkArray = getSheetTypeColumnBreaks().getBrkArray(); + if (!worksheet.isSetColBreaks()) { + // no breaks + return; + } + + CTPageBreak pgBreak = worksheet.getColBreaks(); + CTBreak[] brkArray = pgBreak.getBrkArray(); for (int i = 0 ; i < brkArray.length ; i++) { - if (brkArray[i].getId() == column) { - getSheetTypeColumnBreaks().removeBrk(i); + if (brkArray[i].getId() == (column + 1)) { + pgBreak.removeBrk(i); } } } @@ -1452,10 +1464,13 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { */ @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support public void removeRowBreak(int row) { - CTPageBreak pgBreak = worksheet.isSetRowBreaks() ? worksheet.getRowBreaks() : worksheet.addNewRowBreaks(); + if(!worksheet.isSetRowBreaks()) { + return; + } + CTPageBreak pgBreak = worksheet.getRowBreaks(); CTBreak[] brkArray = pgBreak.getBrkArray(); for (int i = 0 ; i < brkArray.length ; i++) { - if (brkArray[i].getId() == row) { + if (brkArray[i].getId() == (row + 1)) { pgBreak.removeBrk(i); } } @@ -1537,14 +1552,26 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } /** - * Sets a page break at the indicated column + * Sets a page break at the indicated column. + * Breaks occur above the specified row and left of the specified column inclusive. + * + * For example, sheet.setColumnBreak(2); breaks the sheet into two parts + * with columns A,B,C in the first and D,E,... in the second. Simuilar, sheet.setRowBreak(2); + * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part + * and rows starting with rownum=4 in the second. * - * @param column the column to break + * @param column the column to break, inclusive */ public void setColumnBreak(int column) { if (! isColumnBroken(column)) { - CTBreak brk = getSheetTypeColumnBreaks().addNewBrk(); - brk.setId(column); + CTPageBreak pgBreak = worksheet.isSetColBreaks() ? worksheet.getColBreaks() : worksheet.addNewColBreaks(); + CTBreak brk = pgBreak.addNewBrk(); + brk.setId(column + 1); // this is id of the row element which is 1-based: + brk.setMan(true); + brk.setMax(SpreadsheetVersion.EXCEL2007.getLastRowIndex()); //end row of the break + + pgBreak.setCount(pgBreak.sizeOfBrkArray()); + pgBreak.setManualBreakCount(pgBreak.sizeOfBrkArray()); } } -- 2.39.5