import java.io.OutputStream;
import java.util.LinkedList;
+import javax.xml.namespace.QName;
+
import org.apache.poi.ss.usermodel.SharedStringSource;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
public void writeTo(OutputStream out) throws IOException {
XmlOptions options = new XmlOptions();
options.setSaveOuter();
+ options.setUseDefaultNamespace();
+
+ // Requests use of whitespace for easier reading
+ options.setSavePrettyPrint();
+
SstDocument doc = SstDocument.Factory.newInstance(options);
CTSst sst = doc.addNewSst();
sst.setCount(strings.size());
for (String s : strings) {
sst.addNewSi().setT(s);
}
- doc.save(out);
+ doc.save(out, options);
}
}
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
+import javax.xml.namespace.QName;
+
/**
doc = StyleSheetDocument.Factory.parse(is);
// Grab all the different bits we care about
+ if(doc.getStyleSheet().getNumFmts() != null)
for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
}
+ if(doc.getStyleSheet().getFonts() != null)
for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
fonts.add(font);
}
+ if(doc.getStyleSheet().getFills() != null)
for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
fills.add(fill);
}
+ if(doc.getStyleSheet().getBorders() != null)
for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
borders.add(border);
}
+ if(doc.getStyleSheet().getCellStyleXfs() != null)
for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
xfs.add(xf);
}
+ if(doc.getStyleSheet().getCellStyleXfs() != null)
for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
styleXfs.add(xf);
}
public void writeTo(OutputStream out) throws IOException {
XmlOptions options = new XmlOptions();
options.setSaveOuter();
+ options.setUseDefaultNamespace();
+
+ // Requests use of whitespace for easier reading
+ options.setSavePrettyPrint();
+
+ // XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
+ options.setSaveSyntheticDocumentElement(
+ new QName(CTStylesheet.type.getName().getNamespaceURI(), doc.getStyleSheet().getDomNode().getNodeName()));
+
// Work on the current one
// Need to do this, as we don't handle
// TODO
// Save
- doc.save(out);
+ doc.save(out, options);
}
}
import java.io.OutputStream;
public interface XSSFModel {
+ /** Read from the given InputStream */
public void readFrom(InputStream is) throws IOException;
+ /** Write to the supplied OutputStream, with default options */
public void writeTo(OutputStream out) throws IOException;
}
workbook = wb;
// Create the things we only ever need one of
- dataFormat = new XSSFDataFormat();
+ dataFormat = new XSSFDataFormat(workbook.getStylesSource());
}
/**
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.DataFormat;
+import org.apache.poi.ss.usermodel.StylesSource;
/**
- * TODO - figure out how this should really work for XSSF
+ * Handles data formats for XSSF.
+ * TODO Figure out if there are build in formats too
*/
public class XSSFDataFormat implements DataFormat {
+ private StylesSource stylesSource;
+ public XSSFDataFormat(StylesSource stylesSource) {
+ this.stylesSource = stylesSource;
+ }
+
public short getFormat(String format) {
- return -1;
+ return (short)stylesSource.putNumberFormat(format);
}
public String getFormat(short index) {
- return null;
+ return stylesSource.getNumberFormatAt((long)index);
}
}
PackageRelationship rel =
corePart.addRelationship(ppName, TargetMode.INTERNAL, REL);
PackagePart part = corePart.getPackage().createPart(ppName, TYPE);
+
OutputStream out = part.getOutputStream();
model.writeTo(out);
out.close();
CTBookView bv = bvs.addNewWorkbookView();
bv.setActiveTab(0);
this.workbook.addNewSheets();
+
+ // We always require styles and shared strings
+ sharedStringSource = new SharedStringsTable();
+ stylesSource = new StylesTable();
}
public XSSFWorkbook(String path) throws IOException {
// Load shared strings
this.sharedStringSource = (SharedStringSource)
SHARED_STRINGS.load(getCorePart());
+ } catch(Exception e) {
+ throw new IOException("Unable to load shared strings - " + e.toString());
+ }
+ try {
// Load styles source
this.stylesSource = (StylesSource)
STYLES.load(getCorePart());
} catch(Exception e) {
- throw new IOException(e.getMessage());
+ e.printStackTrace();
+ throw new IOException("Unable to load styles - " + e.toString());
}
// Load individual sheets
// Update our internal reference for the package part
workbook.getSheets().getSheetArray(i).setId(rel.getId());
+ workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
}
// Write shared strings and styles
import junit.framework.TestCase;
+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.xssf.model.StylesTable;
Sheet sheet2 = workbook.createSheet("sheet2");
Sheet sheet3 = workbook.createSheet("sheet3");
- sheet1.createRow(0);
- sheet1.createRow(1);
+ RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
+
+ sheet1.createRow(0).createCell((short)0).setCellValue(1.2);
+ sheet1.createRow(1).createCell((short)0).setCellValue(rts);
sheet2.createRow(0);
assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
PackagePart wbPart =
pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
- // Links to the three sheets
+ // Links to the three sheets, shared strings and styles
assertTrue(wbPart.hasRelationships());
- assertEquals(3, wbPart.getRelationships().size());
+ assertEquals(5, wbPart.getRelationships().size());
// Load back the XSSFWorkbook
workbook = new XSSFWorkbook(pkg);
assertNotNull(workbook.getSheetAt(1));
assertNotNull(workbook.getSheetAt(2));
+ assertNotNull(workbook.getSharedStringSource());
+ assertNotNull(workbook.getStylesSource());
+
assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
+
+ sheet1 = workbook.getSheetAt(0);
+ assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
+ assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
}
public void testExisting() throws Exception {