XSSF named range support

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@643189 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-31 22:30:56 +00:00
parent 0c210e6842
commit 64c685079c
4 changed files with 203 additions and 17 deletions

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;
import org.apache.poi.ss.usermodel.Name;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
/**
* XSSF Implementation of a Named Range
*/
public class XSSFName implements Name {
private XSSFWorkbook workbook;
private CTDefinedName ctName;
protected XSSFName(XSSFWorkbook workbook) {
this.workbook = workbook;
this.ctName = CTDefinedName.Factory.newInstance();
}
protected XSSFName(CTDefinedName name, XSSFWorkbook workbook) {
this.workbook = workbook;
this.ctName = name;
}
/**
* Returns the underlying named range object
*/
protected CTDefinedName getCTName() {
return ctName;
}
public String getNameName() {
return ctName.getName();
}
public void setNameName(String nameName) {
ctName.setName(nameName);
}
public String getReference() {
return ctName.getStringValue();
}
public void setReference(String ref) {
ctName.setStringValue(ref);
}
public String getSheetName() {
long sheetId = ctName.getLocalSheetId();
if(sheetId >= 0) {
return workbook.getSheetName((int)sheetId);
}
return null;
}
public String getComment() {
return ctName.getComment();
}
public void setComment(String comment) {
ctName.setComment(comment);
}
}

View File

@ -58,6 +58,8 @@ import org.openxml4j.opc.PackagingURIHelper;
import org.openxml4j.opc.TargetMode;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
@ -188,6 +190,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
private CTWorkbook workbook;
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
private List<XSSFName> namedRanges = new LinkedList<XSSFName>();
private SharedStringSource sharedStringSource;
private StylesSource stylesSource;
@ -247,6 +250,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
} catch (InvalidFormatException e) {
throw new IOException(e.toString());
}
// Process the named ranges
if(workbook.getDefinedNames() != null) {
for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
namedRanges.add(new XSSFName(ctName, this));
}
}
}
protected CTWorkbook getWorkbook() {
@ -324,9 +334,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return null;
}
public Name createName() {
// TODO Auto-generated method stub
return null;
public XSSFName createName() {
XSSFName name = new XSSFName(this);
namedRanges.add(name);
return name;
}
public Sheet createSheet() {
@ -430,19 +441,19 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return null;
}
public Name getNameAt(int index) {
// TODO Auto-generated method stub
return null;
public XSSFName getNameAt(int index) {
return namedRanges.get(index);
}
public int getNameIndex(String name) {
// TODO Auto-generated method stub
return 0;
}
public String getNameName(int index) {
// TODO Auto-generated method stub
return null;
return getNameAt(index).getNameName();
}
public int getNameIndex(String name) {
for(int i=0; i<namedRanges.size(); i++) {
if(namedRanges.get(i).getNameName().equals(name)) {
return i;
}
}
return -1;
}
public short getNumCellStyles() {
@ -456,8 +467,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
public int getNumberOfNames() {
// TODO Auto-generated method stub
return 0;
return namedRanges.size();
}
public int getNumberOfSheets() {
@ -657,6 +667,21 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
StylesTable st = (StylesTable)stylesSource;
STYLES.save(st, corePart);
}
// Named ranges
if(namedRanges.size() > 0) {
CTDefinedNames names = CTDefinedNames.Factory.newInstance();
CTDefinedName[] nr = new CTDefinedName[namedRanges.size()];
for(int i=0; i<namedRanges.size(); i++) {
nr[i] = namedRanges.get(i).getCTName();
}
names.setDefinedNameArray(nr);
workbook.setDefinedNames(names);
} else {
if(workbook.isSetDefinedNames()) {
workbook.setDefinedNames(null);
}
}
// Now we can write out the main Workbook, with
// the correct references to the other parts

View File

@ -17,15 +17,19 @@
package org.apache.poi.xssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
@ -299,7 +303,91 @@ public class TestXSSFWorkbook extends TestCase {
st.putNumberFormat("testFORMAT2"));
assertEquals(10, st._getNumberFormatSize());
// Save, load back in again, and check
// TODO
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
workbook = new XSSFWorkbook(Package.open(bais));
ss = workbook.getStylesSource();
assertNotNull(ss);
assertTrue(ss instanceof StylesTable);
st = (StylesTable)ss;
assertEquals(10, st._getNumberFormatSize());
assertEquals(2, st._getFontsSize());
assertEquals(2, st._getFillsSize());
assertEquals(1, st._getBordersSize());
}
public void testNamedRanges() throws Exception {
// First up, a new file
XSSFWorkbook workbook = new XSSFWorkbook();
assertEquals(0, workbook.getNumberOfNames());
Name nameA = workbook.createName();
nameA.setReference("A2");
nameA.setNameName("ForA2");
XSSFName nameB = workbook.createName();
nameB.setReference("B3");
nameB.setNameName("ForB3");
nameB.setComment("B3 Comment");
// Save and re-load
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
workbook = new XSSFWorkbook(Package.open(bais));
assertEquals(2, workbook.getNumberOfNames());
assertEquals("A2", workbook.getNameAt(0).getReference());
assertEquals("ForA2", workbook.getNameAt(0).getNameName());
assertNull(workbook.getNameAt(0).getComment());
assertEquals("B3", workbook.getNameAt(1).getReference());
assertEquals("ForB3", workbook.getNameAt(1).getNameName());
assertEquals("B3 Comment", workbook.getNameAt(1).getComment());
assertEquals("ForA2", workbook.getNameName(0));
assertEquals(1, workbook.getNameIndex("ForB3"));
assertEquals(-1, workbook.getNameIndex("ForB3!!"));
// Now, an existing file with named ranges
File xml = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "WithVariousData.xlsx"
);
assertTrue(xml.exists());
workbook = new XSSFWorkbook(xml.toString());
assertEquals(2, workbook.getNumberOfNames());
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
assertEquals("AllBStrings", workbook.getNameAt(1).getNameName());
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
// Tweak, save, and re-check
workbook.getNameAt(1).setNameName("BStringsFun");
baos = new ByteArrayOutputStream();
workbook.write(baos);
bais = new ByteArrayInputStream(baos.toByteArray());
workbook = new XSSFWorkbook(Package.open(bais));
assertEquals(2, workbook.getNumberOfNames());
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
}
}