XSSF implementation of header and footer. Contributed by Paolo Mottadelli <p.mottadelli@sourcesense.com> as bugzilla issue #44330.

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@619316 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ugo Cei 2008-02-07 09:24:49 +00:00
parent 321375583d
commit 154979d07d
14 changed files with 777 additions and 195 deletions

View File

@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFEvenFooter extends XSSFHeaderFooter implements Footer{
public XSSFEvenFooter(CTHeaderFooter headerFooter) {
super(headerFooter);
}
public String getText() {
return getHeaderFooter().getEvenFooter();
}
public void setText(String text) {
getHeaderFooter().setEvenFooter(text);
}
}

View File

@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFEvenHeader extends XSSFHeaderFooter implements Header{
public XSSFEvenHeader(CTHeaderFooter headerFooter) {
super(headerFooter);
}
public String getText() {
return getHeaderFooter().getEvenHeader();
}
public void setText(String text) {
getHeaderFooter().setEvenHeader(text);
}
}

View File

@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFFirstFooter extends XSSFHeaderFooter implements Footer{
public XSSFFirstFooter(CTHeaderFooter headerFooter) {
super(headerFooter);
}
public String getText() {
return getHeaderFooter().getFirstFooter();
}
public void setText(String text) {
getHeaderFooter().setFirstFooter(text);
}
}

View File

@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFFirstHeader extends XSSFHeaderFooter implements Header{
public XSSFFirstHeader(CTHeaderFooter headerFooter) {
super(headerFooter);
}
public String getText() {
return getHeaderFooter().getFirstHeader();
}
public void setText(String text) {
getHeaderFooter().setFirstHeader(text);
}
}

View File

@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFOddFooter extends XSSFHeaderFooter implements Footer{
public XSSFOddFooter(CTHeaderFooter headerFooter) {
super(headerFooter);
}
public String getText() {
return getHeaderFooter().getOddFooter();
}
public void setText(String text) {
getHeaderFooter().setOddFooter(text);
}
}

View File

@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFOddHeader extends XSSFHeaderFooter implements Header{
public XSSFOddHeader(CTHeaderFooter headerFooter) {
super(headerFooter);
}
public String getText() {
return getHeaderFooter().getOddHeader();
}
public void setText(String text) {
getHeaderFooter().setOddHeader(text);
}
}

View File

@ -96,8 +96,6 @@ public class XSSFSheet implements Sheet {
col.setCustomWidth(true); col.setCustomWidth(true);
} }
CTHeaderFooter hf = this.worksheet.addNewHeaderFooter(); 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 rowBreaks = worksheet.addNewRowBreaks();
CTPageBreak columnBreak = worksheet.addNewColBreaks(); CTPageBreak columnBreak = worksheet.addNewColBreaks();
CTSheetPr sheetPr = worksheet.addNewSheetPr(); CTSheetPr sheetPr = worksheet.addNewSheetPr();
@ -251,13 +249,36 @@ public class XSSFSheet implements Sheet {
} }
public Footer getFooter() { public Footer getFooter() {
// TODO Auto-generated method stub return getOddFooter();
return null; }
public Footer getOddFooter() {
return new XSSFOddFooter(worksheet.getHeaderFooter());
}
public Footer getEvenFooter() {
return new XSSFEvenFooter(worksheet.getHeaderFooter());
}
public Footer getFirstFooter() {
return new XSSFFirstFooter(worksheet.getHeaderFooter());
} }
public Header getHeader() { public Header getHeader() {
// TODO Auto-generated method stub return getOddHeader();
return null; }
public Header getOddHeader() {
return new XSSFOddHeader(worksheet.getHeaderFooter());
}
public Header getEvenHeader() {
return new XSSFEvenHeader(worksheet.getHeaderFooter()
);
}
public Header getFirstHeader() {
return new XSSFFirstHeader(worksheet.getHeaderFooter());
} }
public boolean getHorizontallyCenter() { public boolean getHorizontallyCenter() {
@ -282,23 +303,23 @@ public class XSSFSheet implements Sheet {
} }
public double getMargin(short margin) { public double getMargin(short margin) {
CTPageMargins pageMargins = worksheet.getPageMargins(); CTPageMargins pageMargins = worksheet.getPageMargins();
switch (margin) { switch (margin) {
case LeftMargin: case LeftMargin:
return pageMargins.getLeft(); return pageMargins.getLeft();
case RightMargin: case RightMargin:
return pageMargins.getRight(); return pageMargins.getRight();
case TopMargin: case TopMargin:
return pageMargins.getTop(); return pageMargins.getTop();
case BottomMargin: case BottomMargin:
return pageMargins.getBottom(); return pageMargins.getBottom();
case HeaderMargin: case HeaderMargin:
return pageMargins.getHeader(); return pageMargins.getHeader();
case FooterMargin: case FooterMargin:
return pageMargins.getFooter(); return pageMargins.getFooter();
default : default :
throw new RuntimeException( "Unknown margin constant: " + margin ); throw new RuntimeException( "Unknown margin constant: " + margin );
} }
} }
public Region getMergedRegionAt(int index) { public Region getMergedRegionAt(int index) {
@ -537,19 +558,11 @@ public class XSSFSheet implements Sheet {
} }
public void setColumnHidden(short column, boolean hidden) { public void setColumnHidden(short column, boolean hidden) {
CTCol col = columnHelper.getColumn(column); columnHelper.getColumn(column).setHidden(hidden);
if (col == null) {
col = columnHelper.createColumn(column);
}
col.setHidden(hidden);
} }
public void setColumnWidth(short column, short width) { public void setColumnWidth(short column, short width) {
CTCol col = columnHelper.getColumn(column); columnHelper.getColumn(column).setWidth(width);
if (col == null) {
col = columnHelper.createColumn(column);
}
col.setWidth(width);
} }
public void setDefaultColumnStyle(short column, CellStyle style) { public void setDefaultColumnStyle(short column, CellStyle style) {
@ -611,21 +624,21 @@ public class XSSFSheet implements Sheet {
} }
public void setMargin(short margin, double size) { public void setMargin(short margin, double size) {
CTPageMargins pageMargins = worksheet.getPageMargins(); CTPageMargins pageMargins = worksheet.getPageMargins();
switch (margin) { switch (margin) {
case LeftMargin: case LeftMargin:
pageMargins.setLeft(size); pageMargins.setLeft(size);
case RightMargin: case RightMargin:
pageMargins.setRight(size); pageMargins.setRight(size);
case TopMargin: case TopMargin:
pageMargins.setTop(size); pageMargins.setTop(size);
case BottomMargin: case BottomMargin:
pageMargins.setBottom(size); pageMargins.setBottom(size);
case HeaderMargin: case HeaderMargin:
pageMargins.setHeader(size); pageMargins.setHeader(size);
case FooterMargin: case FooterMargin:
pageMargins.setFooter(size); pageMargins.setFooter(size);
} }
} }
public void setPrintGridlines(boolean newPrintGridlines) { public void setPrintGridlines(boolean newPrintGridlines) {

View File

@ -0,0 +1,73 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public abstract class XSSFHeaderFooter implements Footer{
private HeaderFooterHelper helper;
private CTHeaderFooter headerFooter;
private String value;
public XSSFHeaderFooter(CTHeaderFooter headerFooter) {
this.headerFooter = headerFooter;
this.value = getText();
this.value = this.value != null ? this.value : "";
this.helper = new HeaderFooterHelper();
}
public CTHeaderFooter getHeaderFooter() {
return this.headerFooter;
}
public String getValue() {
return this.value;
}
public abstract String getText();
protected abstract void setText(String text);
public String getCenter() {
return helper.getCenterSection(getText());
}
public String getLeft() {
return helper.getLeftSection(getText());
}
public String getRight() {
return helper.getRightSection(getText());
}
public void setCenter(String newCenter) {
setText(helper.setCenterSection(getText(), newCenter));
}
public void setLeft(String newLeft) {
setText(helper.setLeftSection(getText(), newLeft));
}
public void setRight(String newRight) {
setText(helper.setRightSection(getText(), newRight));
}
}

View File

@ -1,99 +1,90 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.helpers; package org.apache.poi.xssf.usermodel.helpers;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class ColumnHelper { public class ColumnHelper {
private List<CTCol> columns;
public ColumnHelper(CTWorksheet worksheet) {
super();
setColumns(worksheet);
}
public List<CTCol> getColumns() {
return columns;
}
public void setColumns(CTWorksheet worksheet) {
columns = new ArrayList<CTCol>();
CTCols[] colsArray = worksheet.getColsArray();
for (int i = 0 ; i < colsArray.length ; i++) {
CTCols cols = colsArray[i];
CTCol[] colArray = cols.getColArray();
for (int y = 0 ; y < colArray.length ; y++) {
CTCol col = colArray[y];
for (long k = col.getMin() ; k <= col.getMax() ; k++) {
setColumn(columns, col, k);
}
}
}
}
private void setColumn(List<CTCol> columns, CTCol col, long k) {
CTCol column = getColumn(columns, k);
if (column == null) {
column = CTCol.Factory.newInstance();
column.setMin(k);
column.setMax(k);
setColumnAttributes(col, column);
columns.add(column);
}
else {
setColumnAttributes(col, column);
}
}
private void setColumnAttributes(CTCol col, CTCol column) {
if (col.getWidth() > 0) {
column.setWidth(col.getWidth());
}
// TODO set all col attributes
}
public CTCol getColumn(List<CTCol> columns, long k) { private CTWorksheet worksheet;
for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
CTCol column = it.next();
if (column.getMin() == k) { public ColumnHelper(CTWorksheet worksheet) {
return column; super();
} this.worksheet = worksheet;
} cleanColumns();
return null; }
public void cleanColumns() {
CTCols newCols = CTCols.Factory.newInstance();
CTCols[] colsArray = worksheet.getColsArray();
int i = 0;
for (i = 0 ; i < colsArray.length ; i++) {
CTCols cols = colsArray[i];
CTCol[] colArray = cols.getColArray();
for (int y = 0 ; y < colArray.length ; y++) {
CTCol col = colArray[y];
for (long k = col.getMin() ; k <= col.getMax() ; k++) {
if (!columnExists(newCols, k)) {
CTCol newCol = newCols.addNewCol();
newCol.setMin(k);
newCol.setMax(k);
setColumnAttributes(col, newCol);
}
}
}
}
for (int y = i-1 ; y >= 0 ; y--) {
worksheet.removeCols(y);
}
worksheet.addNewCols();
worksheet.setColsArray(0, newCols);
} }
public CTCol getColumn(long index) { public CTCol getColumn(long index) {
for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) { for (int i = 0 ; i < worksheet.getColsArray(0).sizeOfColArray() ; i++) {
CTCol column = it.next(); if (worksheet.getColsArray(0).getColArray(i).getMin() == index) {
if (getColumnIndex(column) == index) { return worksheet.getColsArray(0).getColArray(i);
return column; }
} }
} return null;
return null;
} }
public long getColumnIndex(CTCol column) { public boolean columnExists(CTCols cols, long index) {
if (column.getMin() == column.getMax()) { for (int i = 0 ; i < cols.sizeOfColArray() ; i++) {
return column.getMin(); if (cols.getColArray(i).getMin() == index) {
} return true;
return -1; }
}
return false;
} }
public CTCol createColumn(long index) { public void setColumnAttributes(CTCol col, CTCol newCol) {
CTCol column = CTCol.Factory.newInstance(); if (col.getWidth() != 0) {
setIndex(column, index); newCol.setWidth(col.getWidth());
columns.add(column); }
return column; if (col.getHidden()) {
newCol.setHidden(true);
}
} }
private void setIndex(CTCol column, long index) {
column.setMin(index);
column.setMax(index);
}
} }

View File

@ -0,0 +1,73 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.helpers;
public class HeaderFooterHelper {
private static final String HeaderFooterEntity = "&amp;";
private static final String HeaderFooterEntity_R = "&amp;R";
private static final String HeaderFooterEntity_L = "&amp;L";
private static final String HeaderFooterEntity_C = "&amp;C";
public String getCenterSection(String string) {
return getSection(string, HeaderFooterEntity_C);
}
public String getLeftSection(String string) {
return getSection(string, HeaderFooterEntity_L);
}
public String getRightSection(String string) {
return getSection(string, HeaderFooterEntity_R);
}
public String setCenterSection(String string, String newCenter) {
return setSection(string, newCenter, HeaderFooterEntity_C);
}
public String setLeftSection(String string, String newLeft) {
return setSection(string, newLeft, HeaderFooterEntity_L);
}
public String setRightSection(String string, String newRight) {
return setSection(string, newRight, HeaderFooterEntity_R);
}
public String setSection(String string, String newSection, String entity) {
string = string != null ? string : "";
String oldSection = getSection(string, entity);
if (oldSection.equals("")) {
return string.concat(entity + newSection);
}
return string.replaceAll(entity + oldSection, entity + newSection);
}
private String getSection(String string, String entity) {
if (string == null) {
return "";
}
String stringAfterEntity = "";
if (string.indexOf(entity) >= 0) {
stringAfterEntity = string.substring(string.indexOf(entity) + entity.length());
}
String nextEntity = "";
if (stringAfterEntity.indexOf(HeaderFooterEntity) > 0) {
nextEntity = stringAfterEntity.substring(stringAfterEntity.indexOf(HeaderFooterEntity), stringAfterEntity.indexOf(HeaderFooterEntity) + (HeaderFooterEntity.length()));
stringAfterEntity = stringAfterEntity.substring(0, stringAfterEntity.indexOf(nextEntity));
}
return stringAfterEntity;
}
}

View File

@ -0,0 +1,50 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import junit.framework.TestCase;
public class TestXSSFHeaderFooter extends TestCase {
public void testGetSetCenterLeftRight() {
XSSFOddFooter footer = new XSSFOddFooter(CTHeaderFooter.Factory.newInstance());
assertEquals("", footer.getCenter());
footer.setCenter("My first center section");
assertEquals("My first center section", footer.getCenter());
footer.setCenter("No, let's update the center section");
assertEquals("No, let's update the center section", footer.getCenter());
footer.setLeft("And add a left one");
footer.setRight("Finally the right section is added");
assertEquals("And add a left one", footer.getLeft());
assertEquals("Finally the right section is added", footer.getRight());
// Test changing the three sections value
footer.setCenter("Second center version");
footer.setLeft("Second left version");
footer.setRight("Second right version");
assertEquals("Second center version", footer.getCenter());
assertEquals("Second left version", footer.getLeft());
assertEquals("Second right version", footer.getRight());
}
}

View File

@ -20,6 +20,7 @@ package org.apache.poi.xssf.usermodel;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
@ -125,14 +126,6 @@ public class TestXSSFSheet extends TestCase {
assertEquals((short) 14, sheet.getDefaultColumnWidth()); assertEquals((short) 14, sheet.getDefaultColumnWidth());
} }
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() { public void testGetFirstLastRowNum() {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1"); Sheet sheet = workbook.createSheet("Sheet 1");
@ -220,17 +213,6 @@ public class TestXSSFSheet extends TestCase {
assertFalse(sheet.getAutobreaks()); 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() { public void testIsSetFitToPage() {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1"); Sheet sheet = workbook.createSheet("Sheet 1");
@ -246,16 +228,31 @@ public class TestXSSFSheet extends TestCase {
Sheet sheet = workbook.createSheet("Sheet 1"); Sheet sheet = workbook.createSheet("Sheet 1");
assertEquals((double) 0, sheet.getMargin((short) 0)); assertEquals((double) 0, sheet.getMargin((short) 0));
sheet.setMargin((short) 0, 10); 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) 10, sheet.getMargin((short) 0));
assertEquals((double) 10, sheet.getMargin((short) 1));
assertEquals((double) 10, sheet.getMargin((short) 2));
assertEquals((double) 10, sheet.getMargin((short) 3));
assertEquals((double) 10, sheet.getMargin((short) 4));
assertEquals((double) 10, sheet.getMargin((short) 5));
sheet.setMargin((short) 1, 11);
assertEquals((double) 11, sheet.getMargin((short) 1)); assertEquals((double) 11, sheet.getMargin((short) 1));
assertEquals((double) 11, sheet.getMargin((short) 2));
assertEquals((double) 11, sheet.getMargin((short) 3));
assertEquals((double) 11, sheet.getMargin((short) 4));
assertEquals((double) 11, sheet.getMargin((short) 5));
sheet.setMargin((short) 2, 12);
assertEquals((double) 12, sheet.getMargin((short) 2)); assertEquals((double) 12, sheet.getMargin((short) 2));
assertEquals((double) 12, sheet.getMargin((short) 3));
assertEquals((double) 12, sheet.getMargin((short) 4));
assertEquals((double) 12, sheet.getMargin((short) 5));
sheet.setMargin((short) 3, 13);
assertEquals((double) 13, sheet.getMargin((short) 3)); assertEquals((double) 13, sheet.getMargin((short) 3));
assertEquals((double) 13, sheet.getMargin((short) 4));
assertEquals((double) 13, sheet.getMargin((short) 5));
sheet.setMargin((short) 4, 14);
assertEquals((double) 14, sheet.getMargin((short) 4)); assertEquals((double) 14, sheet.getMargin((short) 4));
assertEquals((double) 14, sheet.getMargin((short) 5));
sheet.setMargin((short) 5, 15);
assertEquals((double) 15, sheet.getMargin((short) 5)); assertEquals((double) 15, sheet.getMargin((short) 5));
// Test that nothing happens if another margin constant is given (E.G. 65) // Test that nothing happens if another margin constant is given (E.G. 65)
@ -267,4 +264,62 @@ public class TestXSSFSheet extends TestCase {
assertEquals((double) 14, sheet.getMargin((short) 4)); assertEquals((double) 14, sheet.getMargin((short) 4));
assertEquals((double) 15, sheet.getMargin((short) 5)); assertEquals((double) 15, sheet.getMargin((short) 5));
} }
public void testGetFooter() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
assertNotNull(sheet.getFooter());
sheet.getFooter().setCenter("test center footer");
assertEquals("test center footer", sheet.getFooter().getCenter());
}
public void testGetAllHeadersFooters() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
assertNotNull(sheet.getOddFooter());
assertNotNull(sheet.getEvenFooter());
assertNotNull(sheet.getFirstFooter());
assertNotNull(sheet.getOddHeader());
assertNotNull(sheet.getEvenHeader());
assertNotNull(sheet.getFirstHeader());
assertEquals("", sheet.getOddFooter().getLeft());
sheet.getOddFooter().setLeft("odd footer left");
assertEquals("odd footer left", sheet.getOddFooter().getLeft());
assertEquals("", sheet.getEvenFooter().getLeft());
sheet.getEvenFooter().setLeft("even footer left");
assertEquals("even footer left", sheet.getEvenFooter().getLeft());
assertEquals("", sheet.getFirstFooter().getLeft());
sheet.getFirstFooter().setLeft("first footer left");
assertEquals("first footer left", sheet.getFirstFooter().getLeft());
assertEquals("", sheet.getOddHeader().getLeft());
sheet.getOddHeader().setLeft("odd header left");
assertEquals("odd header left", sheet.getOddHeader().getLeft());
assertEquals("", sheet.getOddHeader().getRight());
sheet.getOddHeader().setRight("odd header right");
assertEquals("odd header right", sheet.getOddHeader().getRight());
assertEquals("", sheet.getOddHeader().getCenter());
sheet.getOddHeader().setCenter("odd header center");
assertEquals("odd header center", sheet.getOddHeader().getCenter());
}
public void testGetSetColumnWidth() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
sheet.setColumnWidth((short) 1,(short) 22);
assertEquals(22, sheet.getColumnWidth((short) 1));
}
public void testGetSetColumnHidden() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
sheet.setColumnHidden((short) 2, true);
assertTrue(sheet.isColumnHidden((short) 2));
}
} }

View File

@ -17,55 +17,95 @@
package org.apache.poi.xssf.usermodel.helpers; package org.apache.poi.xssf.usermodel.helpers;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.SharedStringSource;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
public class TestColumnHelper extends TestCase { public class TestColumnHelper extends TestCase {
public void testGetColumnList() { public void testCleanColumns() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
ColumnHelper columnHelper = new ColumnHelper(worksheet);
CTCols cols1 = worksheet.addNewCols();
CTCols cols1 = worksheet.addNewCols(); CTCol col1 = cols1.addNewCol();
CTCols cols2 = worksheet.addNewCols(); col1.setMin(1);
col1.setMax(1);
CTCol col1_1 = cols1.addNewCol(); col1.setWidth(88);
col1_1.setMin(1); col1.setHidden(true);
col1_1.setMax(10); CTCol col2 = cols1.addNewCol();
col1_1.setWidth(13); col2.setMin(2);
CTCol col1_2 = cols1.addNewCol(); col2.setMax(3);
col1_2.setMin(15); CTCols cols2 = worksheet.addNewCols();
col1_2.setMax(15); CTCol col4 = cols2.addNewCol();
col1_2.setWidth(14); col4.setMin(3);
col4.setMax(6);
// Test cleaning cols
assertEquals(2, worksheet.sizeOfColsArray());
int count = countColumns(worksheet);
assertEquals(7, count);
// Clean columns and test a clean worksheet
ColumnHelper helper = new ColumnHelper(worksheet);
assertEquals(1, worksheet.sizeOfColsArray());
count = countColumns(worksheet);
assertEquals(6, count);
assertEquals((double) 88, helper.getColumn(1).getWidth());
assertTrue(helper.getColumn(1).getHidden());
}
public void testGetColumn() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
CTCols cols1 = worksheet.addNewCols();
CTCol col1 = cols1.addNewCol();
col1.setMin(1);
col1.setMax(1);
col1.setWidth(88);
col1.setHidden(true);
CTCol col2 = cols1.addNewCol();
col2.setMin(2);
col2.setMax(3);
CTCols cols2 = worksheet.addNewCols();
CTCol col4 = cols2.addNewCol();
col4.setMin(3);
col4.setMax(6);
ColumnHelper helper = new ColumnHelper(worksheet);
assertNotNull(helper.getColumn(1));
assertEquals((double) 88, helper.getColumn(1).getWidth());
assertTrue(helper.getColumn(1).getHidden());
assertFalse(helper.getColumn(2).getHidden());
assertNull(helper.getColumn(99));
}
public void testSetColumnAttributes() {
CTCol col = CTCol.Factory.newInstance();
col.setWidth(12);
col.setHidden(true);
CTCol newCol = CTCol.Factory.newInstance();
assertEquals((double) 0, newCol.getWidth());
assertFalse(newCol.getHidden());
ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory.newInstance());
helper.setColumnAttributes(col, newCol);
assertEquals((double) 12, newCol.getWidth());
assertTrue(newCol.getHidden());
}
CTCol col2_1 = cols2.addNewCol(); private int countColumns(CTWorksheet worksheet) {
col2_1.setMin(6); int count;
col2_1.setMax(10); count = 0;
CTCol col2_2 = cols2.addNewCol(); for (int i = 0 ; i < worksheet.sizeOfColsArray() ; i++) {
col2_2.setMin(20); for (int y = 0 ; y < worksheet.getColsArray(i).sizeOfColArray() ; y++) {
col2_2.setMax(20); for (long k = worksheet.getColsArray(i).getColArray(y).getMin() ; k <= worksheet.getColsArray(i).getColArray(y).getMax() ; k++) {
count++;
columnHelper.setColumns(worksheet); }
List<CTCol> columns = columnHelper.getColumns(); }
}
assertEquals(12, columns.size()); return count;
assertEquals((double) 14, columnHelper.getColumn(15).getWidth()); }
}
} }

View File

@ -0,0 +1,59 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.helpers;
import junit.framework.TestCase;
import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class TestHeaderFooterHelper extends TestCase {
public void testGetCenterLeftRightSection() {
HeaderFooterHelper helper = new HeaderFooterHelper();
String headerFooter = "&amp;CTest the center section";
assertEquals("Test the center section", helper.getCenterSection(headerFooter));
headerFooter = "&amp;CTest the center section&amp;LThe left one&amp;RAnd the right one";
assertEquals("Test the center section", helper.getCenterSection(headerFooter));
assertEquals("The left one", helper.getLeftSection(headerFooter));
assertEquals("And the right one", helper.getRightSection(headerFooter));
}
public void testSetCenterLeftRightSection() {
HeaderFooterHelper helper = new HeaderFooterHelper();
String headerFooter = "";
headerFooter = helper.setCenterSection(headerFooter, "First added center section");
assertEquals("First added center section", helper.getCenterSection(headerFooter));
headerFooter = helper.setLeftSection(headerFooter, "First left");
assertEquals("First left", helper.getLeftSection(headerFooter));
headerFooter = helper.setRightSection(headerFooter, "First right");
assertEquals("First right", helper.getRightSection(headerFooter));
assertEquals("&amp;CFirst added center section&amp;LFirst left&amp;RFirst right", headerFooter);
headerFooter = helper.setRightSection(headerFooter, "First right&amp");
assertEquals("First right&amp", helper.getRightSection(headerFooter));
assertEquals("&amp;CFirst added center section&amp;LFirst left&amp;RFirst right&amp", headerFooter);
headerFooter = helper.setRightSection(headerFooter, "First right&amp;");
assertEquals("First right", helper.getRightSection(headerFooter));
assertEquals("&amp;CFirst added center section&amp;LFirst left&amp;RFirst right&amp;", headerFooter);
}
}