Commit patch from Paolo <p.mottadelli@sourcesense.com> attached to bug #44319.

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@616303 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ugo Cei 2008-01-29 13:32:48 +00:00
parent 2d33f4190a
commit 9ce000ac3a
2 changed files with 401 additions and 142 deletions

View File

@ -32,13 +32,18 @@ import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetView;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetViews;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
@ -51,6 +56,13 @@ public class XSSFSheet implements Sheet {
private List<Row> rows;
private ColumnHelper columnHelper;
public static final short LeftMargin = 0;
public static final short RightMargin = 1;
public static final short TopMargin = 2;
public static final short BottomMargin = 3;
public static final short HeaderMargin = 4;
public static final short FooterMargin = 5;
public XSSFSheet(CTSheet sheet) {
this.sheet = sheet;
this.worksheet = CTWorksheet.Factory.newInstance();
@ -86,6 +98,11 @@ public class XSSFSheet implements Sheet {
CTHeaderFooter hf = this.worksheet.addNewHeaderFooter();
hf.setOddHeader("&amp;C&amp;A");
hf.setOddFooter("&amp;C&amp;\"Arial\"&amp;10Page &amp;P");
CTPageBreak rowBreaks = worksheet.addNewRowBreaks();
CTPageBreak columnBreak = worksheet.addNewColBreaks();
CTSheetPr sheetPr = worksheet.addNewSheetPr();
CTPageSetUpPr sheetPageSetUpPr = sheetPr.addNewPageSetUpPr();
CTPageMargins pageMargins = worksheet.addNewPageMargins();
columnHelper = new ColumnHelper(worksheet);
}
@ -172,8 +189,7 @@ public class XSSFSheet implements Sheet {
}
public boolean getAutobreaks() {
// TODO Auto-generated method stub
return false;
return worksheet.getSheetPr().getPageSetUpPr().getAutoPageBreaks();
}
public Comment getCellComment(int row, int column) {
@ -182,9 +198,17 @@ public class XSSFSheet implements Sheet {
}
public short[] getColumnBreaks() {
// TODO Auto-generated method stub
CTBreak[] brkArray = worksheet.getColBreaks().getBrkArray();
if (brkArray.length == 0) {
return null;
}
short[] breaks = new short[brkArray.length];
for (int i = 0 ; i < brkArray.length ; i++) {
CTBreak brk = brkArray[i];
breaks[i] = (short) brk.getId();
}
return breaks;
}
public short getColumnWidth(short column) {
return (short) columnHelper.getColumn(column).getWidth();
@ -213,13 +237,17 @@ public class XSSFSheet implements Sheet {
}
public int getFirstRowNum() {
// TODO Auto-generated method stub
return 0;
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row row = it.next();
if (row != null) {
return row.getRowNum();
}
}
return -1;
}
public boolean getFitToPage() {
// TODO Auto-generated method stub
return false;
return worksheet.getSheetPr().getPageSetUpPr().getFitToPage();
}
public Footer getFooter() {
@ -238,8 +266,14 @@ public class XSSFSheet implements Sheet {
}
public int getLastRowNum() {
// TODO Auto-generated method stub
return 0;
int lastRowNum = -1;
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row row = it.next();
if (row != null) {
lastRowNum = row.getRowNum();
}
}
return lastRowNum;
}
public short getLeftCol() {
@ -248,8 +282,23 @@ public class XSSFSheet implements Sheet {
}
public double getMargin(short margin) {
// TODO Auto-generated method stub
return 0;
CTPageMargins pageMargins = worksheet.getPageMargins();
switch (margin) {
case LeftMargin:
return pageMargins.getLeft();
case RightMargin:
return pageMargins.getRight();
case TopMargin:
return pageMargins.getTop();
case BottomMargin:
return pageMargins.getBottom();
case HeaderMargin:
return pageMargins.getHeader();
case FooterMargin:
return pageMargins.getFooter();
default :
throw new RuntimeException( "Unknown margin constant: " + margin );
}
}
public Region getMergedRegionAt(int index) {
@ -278,8 +327,13 @@ public class XSSFSheet implements Sheet {
}
public int getPhysicalNumberOfRows() {
// TODO Auto-generated method stub
return 0;
int counter = 0;
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
if (it.next() != null) {
counter++;
}
}
return counter;
}
public PrintSetup getPrintSetup() {
@ -303,9 +357,18 @@ public class XSSFSheet implements Sheet {
}
public int[] getRowBreaks() {
// TODO Auto-generated method stub
CTPageBreak rowBreaks = worksheet.getRowBreaks();
int breaksCount = rowBreaks.getBrkArray().length;
if (breaksCount == 0) {
return null;
}
int[] breaks = new int[breaksCount];
for (int i = 0 ; i < breaksCount ; i++) {
CTBreak brk = rowBreaks.getBrkArray(i);
breaks[i] = (int) brk.getId();
}
return breaks;
}
public boolean getRowSumsBelow() {
// TODO Auto-generated method stub
@ -343,13 +406,17 @@ public class XSSFSheet implements Sheet {
}
public boolean isColumnBroken(short column) {
// TODO Auto-generated method stub
CTBreak[] brkArray = worksheet.getColBreaks().getBrkArray();
for (int i = 0 ; i < brkArray.length ; i++) {
if (brkArray[i].getId() == column) {
return true;
}
}
return false;
}
public boolean isColumnHidden(short column) {
// TODO Auto-generated method stub
return false;
return columnHelper.getColumn(column).getHidden();
}
public boolean isDisplayFormulas() {
@ -378,7 +445,15 @@ public class XSSFSheet implements Sheet {
}
public boolean isRowBroken(int row) {
// TODO Auto-generated method stub
int[] rowBreaks = getRowBreaks();
if (rowBreaks == null) {
return false;
}
for (int i = 0 ; i < rowBreaks.length ; i++) {
if (rowBreaks[i] == row) {
return true;
}
}
return false;
}
@ -388,8 +463,13 @@ public class XSSFSheet implements Sheet {
}
public void removeColumnBreak(short column) {
// TODO Auto-generated method stub
CTBreak[] brkArray = worksheet.getColBreaks().getBrkArray();
for (int i = 0 ; i < brkArray.length ; i++) {
if (brkArray[i].getId() == column) {
worksheet.getColBreaks().removeBrk(i);
continue;
}
}
}
public void removeMergedRegion(int index) {
@ -398,13 +478,25 @@ public class XSSFSheet implements Sheet {
}
public void removeRow(Row row) {
// TODO Auto-generated method stub
int counter = 0;
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row r = it.next();
if (r.getRowNum() == row.getRowNum()) {
it.remove();
worksheet.getSheetData().removeRow(counter);
}
counter++;
}
}
public void removeRowBreak(int row) {
// TODO Auto-generated method stub
CTBreak[] brkArray = worksheet.getRowBreaks().getBrkArray();
for (int i = 0 ; i < brkArray.length ; i++) {
if (brkArray[i].getId() == row) {
worksheet.getRowBreaks().removeBrk(i);
continue;
}
}
}
public Iterator<Row> rowIterator() {
@ -422,13 +514,14 @@ public class XSSFSheet implements Sheet {
}
public void setAutobreaks(boolean b) {
// TODO Auto-generated method stub
worksheet.getSheetPr().getPageSetUpPr().setAutoPageBreaks(b);
}
public void setColumnBreak(short column) {
// TODO Auto-generated method stub
if (! isColumnBroken(column)) {
CTBreak brk = worksheet.getColBreaks().addNewBrk();
brk.setId(column);
}
}
public void setColumnGroupCollapsed(short columnNumber, boolean collapsed) {
@ -437,8 +530,11 @@ public class XSSFSheet implements Sheet {
}
public void setColumnHidden(short column, boolean hidden) {
// TODO Auto-generated method stub
CTCol col = columnHelper.getColumn(column);
if (col == null) {
col = columnHelper.createColumn(column);
}
col.setHidden(hidden);
}
public void setColumnWidth(short column, short width) {
@ -494,8 +590,7 @@ public class XSSFSheet implements Sheet {
}
public void setFitToPage(boolean b) {
// TODO Auto-generated method stub
worksheet.getSheetPr().getPageSetUpPr().setFitToPage(b);
}
public void setGridsPrinted(boolean value) {
@ -509,8 +604,21 @@ public class XSSFSheet implements Sheet {
}
public void setMargin(short margin, double size) {
// TODO Auto-generated method stub
CTPageMargins pageMargins = worksheet.getPageMargins();
switch (margin) {
case LeftMargin:
pageMargins.setLeft(size);
case RightMargin:
pageMargins.setRight(size);
case TopMargin:
pageMargins.setTop(size);
case BottomMargin:
pageMargins.setBottom(size);
case HeaderMargin:
pageMargins.setHeader(size);
case FooterMargin:
pageMargins.setFooter(size);
}
}
public void setPrintGridlines(boolean newPrintGridlines) {
@ -524,8 +632,11 @@ public class XSSFSheet implements Sheet {
}
public void setRowBreak(int row) {
// TODO Auto-generated method stub
CTPageBreak pageBreak = worksheet.getRowBreaks();
if (! isRowBroken(row)) {
CTBreak brk = pageBreak.addNewBrk();
brk.setId(row);
}
}
public void setRowGroupCollapsed(int row, boolean collapse) {

View File

@ -28,7 +28,7 @@ import junit.framework.TestCase;
public class TestXSSFSheet extends TestCase {
public void testRowIterator() throws Exception {
public void testRowIterator() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
Row row1 = sheet.createRow(0);
@ -42,7 +42,7 @@ public class TestXSSFSheet extends TestCase {
assertFalse(it.hasNext());
}
public void testGetRow() throws Exception {
public void testGetRow() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
Row row1 = sheet.createRow(0);
@ -56,7 +56,7 @@ public class TestXSSFSheet extends TestCase {
assertEquals((double) 1000, cell_got.getNumericCellValue());
}
public void testCreateRow() throws Exception {
public void testCreateRow() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
@ -64,6 +64,7 @@ public class TestXSSFSheet extends TestCase {
Row row1 = sheet.createRow(0);
Row row2 = sheet.createRow(1);
assertEquals(0, row1.getRowNum());
assertEquals(1, row2.getRowNum());
Iterator<Row> it = sheet.rowIterator();
assertTrue(it.hasNext());
assertEquals(row1, it.next());
@ -88,7 +89,19 @@ public class TestXSSFSheet extends TestCase {
assertEquals(row2_overwritten_copy.getCell((short) 0).getNumericCellValue(), (double) 100);
}
public void testGetSetDefaultRowHeight() throws Exception {
public void testRemoveRow() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
Row row1 = sheet.createRow(1);
Row row2 = sheet.createRow(2);
assertNotNull(sheet.getRow(1));
sheet.removeRow(row2);
assertNull(sheet.getRow(0));
assertNull(sheet.getRow(2));
assertNotNull(sheet.getRow(1));
}
public void testGetSetDefaultRowHeight() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
// Test that default height set by the constructor
@ -102,7 +115,7 @@ public class TestXSSFSheet extends TestCase {
assertEquals((short) 340, sheet.getDefaultRowHeight());
}
public void testGetSetDefaultColumnWidth() throws Exception {
public void testGetSetDefaultColumnWidth() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
// Test that default column width set by the constructor
@ -112,11 +125,146 @@ public class TestXSSFSheet extends TestCase {
assertEquals((short) 14, sheet.getDefaultColumnWidth());
}
public void testGetSetColumnWidth() throws Exception {
public void testGetSetColumnWidth() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
// Test setting a column width and getting that value
sheet.setColumnWidth((short) 0, (short) 16);
assertEquals(16, sheet.getColumnWidth((short) 0));
}
public void testGetFirstLastRowNum() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
Row row10 = sheet.createRow(9);
Row row1 = sheet.createRow(0);
Row row2 = sheet.createRow(1);
assertEquals(0, sheet.getFirstRowNum());
assertEquals(9, sheet.getLastRowNum());
}
public void testGetPhysicalNumberOfRows() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
Row row10 = sheet.createRow(9);
Row row1 = sheet.createRow(0);
Row row2 = sheet.createRow(1);
assertEquals(3, sheet.getPhysicalNumberOfRows());
}
public void testGetSetRowBreaks() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertNull(sheet.getRowBreaks());
sheet.setRowBreak(1);
sheet.setRowBreak(15);
assertNotNull(sheet.getRowBreaks());
assertEquals(1, sheet.getRowBreaks()[0]);
assertEquals(15, sheet.getRowBreaks()[1]);
sheet.setRowBreak(1);
assertEquals(2, sheet.getRowBreaks().length);
}
public void testRemoveRowBreak() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
sheet.setRowBreak(1);
assertEquals(1, sheet.getRowBreaks().length);
sheet.setRowBreak(2);
assertEquals(2, sheet.getRowBreaks().length);
sheet.removeRowBreak(1);
assertEquals(1, sheet.getRowBreaks().length);
}
public void testGetSetColumnBreaks() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertNull(sheet.getColumnBreaks());
sheet.setColumnBreak((short) 11);
assertNotNull(sheet.getColumnBreaks());
assertEquals(11, sheet.getColumnBreaks()[0]);
sheet.setColumnBreak((short) 11223);
assertEquals(2, sheet.getColumnBreaks().length);
}
public void testRemoveColumnBreak() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertNull(sheet.getColumnBreaks());
sheet.setColumnBreak((short) 11);
assertNotNull(sheet.getColumnBreaks());
sheet.setColumnBreak((short) 12);
assertEquals(2, sheet.getColumnBreaks().length);
sheet.removeColumnBreak((short) 11);
assertEquals(1, sheet.getColumnBreaks().length);
sheet.removeColumnBreak((short) 15);
assertEquals(1, sheet.getColumnBreaks().length);
}
public void testIsRowColumnBroken() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.isRowBroken(0));
sheet.setRowBreak(3);
assertTrue(sheet.isRowBroken(3));
assertFalse(sheet.isColumnBroken((short) 0));
sheet.setColumnBreak((short) 3);
assertTrue(sheet.isColumnBroken((short) 3));
}
public void testGetSetAutoBreaks() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertTrue(sheet.getAutobreaks());
sheet.setAutobreaks(false);
assertFalse(sheet.getAutobreaks());
}
public void testIsSetColumnHidden() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
sheet.setColumnWidth((short) 0, (short) 13);
sheet.setColumnHidden((short) 1, false);
sheet.setColumnHidden((short) 2, true);
assertFalse(sheet.isColumnHidden((short) 0));
assertFalse(sheet.isColumnHidden((short) 1));
assertTrue(sheet.isColumnHidden((short) 2));
}
public void testIsSetFitToPage() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.getFitToPage());
sheet.setFitToPage(true);
assertTrue(sheet.getFitToPage());
sheet.setFitToPage(false);
assertFalse(sheet.getFitToPage());
}
public void testGetSetMargin() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertEquals((double) 0, sheet.getMargin((short) 0));
sheet.setMargin((short) 0, 10);
sheet.setMargin((short) 1, 11);
sheet.setMargin((short) 2, 12);
sheet.setMargin((short) 3, 13);
sheet.setMargin((short) 4, 14);
sheet.setMargin((short) 5, 15);
assertEquals((double) 10, sheet.getMargin((short) 0));
assertEquals((double) 11, sheet.getMargin((short) 1));
assertEquals((double) 12, sheet.getMargin((short) 2));
assertEquals((double) 13, sheet.getMargin((short) 3));
assertEquals((double) 14, sheet.getMargin((short) 4));
assertEquals((double) 15, sheet.getMargin((short) 5));
// Test that nothing happens if another margin constant is given (E.G. 65)
sheet.setMargin((short) 65, 15);
assertEquals((double) 10, sheet.getMargin((short) 0));
assertEquals((double) 11, sheet.getMargin((short) 1));
assertEquals((double) 12, sheet.getMargin((short) 2));
assertEquals((double) 13, sheet.getMargin((short) 3));
assertEquals((double) 14, sheet.getMargin((short) 4));
assertEquals((double) 15, sheet.getMargin((short) 5));
}
}