Browse Source

[ooxml branch] XSSFSheet get/setColumnWidth methods and tests, patch from Paolo

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@615258 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_5_BETA2
Nick Burch 16 years ago
parent
commit
da444a2b80

+ 9
- 3
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java View File

@@ -31,6 +31,7 @@ import org.apache.poi.ss.usermodel.Patriarch;
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.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
@@ -48,6 +49,7 @@ public class XSSFSheet implements Sheet {
private CTSheet sheet;
private CTWorksheet worksheet;
private List<Row> rows;
private ColumnHelper columnHelper;
public XSSFSheet(CTSheet sheet) {
this.sheet = sheet;
@@ -84,6 +86,7 @@ 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");
columnHelper = new ColumnHelper(worksheet);
}

protected CTSheet getSheet() {
@@ -184,8 +187,7 @@ public class XSSFSheet implements Sheet {
}

public short getColumnWidth(short column) {
// TODO Auto-generated method stub
return 0;
return (short) columnHelper.getColumn(column).getWidth();
}

public short getDefaultColumnWidth() {
@@ -440,7 +442,11 @@ public class XSSFSheet implements Sheet {
}

public void setColumnWidth(short column, short width) {
// TODO Auto-generated method stub
CTCol col = columnHelper.getColumn(column);
if (col == null) {
col = columnHelper.createColumn(column);
}
col.setWidth(width);
}

public void setDefaultColumnStyle(short column, CellStyle style) {

+ 99
- 0
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java View File

@@ -0,0 +1,99 @@
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.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;

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) {
for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
CTCol column = it.next();
if (column.getMin() == k) {
return column;
}
}
return null;
}
public CTCol getColumn(long index) {
for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
CTCol column = it.next();
if (getColumnIndex(column) == index) {
return column;
}
}
return null;
}
public long getColumnIndex(CTCol column) {
if (column.getMin() == column.getMax()) {
return column.getMin();
}
return -1;
}
public CTCol createColumn(long index) {
CTCol column = CTCol.Factory.newInstance();
setIndex(column, index);
columns.add(column);
return column;
}

private void setIndex(CTCol column, long index) {
column.setMin(index);
column.setMax(index);
}

}

+ 8
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java View File

@@ -111,4 +111,12 @@ public class TestXSSFSheet extends TestCase {
sheet.setDefaultColumnWidth((short) 14);
assertEquals((short) 14, sheet.getDefaultColumnWidth());
}
public void testGetSetColumnWidth() throws Exception {
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));
}
}

+ 71
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java View File

@@ -0,0 +1,71 @@
/* ====================================================================
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

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.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;


public class TestColumnHelper extends TestCase {
public void testGetColumnList() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
ColumnHelper columnHelper = new ColumnHelper(worksheet);
CTCols cols1 = worksheet.addNewCols();
CTCols cols2 = worksheet.addNewCols();
CTCol col1_1 = cols1.addNewCol();
col1_1.setMin(1);
col1_1.setMax(10);
col1_1.setWidth(13);
CTCol col1_2 = cols1.addNewCol();
col1_2.setMin(15);
col1_2.setMax(15);
col1_2.setWidth(14);

CTCol col2_1 = cols2.addNewCol();
col2_1.setMin(6);
col2_1.setMax(10);
CTCol col2_2 = cols2.addNewCol();
col2_2.setMin(20);
col2_2.setMax(20);
columnHelper.setColumns(worksheet);
List<CTCol> columns = columnHelper.getColumns();
assertEquals(12, columns.size());
assertEquals((double) 14, columnHelper.getColumn(15).getWidth());
}
}

Loading…
Cancel
Save