mirror of https://github.com/apache/poi.git
Start supporting styles on the XSSFWorkbook, and tests for it
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@637669 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ef20df0bd1
commit
58b52d1284
|
@ -0,0 +1,22 @@
|
|||
/* ====================================================================
|
||||
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.ss.usermodel;
|
||||
|
||||
public interface StylesSource {
|
||||
public String getNumberFormatAt(long idx);
|
||||
public long putNumberFormat(String fmt);
|
||||
}
|
|
@ -20,9 +20,12 @@ package org.apache.poi.xssf.model;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.poi.ss.usermodel.SharedStringSource;
|
||||
import org.apache.poi.ss.usermodel.StylesSource;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
import org.openxml4j.opc.PackagePart;
|
||||
|
@ -43,8 +46,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;;
|
|||
*
|
||||
* @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $
|
||||
*/
|
||||
public class StylesTable {
|
||||
private final LinkedList<String> numberFormats = new LinkedList<String>();
|
||||
public class StylesTable implements StylesSource {
|
||||
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
|
||||
private final LinkedList<CTFont> fonts = new LinkedList<CTFont>();
|
||||
private final LinkedList<CTFill> fills = new LinkedList<CTFill>();
|
||||
private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
|
||||
|
@ -80,7 +83,7 @@ public class StylesTable {
|
|||
|
||||
// Grab all the different bits we care about
|
||||
for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
|
||||
numberFormats.add(nfmt.getFormatCode());
|
||||
numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
|
||||
}
|
||||
for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
|
||||
fonts.add(font);
|
||||
|
@ -96,17 +99,56 @@ public class StylesTable {
|
|||
}
|
||||
}
|
||||
|
||||
public String getNumberFormatAt(int idx) {
|
||||
public String getNumberFormatAt(long idx) {
|
||||
return numberFormats.get(idx);
|
||||
}
|
||||
|
||||
public synchronized int putNumberFormat(String fmt) {
|
||||
if (numberFormats.contains(fmt)) {
|
||||
return numberFormats.indexOf(fmt);
|
||||
public synchronized long putNumberFormat(String fmt) {
|
||||
if (numberFormats.containsValue(fmt)) {
|
||||
// Find the key, and return that
|
||||
for(Enumeration<Long> keys = numberFormats.keys(); keys.hasMoreElements();) {
|
||||
Long key = keys.nextElement();
|
||||
if(numberFormats.get(key).equals(fmt)) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
|
||||
}
|
||||
numberFormats.add(fmt);
|
||||
return numberFormats.size() - 1;
|
||||
|
||||
// Find a spare key, and add that
|
||||
long newKey = 1;
|
||||
while(numberFormats.containsKey(newKey)) {
|
||||
newKey++;
|
||||
}
|
||||
numberFormats.put(newKey, fmt);
|
||||
return newKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* For unit testing only
|
||||
*/
|
||||
public int _getNumberFormatSize() {
|
||||
return numberFormats.size();
|
||||
}
|
||||
/**
|
||||
* For unit testing only
|
||||
*/
|
||||
public int _getFontsSize() {
|
||||
return fonts.size();
|
||||
}
|
||||
/**
|
||||
* For unit testing only
|
||||
*/
|
||||
public int _getFillsSize() {
|
||||
return fills.size();
|
||||
}
|
||||
/**
|
||||
* For unit testing only
|
||||
*/
|
||||
public int _getBordersSize() {
|
||||
return borders.size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save this table to its own PackagePart.
|
||||
|
@ -139,16 +181,21 @@ public class StylesTable {
|
|||
// Formats
|
||||
CTNumFmts formats = CTNumFmts.Factory.newInstance();
|
||||
formats.setCount(numberFormats.size());
|
||||
for (String fmt : numberFormats) {
|
||||
formats.addNewNumFmt().setFormatCode(fmt);
|
||||
for (Entry<Long, String> fmt : numberFormats.entrySet()) {
|
||||
CTNumFmt ctFmt = formats.addNewNumFmt();
|
||||
ctFmt.setNumFmtId(fmt.getKey());
|
||||
ctFmt.setFormatCode(fmt.getValue());
|
||||
}
|
||||
doc.getStyleSheet().setNumFmts(formats);
|
||||
|
||||
// Fonts
|
||||
// TODO
|
||||
|
||||
// Fills
|
||||
// TODO
|
||||
|
||||
// Borders
|
||||
// TODO
|
||||
|
||||
// Save
|
||||
doc.save(out);
|
||||
|
|
|
@ -35,10 +35,12 @@ import org.apache.poi.ss.usermodel.Palette;
|
|||
import org.apache.poi.ss.usermodel.PictureData;
|
||||
import org.apache.poi.ss.usermodel.SharedStringSource;
|
||||
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.util.POILogFactory;
|
||||
import org.apache.poi.util.POILogger;
|
||||
import org.apache.poi.xssf.model.SharedStringsTable;
|
||||
import org.apache.poi.xssf.model.StylesTable;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.apache.xmlbeans.XmlObject;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
|
@ -69,6 +71,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
|
||||
private static final String SHARED_STRINGS_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
|
||||
|
||||
private static final String STYLES_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
|
||||
|
||||
private static final String DRAWING_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
|
||||
|
||||
private static final String IMAGE_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
|
@ -78,6 +82,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
||||
|
||||
private SharedStringSource sharedStringSource;
|
||||
private StylesSource stylesSource;
|
||||
|
||||
private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
|
||||
|
||||
|
@ -97,14 +102,27 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
try {
|
||||
WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream());
|
||||
this.workbook = doc.getWorkbook();
|
||||
|
||||
PackageRelationshipCollection prc;
|
||||
Iterator<PackageRelationship> it;
|
||||
|
||||
// Load shared strings
|
||||
PackageRelationshipCollection prc = getCorePart().getRelationshipsByType(SHARED_STRINGS_RELATIONSHIP);
|
||||
Iterator<PackageRelationship> it = prc.iterator();
|
||||
prc = getCorePart().getRelationshipsByType(SHARED_STRINGS_RELATIONSHIP);
|
||||
it = prc.iterator();
|
||||
if (it.hasNext()) {
|
||||
PackageRelationship rel = it.next();
|
||||
PackagePart part = getTargetPart(rel);
|
||||
this.sharedStringSource = new SharedStringsTable(part);
|
||||
}
|
||||
// Load styles source
|
||||
prc = getCorePart().getRelationshipsByType(STYLES_RELATIONSHIP);
|
||||
it = prc.iterator();
|
||||
if (it.hasNext()) {
|
||||
PackageRelationship rel = it.next();
|
||||
PackagePart part = getTargetPart(rel);
|
||||
this.stylesSource = new StylesTable(part);
|
||||
}
|
||||
|
||||
// Load individual sheets
|
||||
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
|
||||
PackagePart part = getPackagePart(ctSheet);
|
||||
|
@ -345,8 +363,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
}
|
||||
|
||||
public String getSSTString(int index) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return getSharedStringSource().getSharedStringAt(index);
|
||||
}
|
||||
|
||||
public short getSelectedTab() {
|
||||
|
@ -513,6 +530,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
out.close();
|
||||
}
|
||||
|
||||
// TODO - shared strings source
|
||||
// TODO - styles source
|
||||
|
||||
pkg.close();
|
||||
|
||||
} catch (InvalidFormatException e) {
|
||||
|
@ -529,10 +549,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
public SharedStringSource getSharedStringSource() {
|
||||
return this.sharedStringSource;
|
||||
}
|
||||
|
||||
protected void setSharedStringSource(SharedStringSource sharedStringSource) {
|
||||
this.sharedStringSource = sharedStringSource;
|
||||
}
|
||||
|
||||
public StylesSource getStylesSource() {
|
||||
return this.stylesSource;
|
||||
}
|
||||
protected void setStylesSource(StylesSource stylesSource) {
|
||||
this.stylesSource = stylesSource;
|
||||
}
|
||||
|
||||
public CreationHelper getCreationHelper() {
|
||||
return new XSSFCreationHelper(this);
|
||||
|
|
|
@ -281,6 +281,10 @@ public class TestXSSFCell extends TestCase {
|
|||
|
||||
assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
|
||||
}
|
||||
|
||||
public void testCellFormatting() {
|
||||
|
||||
}
|
||||
|
||||
private XSSFRow createParentObjects() {
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
|
|
|
@ -24,6 +24,8 @@ import java.io.OutputStream;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.StylesSource;
|
||||
import org.apache.poi.xssf.model.StylesTable;
|
||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
|
||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
|
||||
|
||||
|
@ -130,4 +132,40 @@ public class TestXSSFWorkbook extends TestCase {
|
|||
workbook.write(out);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public void testExisting() throws Exception {
|
||||
File xml = new File(
|
||||
System.getProperty("HSSF.testdata.path") +
|
||||
File.separator + "Formatting.xlsx"
|
||||
);
|
||||
assertTrue(xml.exists());
|
||||
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
|
||||
assertNotNull(workbook.getSharedStringSource());
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
}
|
||||
|
||||
public void testStyles() throws Exception {
|
||||
File xml = new File(
|
||||
System.getProperty("HSSF.testdata.path") +
|
||||
File.separator + "Formatting.xlsx"
|
||||
);
|
||||
assertTrue(xml.exists());
|
||||
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
|
||||
|
||||
StylesSource ss = workbook.getStylesSource();
|
||||
assertNotNull(ss);
|
||||
assertTrue(ss instanceof StylesTable);
|
||||
StylesTable st = (StylesTable)ss;
|
||||
|
||||
// Has 8 number formats
|
||||
assertEquals(8, st._getNumberFormatSize());
|
||||
// Has 2 fonts
|
||||
assertEquals(2, st._getFontsSize());
|
||||
// Has 2 fills
|
||||
assertEquals(2, st._getFillsSize());
|
||||
// Has 1 border
|
||||
assertEquals(1, st._getBordersSize());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue