--- /dev/null
+/* ====================================================================
+ 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);
+}
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;
*
* @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>();
// 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);
}
}
- 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!");
+ }
+
+ // Find a spare key, and add that
+ long newKey = 1;
+ while(numberFormats.containsKey(newKey)) {
+ newKey++;
}
- numberFormats.add(fmt);
- return numberFormats.size() - 1;
+ 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.
// 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);
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;
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";
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
private SharedStringSource sharedStringSource;
+ private StylesSource stylesSource;
private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
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);
}
public String getSSTString(int index) {
- // TODO Auto-generated method stub
- return null;
+ return getSharedStringSource().getSharedStringAt(index);
}
public short getSelectedTab() {
out.close();
}
+ // TODO - shared strings source
+ // TODO - styles source
+
pkg.close();
} catch (InvalidFormatException e) {
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);
assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
}
+
+ public void testCellFormatting() {
+
+ }
private XSSFRow createParentObjects() {
XSSFWorkbook wb = new XSSFWorkbook();
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;
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());
+ }
}