more cleanup and refactoring of the ooxml code:1. removed deprecated methods from xssf and interfaces

2. minimized the accessibility of internal constructors
3. more javadocs


git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@707839 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-10-25 11:48:50 +00:00
parent 18e5d2d0db
commit 97ceedbbf7
33 changed files with 461 additions and 513 deletions

View File

@ -20,6 +20,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.hssf.util.Region;
@ -38,7 +39,7 @@ public class MergingCells {
Cell cell = row.createCell((short) 1);
cell.setCellValue(new XSSFRichTextString("This is a test of merging"));
sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");

View File

@ -50,4 +50,21 @@ public class CellRangeAddress extends CellRangeAddressBase {
public static int getEncodedSize(int numberOfItems) {
return numberOfItems * ENCODED_SIZE;
}
public String formatAsString() {
StringBuffer sb = new StringBuffer();
CellReference cellRefFrom = new CellReference(getFirstRow(), getFirstColumn());
CellReference cellRefTo = new CellReference(getLastRow(), getLastColumn());
sb.append(cellRefFrom.formatAsString());
sb.append(':');
sb.append(cellRefTo.formatAsString());
return sb.toString();
}
public static CellRangeAddress valueOf(String ref) {
int sep = ref.indexOf(":");
CellReference cellFrom = new CellReference(ref.substring(0, sep));
CellReference cellTo = new CellReference(ref.substring(sep + 1));
return new CellRangeAddress(cellFrom.getRow(), cellTo.getRow(), cellFrom.getCol(), cellTo.getCol());
}
}

View File

@ -21,7 +21,6 @@ import java.util.Iterator;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.Region;
public interface Sheet extends Iterable<Row> {
@ -197,11 +196,6 @@ public interface Sheet extends Iterable<Row> {
*/
int addMergedRegion(CellRangeAddress region);
/**
* @deprecated (Aug-2008) use {@link #addMergedRegion(CellRangeAddress)}
*/
int addMergedRegion(Region region);
/**
* determines whether the output is vertically centered on the page.
* @param value true to vertically center, false otherwise.
@ -236,14 +230,6 @@ public interface Sheet extends Iterable<Row> {
int getNumMergedRegions();
/**
* gets the region at a particular index
* @param index of the region to fetch
* @return the merged region (simple eh?)
*/
Region getMergedRegionAt(int index);
/**
* @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
* be the third row if say for instance the second row is undefined.
@ -300,20 +286,6 @@ public interface Sheet extends Iterable<Row> {
void setRowSumsRight(boolean b);
/**
* whether alternate expression evaluation is on
* @return alternative expression evaluation or not
*/
boolean getAlternateExpression();
/**
* whether alternative formula entry is on
* @return alternative formulas or not
*/
boolean getAlternateFormula();
/**
* show automatic page breaks or not
* @return whether to show auto page breaks
@ -411,29 +383,12 @@ public interface Sheet extends Iterable<Row> {
*/
boolean getProtect();
/**
* @return hashed password
*/
short getPassword();
/**
* Answer whether object protection is enabled or disabled
* @return true => protection enabled; false => protection disabled
*/
boolean getObjectProtect();
/**
* Answer whether scenario protection is enabled or disabled
* @return true => protection enabled; false => protection disabled
*/
boolean getScenarioProtect();
/**
* Sets the protection enabled as well as the password
* @param password to set for protection
*/
void protectSheet(String password);
/**
* Sets the zoom magnication for the sheet. The zoom is expressed as a
* fraction. For example to express a zoom of 75% use 3 for the numerator

View File

@ -375,13 +375,6 @@ public interface Workbook {
*/
List getAllPictures();
/**
* Gets all embedded OLE2 objects from the Workbook.
*
* @return the list of embedded objects
*/
List getAllEmbeddedObjects();
/**
* Returns an object that handles instantiating concrete
* classes of the various instances one needs for

View File

@ -17,7 +17,6 @@
package org.apache.poi;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.poifs.common.POIFSConstants;
@ -48,19 +47,9 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{
*/
private POIXMLProperties properties;
protected POIXMLDocument() {
super(null, null);
try {
Package pkg = newPackage();
initialize(pkg);
} catch (IOException e){
throw new POIXMLException(e);
}
}
protected POIXMLDocument(Package pkg) throws IOException {
super(null, null);
initialize(pkg);
protected POIXMLDocument(Package pkg) {
super(pkg);
this.pkg = pkg;
}
/**
@ -76,36 +65,12 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{
}
}
private void initialize(Package pkg) throws IOException {
try {
this.pkg = pkg;
PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
// Get core part
this.packagePart = this.pkg.getPart(coreDocRelationship);
this.packageRel = coreDocRelationship;
// Verify it's there
if(this.packagePart == null) {
throw new IllegalArgumentException("No core part found for this document! Nothing with " + coreDocRelationship.getRelationshipType() + " present as a relation.");
}
} catch (OpenXML4JException e) {
throw new IOException(e.toString());
}
}
protected Package newPackage() throws IOException {
throw new POIXMLException("Must be overridden");
}
public Package getPackage() {
return this.pkg;
}
protected PackagePart getCorePart() {
return this.packagePart;
return getPackagePart();
}
/**

View File

@ -25,6 +25,7 @@ import org.apache.poi.util.POILogger;
import org.apache.poi.util.POILogFactory;
import org.openxml4j.exceptions.OpenXML4JException;
import org.openxml4j.opc.*;
import org.openxml4j.opc.Package;
/**
* Represents an entry of a OOXML package.
@ -38,7 +39,7 @@ import org.openxml4j.opc.*;
public class POIXMLDocumentPart {
private static POILogger logger = POILogFactory.getLogger(POIXMLDocumentPart.class);
public static XmlOptions DEFAULT_XML_OPTIONS;
public static final XmlOptions DEFAULT_XML_OPTIONS;
static {
DEFAULT_XML_OPTIONS = new XmlOptions();
DEFAULT_XML_OPTIONS.setSaveOuter();
@ -46,14 +47,46 @@ public class POIXMLDocumentPart {
DEFAULT_XML_OPTIONS.setSaveAggressiveNamespaces();
}
protected PackagePart packagePart;
protected PackageRelationship packageRel;
protected POIXMLDocumentPart parent;
private PackagePart packagePart;
private PackageRelationship packageRel;
private POIXMLDocumentPart parent;
private List<POIXMLDocumentPart> relations;
protected List<POIXMLDocumentPart> relations;
/**
* Construct POIXMLDocumentPart representing a "core document" package part.
*/
public POIXMLDocumentPart(Package pkg) {
try {
PackageRelationship coreRel = pkg.getRelationshipsByType(
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
this.relations = new LinkedList<POIXMLDocumentPart>();
this.packagePart = pkg.getPart(coreRel);
this.packageRel = coreRel;
} catch (OpenXML4JException e){
throw new POIXMLException(e);
}
}
/**
* Creates new POIXMLDocumentPart - called by client code to create new parts from scratch.
*
* @see #createRelationship(POIXMLRelation, POIXMLFactory, int, boolean)
*/
public POIXMLDocumentPart(){
this.relations = new LinkedList<POIXMLDocumentPart>();
}
/**
* Creates an POIXMLDocumentPart representing the given package part and relationship.
* Called by {@link #read(POIXMLFactory)} when reading in an exisiting file.
*
* @param part - The package part that holds xml data represenring this sheet.
* @param rel - the relationship of the given package part
* @see #read(POIXMLFactory)
*/
public POIXMLDocumentPart(PackagePart part, PackageRelationship rel){
relations = new LinkedList<POIXMLDocumentPart>();
this.relations = new LinkedList<POIXMLDocumentPart>();
this.packagePart = part;
this.packageRel = rel;
}
@ -63,7 +96,7 @@ public class POIXMLDocumentPart {
*
* @return the underlying PackagePart
*/
public PackagePart getPackagePart(){
public final PackagePart getPackagePart(){
return packagePart;
}
@ -72,7 +105,7 @@ public class POIXMLDocumentPart {
*
* @return the PackageRelationship that identifies this POIXMLDocumentPart
*/
public PackageRelationship getPackageRelationship(){
public final PackageRelationship getPackageRelationship(){
return packageRel;
}
@ -81,7 +114,7 @@ public class POIXMLDocumentPart {
*
* @return child relations
*/
public List<POIXMLDocumentPart> getRelations(){
public final List<POIXMLDocumentPart> getRelations(){
return relations;
}
@ -90,7 +123,7 @@ public class POIXMLDocumentPart {
*
* @param part the child to add
*/
protected void addRelation(POIXMLDocumentPart part){
protected final void addRelation(POIXMLDocumentPart part){
relations.add(part);
}
@ -99,7 +132,7 @@ public class POIXMLDocumentPart {
*
* @return the parent POIXMLDocumentPart or <code>null</code> for the root element.
*/
public POIXMLDocumentPart getParent(){
public final POIXMLDocumentPart getParent(){
return parent;
}
@ -132,11 +165,12 @@ public class POIXMLDocumentPart {
/**
* Save changes in the underlying OOXML package.
* Recursively fires {@link #commit()} for each package part
*/
protected void save() throws IOException{
protected final void onSave() throws IOException{
commit();
for(POIXMLDocumentPart p : relations){
p.save();
p.onSave();
}
}
@ -147,11 +181,11 @@ public class POIXMLDocumentPart {
* @param factory the factory that will create an instance of the requested relation
* @return the created child POIXMLDocumentPart
*/
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory){
protected final POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory){
return createRelationship(descriptor, factory, -1, false);
}
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx){
protected final POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx){
return createRelationship(descriptor, factory, idx, false);
}
@ -164,7 +198,7 @@ public class POIXMLDocumentPart {
* @param noRelation if true, then no relationship is added.
* @return the created child POIXMLDocumentPart
*/
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation){
protected final POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation){
try {
PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx));
@ -176,7 +210,6 @@ public class POIXMLDocumentPart {
doc.packageRel = rel;
doc.packagePart = part;
doc.parent = this;
doc.onDocumentCreate();
addRelation(doc);
return doc;
} catch (Exception e){
@ -190,7 +223,7 @@ public class POIXMLDocumentPart {
*
* @param factory the factory object that creates POIXMLFactory instances
*/
protected void read(POIXMLFactory factory) throws OpenXML4JException {
protected final void read(POIXMLFactory factory) throws OpenXML4JException {
PackageRelationshipCollection rels = packagePart.getRelationships();
for (PackageRelationship rel : rels) {
if(rel.getTargetMode() == TargetMode.INTERNAL){
@ -202,7 +235,6 @@ public class POIXMLDocumentPart {
}
POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);
childPart.parent = this;
childPart.onDocumentRead();
addRelation(childPart);
if(p.hasRelationships()) childPart.read(factory);
@ -210,17 +242,19 @@ public class POIXMLDocumentPart {
}
}
/**
* Fired when a new package part is created
*/
protected void onDocumentCreate(){
protected void onDocumentCreate() throws IOException {
}
/**
* Fired when a package part is read
*/
protected void onDocumentRead(){
protected void onDocumentRead() throws IOException{
}
}

View File

@ -34,11 +34,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CommentsDocument;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackageRelationship;
public class CommentsTable extends POIXMLDocumentPart implements CommentsSource {
public class CommentsTable extends POIXMLDocumentPart {
protected CTComments comments;
public CommentsTable() {
super(null, null);
super();
comments = CTComments.Factory.newInstance();
}
@ -56,16 +56,9 @@ public class CommentsTable extends POIXMLDocumentPart implements CommentsSource
}
}
public void writeTo(OutputStream out) throws IOException {
XmlOptions options = new XmlOptions();
options.setSaveOuter();
options.setUseDefaultNamespace();
// Requests use of whitespace for easier reading
//options.setSavePrettyPrint();
CommentsDocument doc = CommentsDocument.Factory.newInstance(options);
CommentsDocument doc = CommentsDocument.Factory.newInstance();
doc.setComments(comments);
doc.save(out, options);
doc.save(out, DEFAULT_XML_OPTIONS);
}
@Override

View File

@ -86,7 +86,7 @@ public class SharedStringsTable extends POIXMLDocumentPart {
private int uniqueCount;
public SharedStringsTable() {
super(null, null);
super();
}
public SharedStringsTable(PackagePart part, PackageRelationship rel) throws IOException {

View File

@ -64,8 +64,8 @@ import org.openxml4j.opc.PackageRelationship;
*
* @author ugo
*/
public class StylesTable extends POIXMLDocumentPart implements StylesSource {
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
public class StylesTable extends POIXMLDocumentPart {
private final Hashtable<Integer, String> numberFormats = new Hashtable<Integer,String>();
private final List<XSSFFont> fonts = new ArrayList<XSSFFont>();
private final List<XSSFCellFill> fills = new ArrayList<XSSFCellFill>();
private final List<XSSFCellBorder> borders = new ArrayList<XSSFCellBorder>();
@ -77,7 +77,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
/**
* The first style id available for use as a custom style
*/
public static final long FIRST_CUSTOM_STYLE_ID = 165;
public static final int FIRST_CUSTOM_STYLE_ID = 165;
private StyleSheetDocument doc;
@ -85,7 +85,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
* Create a new, empty StylesTable
*/
public StylesTable() {
super(null, null);
super();
doc = StyleSheetDocument.Factory.newInstance();
doc.addNewStyleSheet();
// Initialization required in order to make the document readable by MSExcel
@ -109,7 +109,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
// 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());
numberFormats.put((int)nfmt.getNumFmtId(), nfmt.getFormatCode());
}
if(doc.getStyleSheet().getFonts() != null){
int idx = 0;
@ -150,14 +150,14 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
// Start of style related getters and setters
// ===========================================================
public String getNumberFormatAt(long idx) {
public String getNumberFormatAt(int idx) {
return numberFormats.get(idx);
}
public synchronized long putNumberFormat(String fmt) {
public synchronized int 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();
for(Enumeration<Integer> keys = numberFormats.keys(); keys.hasMoreElements();) {
int key = keys.nextElement();
if(numberFormats.get(key).equals(fmt)) {
return key;
}
@ -166,7 +166,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
}
// Find a spare key, and add that
long newKey = FIRST_CUSTOM_STYLE_ID;
int newKey = FIRST_CUSTOM_STYLE_ID;
while(numberFormats.containsKey(newKey)) {
newKey++;
}
@ -174,11 +174,11 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
return newKey;
}
public XSSFFont getFontAt(long idx) {
public XSSFFont getFontAt(int idx) {
return fonts.get((int)idx);
}
public long putFont(Font font) {
public int putFont(Font font) {
int idx = fonts.indexOf(font);
if (idx != -1) {
return idx;
@ -187,7 +187,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
return fonts.size() - 1;
}
public XSSFCellStyle getStyleAt(long idx) {
public XSSFCellStyle getStyleAt(int idx) {
int styleXfId = 0;
// 0 is the empty default
@ -197,7 +197,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
return new XSSFCellStyle((int) idx, styleXfId, this);
}
public synchronized long putStyle(CellStyle style) {
public synchronized int putStyle(CellStyle style) {
XSSFCellStyle xStyle = (XSSFCellStyle)style;
CTXf mainXF = xStyle.getCoreXf();
@ -241,7 +241,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
return fills.size() - 1;
}
public CTXf getCellXfAt(long idx) {
public CTXf getCellXfAt(int idx) {
return xfs.get((int) idx);
}
public int putCellXf(CTXf cellXf) {
@ -249,7 +249,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
return xfs.size();
}
public CTXf getCellStyleXfAt(long idx) {
public CTXf getCellStyleXfAt(int idx) {
return styleXfs.get((int) idx);
}
public int putCellStyleXf(CTXf cellStyleXf) {
@ -328,7 +328,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
// Formats
CTNumFmts formats = CTNumFmts.Factory.newInstance();
formats.setCount(numberFormats.size());
for (Entry<Long, String> fmt : numberFormats.entrySet()) {
for (Entry<Integer, String> fmt : numberFormats.entrySet()) {
CTNumFmt ctFmt = formats.addNewNumFmt();
ctFmt.setNumFmtId(fmt.getKey());
ctFmt.setFormatCode(fmt.getValue());
@ -460,14 +460,14 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource {
return xssfFont;
}
public CTDxf getDxf(long idx) {
public CTDxf getDxf(int idx) {
if(dxfs.size()==0)
return CTDxf.Factory.newInstance();
else
return dxfs.get((int) idx);
}
public long putDxf(CTDxf dxf) {
public int putDxf(CTDxf dxf) {
this.dxfs.add(dxf);
return this.dxfs.size();
}

View File

@ -383,7 +383,7 @@ public final class XSSFCell implements Cell {
*/
public XSSFCellStyle getCellStyle() {
long idx = cell.isSetS() ? cell.getS() : 0;
return stylesSource.getStyleAt(idx);
return stylesSource.getStyleAt((int)idx);
}
/**

View File

@ -77,7 +77,7 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
/**
* Creates an empty Cell Style
*/
public XSSFCellStyle(StylesSource stylesSource) {
public XSSFCellStyle(StylesTable stylesSource) {
this.stylesSource = (StylesTable)stylesSource;
// We need a new CTXf for the main styles
// TODO decide on a style ctxf
@ -94,7 +94,7 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
* workbook (if they're not, it won't work)
* @throws IllegalArgumentException if there's a workbook mis-match
*/
public void verifyBelongsToStylesSource(StylesSource src) {
public void verifyBelongsToStylesSource(StylesTable src) {
if(this.stylesSource != src) {
throw new IllegalArgumentException("This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook?");
}

View File

@ -20,6 +20,7 @@ import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.usermodel.helpers.RichTextStringHelper;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.ss.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
@ -27,7 +28,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
public class XSSFComment implements Comment {
private CTComment comment;
private CommentsSource comments;
private CommentsTable comments;
/**
* Creates a new XSSFComment, associated with a given
@ -35,13 +36,13 @@ public class XSSFComment implements Comment {
* If, as an end user, you want a new XSSFComment
* object, the please ask your sheet for one.
*/
public XSSFComment(CommentsSource comments, CTComment comment) {
public XSSFComment(CommentsTable comments, CTComment comment) {
this.comment = comment;
this.comments = comments;
}
public String getAuthor() {
return comments.getAuthor(comment.getAuthorId());
return comments.getAuthor((int)comment.getAuthorId());
}
public int getColumn() {

View File

@ -18,22 +18,24 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.xssf.model.StylesTable;
/**
* 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 (short)stylesSource.putNumberFormat(format);
}
private StylesTable stylesSource;
public String getFormat(short index) {
return stylesSource.getNumberFormatAt((long)index);
}
protected XSSFDataFormat(StylesTable stylesSource) {
this.stylesSource = stylesSource;
}
public short getFormat(String format) {
return (short)stylesSource.putNumberFormat(format);
}
public String getFormat(short index) {
return stylesSource.getNumberFormatAt(index);
}
}

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.POIXMLException;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import org.openxml4j.opc.PackagePart;
@ -26,13 +27,14 @@ import org.openxml4j.opc.PackageRelationship;
import java.io.IOException;
//YK: TODO: this is only a prototype
public class XSSFDialogsheet extends XSSFSheet implements Sheet{
protected CTDialogsheet dialogsheet;
public XSSFDialogsheet(XSSFSheet sheet) {
this.packagePart = sheet.getPackagePart();
this.packageRel = sheet.getPackageRelationship();
protected XSSFDialogsheet(XSSFSheet sheet) {
super(sheet.getPackagePart(), sheet.getPackageRelationship());
this.dialogsheet = CTDialogsheet.Factory.newInstance();
this.worksheet = CTWorksheet.Factory.newInstance();
}
public XSSFRow createRow(int rowNum) {

View File

@ -19,7 +19,6 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlObject;
import org.openxml4j.opc.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*;
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
@ -29,8 +28,6 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
/**
* Represents a SpreadsheetML drawing
@ -48,8 +45,8 @@ public class XSSFDrawing extends POIXMLDocumentPart {
*
* @see org.apache.poi.xssf.usermodel.XSSFSheet#createDrawingPatriarch()
*/
public XSSFDrawing() {
super(null, null);
protected XSSFDrawing() {
super();
drawing = newDrawing();
}
@ -61,7 +58,7 @@ public class XSSFDrawing extends POIXMLDocumentPart {
* @param rel the package relationship holding this drawing,
* the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing
*/
public XSSFDrawing(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
protected XSSFDrawing(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
super(part, rel);
drawing = CTDrawing.Factory.parse(part.getInputStream());
}
@ -157,7 +154,7 @@ public class XSSFDrawing extends POIXMLDocumentPart {
XSSFWorkbook wb = (XSSFWorkbook)getParent().getParent();
XSSFPictureData data = wb.getAllPictures().get(pictureIndex);
PackagePartName ppName = data.getPackagePart().getPartName();
PackageRelationship rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, XSSFRelation.IMAGES.getRelation());
PackageRelationship rel = getPackagePart().addRelationship(ppName, TargetMode.INTERNAL, XSSFRelation.IMAGES.getRelation());
addRelation(new XSSFPictureData(data.getPackagePart(), rel));
return rel;
}

View File

@ -39,8 +39,10 @@ public class XSSFFactory extends POIXMLFactory {
}
private static final XSSFFactory inst = new XSSFFactory();
public static XSSFFactory getInstance(){
return new XSSFFactory();
return inst;
}
public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
@ -52,7 +54,7 @@ public class XSSFFactory extends POIXMLFactory {
try {
Class cls = descriptor.getRelationClass();
Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor(PackagePart.class, PackageRelationship.class);
Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(PackagePart.class, PackageRelationship.class);
return constructor.newInstance(part, rel);
} catch (Exception e){
throw new POIXMLException(e);
@ -62,7 +64,7 @@ public class XSSFFactory extends POIXMLFactory {
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
try {
Class cls = descriptor.getRelationClass();
Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor();
Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor();
return constructor.newInstance();
} catch (Exception e){
throw new POIXMLException(e);

View File

@ -23,7 +23,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFFirstFooter extends XSSFHeaderFooter implements Footer{
public XSSFFirstFooter(CTHeaderFooter headerFooter) {
protected XSSFFirstFooter(CTHeaderFooter headerFooter) {
super(headerFooter);
}

View File

@ -23,7 +23,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFFirstHeader extends XSSFHeaderFooter implements Header{
public XSSFFirstHeader(CTHeaderFooter headerFooter) {
protected XSSFFirstHeader(CTHeaderFooter headerFooter) {
super(headerFooter);
}

View File

@ -23,7 +23,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class XSSFOddHeader extends XSSFHeaderFooter implements Header{
public XSSFOddHeader(CTHeaderFooter headerFooter) {
protected XSSFOddHeader(CTHeaderFooter headerFooter) {
super(headerFooter);
}

View File

@ -52,8 +52,8 @@ public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
*
* @see org.apache.poi.xssf.usermodel.XSSFWorkbook#addPicture(byte[], int)
*/
public XSSFPictureData() {
super(null, null);
protected XSSFPictureData() {
super();
}
/**
@ -63,7 +63,7 @@ public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
* @param rel the package relationship holding this drawing,
* the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/image
*/
public XSSFPictureData(PackagePart part, PackageRelationship rel) {
protected XSSFPictureData(PackagePart part, PackageRelationship rel) {
super(part, rel);
}

View File

@ -31,7 +31,7 @@ public class XSSFPrintSetup implements PrintSetup {
private CTPageMargins pageMargins;
public XSSFPrintSetup(CTWorksheet worksheet) {
protected XSSFPrintSetup(CTWorksheet worksheet) {
this.ctWorksheet = worksheet;
this.pageSetup = ctWorksheet.getPageSetup() == null ? ctWorksheet.addNewPageSetup() : ctWorksheet.getPageSetup();
this.pageMargins = ctWorksheet.getPageMargins() == null ? ctWorksheet.addNewPageMargins() : ctWorksheet.getPageMargins();

View File

@ -16,11 +16,6 @@
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTAbsoluteAnchor;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTOneCellAnchor;
import org.openxmlformats.schemas.drawingml.x2006.chartDrawing.CTGroupShape;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
/**

View File

@ -18,11 +18,7 @@ package org.apache.poi.xssf.usermodel;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxml4j.opc.PackagePartName;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.TargetMode;
import java.util.List;
/**
* This object specifies a group shape that represents many shapes grouped together. This shape is to be treated
@ -43,7 +39,7 @@ public class XSSFShapeGroup extends XSSFShape {
* @param drawing the XSSFDrawing that owns this shape
* @param ctGroup the XML bean that stores this group content
*/
public XSSFShapeGroup(XSSFDrawing drawing, CTGroupShape ctGroup) {
protected XSSFShapeGroup(XSSFDrawing drawing, CTGroupShape ctGroup) {
this.drawing = drawing;
this.ctGroup = ctGroup;
}

View File

@ -24,16 +24,13 @@ import javax.xml.namespace.QName;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.Region;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.Control;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
@ -44,6 +41,7 @@ import org.apache.xmlbeans.XmlException;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxml4j.exceptions.InvalidFormatException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
@ -71,20 +69,30 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
protected CTSheet sheet;
protected CTWorksheet worksheet;
protected TreeMap<Integer, Row> rows;
protected List<XSSFHyperlink> hyperlinks;
protected ColumnHelper columnHelper;
private CommentsSource sheetComments;
private TreeMap<Integer, Row> rows;
private List<XSSFHyperlink> hyperlinks;
private ColumnHelper columnHelper;
private CommentsTable sheetComments;
public XSSFSheet() {
super(null, null);
this.worksheet = newSheet();
initialize();
/**
* Creates new XSSFSheet - called by XSSFWorkbook to create a sheet from scratch.
*
* @see org.apache.poi.xssf.usermodel.XSSFWorkbook#createSheet()
*/
protected XSSFSheet() {
super();
onDocumentCreate();
}
public XSSFSheet(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
/**
* Creates an XSSFSheet representing the given package part and relationship.
* Should only be called by XSSFWorkbook when reading in an exisiting file.
*
* @param part - The package part that holds xml data represenring this sheet.
* @param rel - the relationship of the given package part in the underlying OPC package
*/
protected XSSFSheet(PackagePart part, PackageRelationship rel) {
super(part, rel);
worksheet = WorksheetDocument.Factory.parse(part.getInputStream()).getWorksheet();
}
/**
@ -96,25 +104,81 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return (XSSFWorkbook)getParent();
}
protected void initialize(){
if (this.worksheet.getSheetData() == null) {
this.worksheet.addNewSheetData();
/**
* Initialize worksheet data when reading in an exisiting file.
*/
@Override
protected void onDocumentRead() {
try {
worksheet = WorksheetDocument.Factory.parse(getPackagePart().getInputStream()).getWorksheet();
} catch (XmlException e){
throw new POIXMLException(e);
} catch (IOException e){
throw new POIXMLException(e);
}
initRows(this.worksheet);
initColumns(this.worksheet);
initRows(worksheet);
columnHelper = new ColumnHelper(worksheet);
for(POIXMLDocumentPart p : getRelations()){
if(p instanceof CommentsTable) sheetComments = (CommentsTable)p;
}
hyperlinks = new ArrayList<XSSFHyperlink>();
// Process external hyperlinks for the sheet, if there are any
initHyperlinks();
}
/**
* Create a new CTWorksheet instance and setup default values
* Initialize worksheet data when creating a new sheet.
*/
@Override
protected void onDocumentCreate(){
worksheet = newSheet();
initRows(worksheet);
columnHelper = new ColumnHelper(worksheet);
hyperlinks = new ArrayList<XSSFHyperlink>();
}
private void initRows(CTWorksheet worksheet) {
rows = new TreeMap<Integer, Row>();
for (CTRow row : worksheet.getSheetData().getRowArray()) {
XSSFRow r = new XSSFRow(row, this);
rows.put(r.getRowNum(), r);
}
}
/**
* Read hyperlink relations, link them with CTHyperlink beans in this worksheet
* and initialize the internal array of XSSFHyperlink objects
*/
private void initHyperlinks() {
hyperlinks = new ArrayList<XSSFHyperlink>();
if(!worksheet.isSetHyperlinks()) return;
try {
PackageRelationshipCollection hyperRels =
getPackagePart().getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
// Turn each one into a XSSFHyperlink
for(CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
PackageRelationship hyperRel = null;
if(hyperlink.getId() != null) {
hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
}
hyperlinks.add( new XSSFHyperlink(hyperlink, hyperRel) );
}
} catch (InvalidFormatException e){
throw new POIXMLException(e);
}
}
/**
* Create a new CTWorksheet instance with all values set to defaults
*
* @return a new instance
*/
protected static CTWorksheet newSheet(){
private static CTWorksheet newSheet(){
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
CTSheetFormatPr ctFormat = worksheet.addNewSheetFormatPr();
ctFormat.setDefaultRowHeight(15.0);
@ -137,17 +201,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return worksheet;
}
public List<Control> getControls()
{
return null;
}
/**
* Provide access to the underlying XML bean
* Provide access to the CTWorksheet bean holding this sheet's data
*
* @return the underlying CTWorksheet bean
* @return the CTWorksheet bean holding this sheet's data
*/
public CTWorksheet getWorksheet() {
public CTWorksheet getCTWorksheet() {
return this.worksheet;
}
@ -155,48 +214,16 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return columnHelper;
}
protected void initRows(CTWorksheet worksheet) {
this.rows = new TreeMap<Integer, Row>();
for (CTRow row : worksheet.getSheetData().getRowArray()) {
XSSFRow r = new XSSFRow(row, this);
this.rows.put(r.getRowNum(), r);
}
}
protected void initColumns(CTWorksheet worksheet) {
columnHelper = new ColumnHelper(worksheet);
}
protected void initHyperlinks(PackageRelationshipCollection hyperRels) {
if(worksheet.getHyperlinks() == null) return;
// Turn each one into a XSSFHyperlink
for(CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
PackageRelationship hyperRel = null;
if(hyperlink.getId() != null) {
hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
}
hyperlinks.add(
new XSSFHyperlink(hyperlink, hyperRel)
);
}
}
protected CTSheet getSheet() {
return this.sheet;
}
/**
* Sdds a merged region of cells (hence those cells form one)
*
* @param cra (rowfrom/colfrom-rowto/colto) to merge
* @return index of this region
*/
public int addMergedRegion(CellRangeAddress cra) {
Region r = new Region(cra.getFirstRow(), (short)cra.getFirstColumn(),
cra.getLastRow(), (short)cra.getLastColumn());
return addMergedRegion(r);
}
public int addMergedRegion(Region region) {
CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells();
CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
ctMergeCell.setRef(region.getRegionRef());
ctMergeCell.setRef(cra.formatAsString());
return ctMergeCells.sizeOfMergeCellArray();
}
@ -215,11 +242,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
/**
* Adjusts the column width to fit the contents.
*
* <p>
* This process can be relatively slow on large sheets, so this should
* normally only be called once per column, at the end of your
* processing.
*
* </p>
* You can specify whether the content of merged cells should be considered or ignored.
* Default is to ignore merged cells.
*
@ -305,7 +332,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
if (sheetComments == null) {
sheetComments = (CommentsTable)createRelationship(XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId());
}
return (XSSFComment)sheetComments.addComment();
return sheetComments.addComment();
}
/**
@ -341,19 +368,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
getPane().setActivePane(STPane.Enum.forInt(activePane));
}
public boolean getAlternateExpression() {
// TODO Auto-generated method stub
return false;
}
public boolean getAlternateFormula() {
// TODO Auto-generated method stub
return false;
}
public XSSFComment getCellComment(int row, int column) {
if (sheetComments == null) return null;
else return (XSSFComment)sheetComments.findCellComment(row, column);
else return sheetComments.findCellComment(row, column);
}
public XSSFHyperlink getHyperlink(int row, int column) {
@ -480,13 +497,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* @return the number of the first logical row on the sheet, zero based
*/
public int getFirstRowNum() {
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row row = it.next();
if (row != null) {
return row.getRowNum();
}
}
return -1;
return rows.size() == 0 ? -1 : rows.firstKey();
}
/**
@ -602,14 +613,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
public int getLastRowNum() {
int lastRowNum = -1;
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row row = it.next();
if (row != null) {
lastRowNum = row.getRowNum();
}
}
return lastRowNum;
return rows.size() == 0 ? -1 : rows.lastKey();
}
public short getLeftCol() {
@ -618,43 +622,74 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return cellReference.getCol();
}
/**
* Gets the size of the margin in inches.
*
* @param margin which margin to get
* @return the size of the margin
* @see Sheet#LeftMargin
* @see Sheet#RightMargin
* @see Sheet#TopMargin
* @see Sheet#BottomMargin
* @see Sheet#HeaderMargin
* @see Sheet#FooterMargin
*/
public double getMargin(short margin) {
CTPageMargins pageMargins = getSheetTypePageMargins();
if (!worksheet.isSetPageMargins()) return 0;
CTPageMargins pageMargins = worksheet.getPageMargins();
switch (margin) {
case LeftMargin:
return pageMargins.getLeft();
case RightMargin:
return pageMargins.getRight();
case TopMargin:
return pageMargins.getTop();
case BottomMargin:
return pageMargins.getBottom();
case HeaderMargin:
return pageMargins.getHeader();
case FooterMargin:
return pageMargins.getFooter();
default :
throw new POIXMLException( "Unknown margin constant: " + margin );
case LeftMargin:
return pageMargins.getLeft();
case RightMargin:
return pageMargins.getRight();
case TopMargin:
return pageMargins.getTop();
case BottomMargin:
return pageMargins.getBottom();
case HeaderMargin:
return pageMargins.getHeader();
case FooterMargin:
return pageMargins.getFooter();
default :
throw new POIXMLException("Unknown margin constant: " + margin);
}
}
protected CTPageMargins getSheetTypePageMargins() {
if (worksheet.getPageMargins() == null) {
worksheet.setPageMargins(CTPageMargins.Factory.newInstance());
/**
* Sets the size of the margin in inches.
*
* @param margin which margin to get
* @param size the size of the margin
* @see Sheet#LeftMargin
* @see Sheet#RightMargin
* @see Sheet#TopMargin
* @see Sheet#BottomMargin
* @see Sheet#HeaderMargin
* @see Sheet#FooterMargin
*/
public void setMargin(short margin, double size) {
CTPageMargins pageMargins = worksheet.isSetPageMargins() ?
worksheet.getPageMargins() : worksheet.addNewPageMargins();
switch (margin) {
case LeftMargin:
pageMargins.setLeft(size);
case RightMargin:
pageMargins.setRight(size);
case TopMargin:
pageMargins.setTop(size);
case BottomMargin:
pageMargins.setBottom(size);
case HeaderMargin:
pageMargins.setHeader(size);
case FooterMargin:
pageMargins.setFooter(size);
}
return worksheet.getPageMargins();
}
public Region getMergedRegionAt(int index) {
CTMergeCells ctMergeCells = worksheet.getMergeCells();
if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions");
CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index);
return new Region(ctMergeCell.getRef());
}
/**
* @return the merged region at the specified index
* @throws IllegalStateException if this worksheet does not contain merged regions
*/
public CellRangeAddress getMergedRegion(int index) {
CTMergeCells ctMergeCells = worksheet.getMergeCells();
@ -667,6 +702,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return new CellRangeAddress(cell1.getRow(), cell2.getRow(), cell1.getCol(), cell2.getCol());
}
/**
* Returns the number of merged regions defined in this worksheet
*
* @return number of merged regions in this worksheet
*/
public int getNumMergedRegions() {
CTMergeCells ctMergeCells = worksheet.getMergeCells();
return ctMergeCells == null ? 0 : ctMergeCells.sizeOfMergeCellArray();
@ -676,38 +716,36 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return hyperlinks.size();
}
public boolean getObjectProtect() {
// TODO Auto-generated method stub
return false;
}
public PaneInformation getPaneInformation() {
// TODO Auto-generated method stub
return null;
}
public short getPassword() {
// TODO Auto-generated method stub
return 0;
}
/**
* Returns the number of phsyically defined rows (NOT the number of rows in the sheet)
*
* @return the number of phsyically defined rows
*/
public int getPhysicalNumberOfRows() {
int counter = 0;
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
if (it.next() != null) {
counter++;
}
}
return counter;
return rows.size();
}
/**
* Gets the print setup object.
*
* @return The user model for the print setup object.
*/
public XSSFPrintSetup getPrintSetup() {
return new XSSFPrintSetup(getWorksheet());
return new XSSFPrintSetup(worksheet);
}
/**
* Answer whether protection is enabled or disabled
*
* @return true => protection enabled; false => protection disabled
*/
public boolean getProtect() {
// TODO Auto-generated method stub
return false;
return worksheet.isSetSheetProtection() && worksheet.getSheetProtection().getSheet();
}
/**
@ -757,8 +795,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
public boolean getRowSumsBelow() {
CTSheetPr sheetPr = worksheet.getSheetPr();
CTOutlinePr outlinePr = (sheetPr != null && sheetPr.isSetOutlinePr())
? sheetPr.getOutlinePr() : CTOutlinePr.Factory.newInstance();
return outlinePr.getSummaryBelow();
? sheetPr.getOutlinePr() : null;
return outlinePr == null || outlinePr.getSummaryBelow();
}
/**
@ -821,8 +859,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
*/
private CTOutlinePr ensureOutlinePr(){
CTSheetPr sheetPr = worksheet.isSetSheetPr() ? worksheet.getSheetPr() : worksheet.addNewSheetPr();
CTOutlinePr outlinePr = sheetPr.isSetOutlinePr() ? sheetPr.getOutlinePr() : sheetPr.addNewOutlinePr();
return outlinePr;
return sheetPr.isSetOutlinePr() ? sheetPr.getOutlinePr() : sheetPr.addNewOutlinePr();
}
/**
@ -831,14 +868,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* @return true => protection enabled; false => protection disabled
*/
public boolean getScenarioProtect() {
return getSheetTypeProtection().getScenarios();
}
protected CTSheetProtection getSheetTypeProtection() {
if (worksheet.getSheetProtection() == null) {
worksheet.setSheetProtection(CTSheetProtection.Factory.newInstance());
}
return worksheet.getSheetProtection();
return worksheet.isSetSheetProtection() && worksheet.getSheetProtection().getScenarios();
}
/**
@ -1015,11 +1045,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
}
public void protectSheet(String password) {
// TODO Auto-generated method stub
}
/**
* Removes a merged region of cells (hence letting them free)
*
@ -1040,8 +1065,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
ctMergeCells.setMergeCellArray(mergeCellsArray);
}
/**
* Remove a row from this sheet. All cells contained in the row are removed as well
*
* @param row the row to remove.
*/
public void removeRow(Row row) {
rows.remove(row.getRowNum());
}
@ -1227,24 +1256,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
getSheetTypePrintOptions().setHorizontalCentered(value);
}
public void setMargin(short margin, double size) {
CTPageMargins pageMargins = getSheetTypePageMargins();
switch (margin) {
case LeftMargin:
pageMargins.setLeft(size);
case RightMargin:
pageMargins.setRight(size);
case TopMargin:
pageMargins.setTop(size);
case BottomMargin:
pageMargins.setBottom(size);
case HeaderMargin:
pageMargins.setHeader(size);
case FooterMargin:
pageMargins.setFooter(size);
}
}
public void setPrintGridlines(boolean newPrintGridlines) {
getSheetTypePrintOptions().setGridLines(newPrintGridlines);
}
@ -1474,7 +1485,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
getSheetTypeSheetFormatPr().setOutlineLevelCol(maxLevelCol);
}
protected CTSheetViews getSheetTypeSheetViews() {
private CTSheetViews getSheetTypeSheetViews() {
if (worksheet.getSheetViews() == null) {
worksheet.setSheetViews(CTSheetViews.Factory.newInstance());
worksheet.getSheetViews().addNewSheetView();
@ -1560,6 +1571,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
if(sheetComments == null) { return false; }
return (sheetComments.getNumberOfComments() > 0);
}
protected int getNumberOfComments() {
if(sheetComments == null) { return 0; }
return sheetComments.getNumberOfComments();
@ -1593,7 +1605,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* Returns the sheet's comments object if there is one,
* or null if not
*/
protected CommentsSource getCommentsSourceIfExists() {
protected CommentsTable getCommentsSourceIfExists() {
return sheetComments;
}

View File

@ -17,7 +17,6 @@
package org.apache.poi.xssf.usermodel;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
/**
* Represents a text box in a SpreadsheetML drawing.

View File

@ -37,8 +37,8 @@ import org.apache.poi.xssf.model.*;
import org.apache.poi.POIXMLException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlException;
import org.openxml4j.exceptions.OpenXML4JException;
import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.opc.*;
import org.openxml4j.opc.Package;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
@ -104,26 +104,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
/**
* The embedded OLE2 files in the OPC package
*/
private List<PackagePart> embedds;
/**
* Create a new SpreadsheetML workbook.
*/
public XSSFWorkbook() {
super();
onDocumentCreate();
}
/**
* Constructs a XSSFWorkbook object given a file name.
*
* @param path the file name.
*/
public XSSFWorkbook(String path) throws IOException {
this(openPackage(path));
super(newPackage());
onWorkbookCreate();
}
/**
@ -134,9 +120,25 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*/
public XSSFWorkbook(Package pkg) throws IOException {
super(ensureWriteAccess(pkg));
//build a tree of POIXMLDocumentParts, this workbook being the root
try {
read(XSSFFactory.getInstance());
} catch (OpenXML4JException e){
throw new POIXMLException(e);
}
onDocumentRead();
}
/**
* Constructs a XSSFWorkbook object given a file name.
*
* @param path the file name.
*/
public XSSFWorkbook(String path) throws IOException {
this(openPackage(path));
}
/**
* YK: current implementation of OpenXML4J is funny.
* Packages opened by Package.open(InputStream is) are read-only,
@ -154,19 +156,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
return pkg;
}
/**
* Initialize this workbook from the specified Package
*/
@Override
protected void onDocumentRead() {
protected void onDocumentRead() throws IOException {
try {
//build the POIXMLDocumentPart tree, this workbook is the root
read(XSSFFactory.getInstance());
WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream());
this.workbook = doc.getWorkbook();
HashMap<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
for(POIXMLDocumentPart p : getRelations()){
if(p instanceof SharedStringsTable) sharedStringSource = (SharedStringsTable)p;
else if(p instanceof StylesTable) stylesSource = (StylesTable)p;
@ -175,34 +171,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
}
// Load individual sheets
sheets = new LinkedList<XSSFSheet>();
embedds = new LinkedList<PackagePart>();
// Load individual sheets. The order of sheets is defined by the order of CTSheet beans in the workbook
sheets = new ArrayList<XSSFSheet>(shIdMap.size());
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
String id = ctSheet.getId();
XSSFSheet sh = shIdMap.get(id);
sh.sheet = ctSheet;
XSSFSheet sh = shIdMap.get(ctSheet.getId());
if(sh == null) {
log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping");
continue;
}
//initialize internal arrays of rows and columns
sh.initialize();
PackagePart sheetPart = sh.getPackagePart();
// Process external hyperlinks for the sheet,
// if there are any
PackageRelationshipCollection hyperlinkRels =
sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
sh.initHyperlinks(hyperlinkRels);
// Get the embeddings for the workbook
for(PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel)); // TODO: Add this reference to each sheet as well
for(PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
sh.sheet = ctSheet;
sh.onDocumentRead();
sheets.add(sh);
}
@ -212,20 +190,22 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
// Process the named ranges
namedRanges = new LinkedList<XSSFName>();
namedRanges = new ArrayList<XSSFName>();
if(workbook.getDefinedNames() != null) {
for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
namedRanges.add(new XSSFName(ctName, this));
}
}
} catch (Exception e) {
} catch (XmlException e) {
throw new POIXMLException(e);
}
}
@Override
protected void onDocumentCreate() {
/**
* Create a new CTWorkbook with all values set to default
*/
private void onWorkbookCreate() {
workbook = CTWorkbook.Factory.newInstance();
CTBookViews bvs = workbook.addNewBookViews();
CTBookView bv = bvs.addNewWorkbookView();
@ -235,15 +215,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
namedRanges = new LinkedList<XSSFName>();
sheets = new LinkedList<XSSFSheet>();
embedds = new LinkedList<PackagePart>();
namedRanges = new ArrayList<XSSFName>();
sheets = new ArrayList<XSSFSheet>();
}
/**
* Create a new SpreadsheetML package and setup the default minimal content
*/
protected Package newPackage() throws IOException {
protected static Package newPackage() {
try {
Package pkg = Package.create(PackageHelper.createTempFile());
// Main part
@ -256,7 +235,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
pkg.getPackageProperties().setCreatorProperty("Apache POI");
return pkg;
} catch (InvalidFormatException e){
} catch (Exception e){
throw new POIXMLException(e);
}
}
@ -266,7 +245,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*
* @return the underlying CTWorkbook bean
*/
public CTWorkbook getWorkbook() {
public CTWorkbook getCTWorkbook() {
return this.workbook;
}
@ -345,7 +324,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
XSSFSheet clonedSheet = createSheet(name);
clonedSheet.worksheet.set(srcSheet.worksheet);
clonedSheet.getCTWorksheet().set(srcSheet.getCTWorksheet());
return clonedSheet;
}
@ -380,7 +359,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
/**
* create a new Font and add it to the workbook's font table
* Create a new Font and add it to the workbook's font table
*
* @return new font object
*/
@ -391,7 +370,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
/**
* Creates a new named range and add it to the model
* Create a new named range and add it to the workbook's names table
*
* @return named range high level
*/
@ -402,7 +381,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
/**
* create an XSSFSheet for this workbook, adds it to the sheets and returns
* Create an XSSFSheet for this workbook, adds it to the sheets and returns
* the high level representation. Use this to create new sheets.
*
* @return XSSFSheet representing the new sheet.
@ -413,25 +392,26 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
/**
* create an XSSFSheet for this workbook, adds it to the sheets and returns
* Create an XSSFSheet for this workbook, adds it to the sheets and returns
* the high level representation. Use this to create new sheets.
*
* @param sheetname sheetname to set for the sheet, can't be duplicate, greater than 31 chars or contain /\?*[]
* @return XSSFSheet representing the new sheet.
* @throws IllegalArgumentException if the sheetname is invalid or the workbook already contains a sheet of this name
*/
public XSSFSheet createSheet(String sheetname) {
if (containsSheet( sheetname, sheets.size() ))
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
CTSheet sheet = addSheet(sheetname);
int sheetNumber = getNumberOfSheets() + 1;
XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
CTSheet sheet = addSheet(sheetname);
wrapper.sheet = sheet;
sheet.setId(wrapper.getPackageRelationship().getId());
sheet.setSheetId(sheetNumber);
if(sheets.size() == 0) wrapper.setSelected(true);
this.sheets.add(wrapper);
sheets.add(wrapper);
return wrapper;
}
@ -479,23 +459,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
public int getActiveSheetIndex() {
//activeTab (Active Sheet Index) Specifies an unsignedInt
//that contains the index to the active sheet in this book view.
Long index = workbook.getBookViews().getWorkbookViewArray(0).getActiveTab();
return index.intValue();
}
/**
* Gets all embedded OLE2 objects from the Workbook.
*
* @return the list of embedded objects (a list of {@link org.openxml4j.opc.PackagePart} objects.)
*/
public List getAllEmbeddedObjects() {
return embedds;
return (int)workbook.getBookViews().getWorkbookViewArray(0).getActiveTab();
}
/**
* Gets all pictures from the Workbook.
*
* @return the list of pictures (a list of {@link XSSFPictureData} objects.)
* @see #addPicture(byte[], int)
*/
public List<XSSFPictureData> getAllPictures() {
if(pictures == null) {
@ -642,10 +613,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @return XSSFSheet with the name provided or <code>null</code> if it does not exist
*/
public XSSFSheet getSheet(String name) {
CTSheet[] sheets = this.workbook.getSheets().getSheetArray();
for (int i = 0 ; i < sheets.length ; ++i) {
if (name.equals(sheets[i].getName())) {
return this.sheets.get(i);
CTSheet[] ctSheets = this.workbook.getSheets().getSheetArray();
for (int i = 0 ; i < ctSheets.length ; ++i) {
if (name.equalsIgnoreCase(ctSheets[i].getName())) {
return sheets.get(i);
}
}
return null;
@ -656,22 +627,24 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*
* @param index of the sheet number (0-based physical & logical)
* @return XSSFSheet at the provided index
* @throws IllegalArgumentException if the index is out of range (index
* &lt; 0 || index &gt;= getNumberOfSheets()).
*/
public XSSFSheet getSheetAt(int index) {
validateSheetIndex(index);
return this.sheets.get(index);
return sheets.get(index);
}
/**
* Returns the index of the sheet by his name
* Returns the index of the sheet by his name (case insensitive match)
*
* @param name the sheet name
* @return index of the sheet (0 based)
* @return index of the sheet (0 based) or <tt>-1</tt if not found
*/
public int getSheetIndex(String name) {
CTSheet[] sheets = this.workbook.getSheets().getSheetArray();
for (int i = 0 ; i < sheets.length ; ++i) {
if (name.equals(sheets[i].getName())) {
if (name.equalsIgnoreCase(sheets[i].getName())) {
return i;
}
}
@ -1034,7 +1007,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*/
public void write(OutputStream stream) throws IOException {
//force all children to commit their changes into the underlying OOXML Package
save();
onSave();
getPackage().save(stream);
}
@ -1147,6 +1120,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* Get the document's embedded files.
*/
public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
List<PackagePart> embedds = new LinkedList<PackagePart>();
for(XSSFSheet sheet : sheets){
// Get the embeddings for the workbook
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
}
return embedds;
}
}

View File

@ -118,8 +118,8 @@ public class TestStylesTable extends TestCase {
assertEquals(1, st._getStyleXfsSize());
assertEquals(0, st._getNumberFormatSize());
long nf1 = st.putNumberFormat("yyyy-mm-dd");
long nf2 = st.putNumberFormat("yyyy-mm-DD");
int nf1 = st.putNumberFormat("yyyy-mm-dd");
int nf2 = st.putNumberFormat("yyyy-mm-DD");
assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
st.putStyle(new XSSFCellStyle(st));
@ -146,8 +146,8 @@ public class TestStylesTable extends TestCase {
assertEquals(1, st._getStyleXfsSize());
assertEquals(8, st._getNumberFormatSize());
long nf1 = st.putNumberFormat("YYYY-mm-dd");
long nf2 = st.putNumberFormat("YYYY-mm-DD");
int nf1 = st.putNumberFormat("YYYY-mm-dd");
int nf2 = st.putNumberFormat("YYYY-mm-DD");
assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();

View File

@ -369,7 +369,7 @@ public final class TestXSSFCell extends TestCase {
Cell cell = sheet.createRow(0).createCell((short)0);
cell.setAsActiveCell();
assertEquals("A1", sheet.getWorksheet().getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
assertEquals("A1", sheet.getCTWorksheet().getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
}

View File

@ -22,7 +22,6 @@ import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
public class TestXSSFDialogSheet extends TestCase {
@ -51,7 +50,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetSetAutoBreaks() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(sheet.getAutobreaks());
sheet.setAutobreaks(false);
assertFalse(sheet.getAutobreaks());
@ -59,7 +58,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testIsSetFitToPage() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getFitToPage());
sheet.setFitToPage(true);
assertTrue(sheet.getFitToPage());
@ -69,7 +68,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetSetMargin() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertEquals((double) 0, sheet.getMargin((short) 0));
sheet.setMargin((short) 0, 10);
assertEquals((double) 10, sheet.getMargin((short) 0));
@ -111,7 +110,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetFooter() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertNotNull(sheet.getFooter());
sheet.getFooter().setCenter("test center footer");
assertEquals("test center footer", sheet.getFooter().getCenter());
@ -119,7 +118,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetAllHeadersFooters() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertNotNull(sheet);
assertNotNull(sheet.getOddFooter());
assertNotNull(sheet.getEvenFooter());
@ -156,7 +155,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetSetHorizontallyCentered() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getHorizontallyCenter());
sheet.setHorizontallyCenter(true);
assertTrue(sheet.getHorizontallyCenter());
@ -166,7 +165,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetSetVerticallyCentered() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getVerticallyCenter());
sheet.setVerticallyCenter(true);
assertTrue(sheet.getVerticallyCenter());
@ -176,7 +175,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testIsSetPrintGridlines() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.isPrintGridlines());
sheet.setPrintGridlines(true);
assertTrue(sheet.isPrintGridlines());
@ -184,7 +183,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testIsSetDisplayFormulas() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.isDisplayFormulas());
sheet.setDisplayFormulas(true);
assertTrue(sheet.isDisplayFormulas());
@ -192,7 +191,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testIsSetDisplayGridLines() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(sheet.isDisplayGridlines());
sheet.setDisplayGridlines(false);
assertFalse(sheet.isDisplayGridlines());
@ -200,7 +199,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testIsSetDisplayRowColHeadings() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(sheet.isDisplayRowColHeadings());
sheet.setDisplayRowColHeadings(false);
assertFalse(sheet.isDisplayRowColHeadings());
@ -208,7 +207,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetScenarioProtect() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = (XSSFDialogsheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getScenarioProtect());
}

View File

@ -42,8 +42,8 @@ public class TestXSSFDrawing extends TestCase {
String drawingId = drawing.getPackageRelationship().getId();
//there should be a relation to this drawing in the worksheet
assertTrue(sheet.getWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getWorksheet().getDrawing().getId());
assertTrue(sheet.getCTWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
}
@ -63,8 +63,8 @@ public class TestXSSFDrawing extends TestCase {
String drawingId = drawing.getPackageRelationship().getId();
//there should be a relation to this drawing in the worksheet
assertTrue(sheet.getWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getWorksheet().getDrawing().getId());
assertTrue(sheet.getCTWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
}
public void testMultipleDrawings(){

View File

@ -25,6 +25,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.Region;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
@ -404,7 +405,7 @@ public class TestXSSFSheet extends TestCase {
// Now check the low level stuff, and check that's all
// been set correctly
XSSFSheet xs = sheet;
CTWorksheet cts = xs.getWorksheet();
CTWorksheet cts = xs.getCTWorksheet();
CTCols[] cols_s = cts.getColsArray();
assertEquals(1, cols_s.length);
@ -621,7 +622,7 @@ public class TestXSSFSheet extends TestCase {
sheet.setCellComment("A1", comment);
assertEquals("A1", ctComments.getCommentList().getCommentArray(0).getRef());
comment.setAuthor("test A1 author");
assertEquals("test A1 author", comments.getAuthor(ctComments.getCommentList().getCommentArray(0).getAuthorId()));
assertEquals("test A1 author", comments.getAuthor((int)ctComments.getCommentList().getCommentArray(0).getAuthorId()));
}
public void testGetActiveCell() {
@ -636,7 +637,7 @@ public class TestXSSFSheet extends TestCase {
public void testCreateFreezePane() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getWorksheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
sheet.createFreezePane(2, 4);
assertEquals((double)2, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getXSplit());
@ -653,17 +654,16 @@ public class TestXSSFSheet extends TestCase {
public void testNewMergedRegionAt() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
Region region = new Region("B2:D4");
CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
sheet.addMergedRegion(region);
assertEquals("B2:D4", sheet.getMergedRegionAt(0).getRegionRef());
assertEquals("B2:D4", sheet.getMergedRegion(0).formatAsString());
}
public void testGetNumMergedRegions() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getWorksheet();
assertEquals(0, sheet.getNumMergedRegions());
Region region = new Region("B2:D4");
CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
sheet.addMergedRegion(region);
assertEquals(1, sheet.getNumMergedRegions());
}
@ -671,10 +671,10 @@ public class TestXSSFSheet extends TestCase {
public void testRemoveMergedRegion() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getWorksheet();
Region region_1 = new Region("A1:B2");
Region region_2 = new Region("C3:D4");
Region region_3 = new Region("E5:F6");
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
CellRangeAddress region_1 = CellRangeAddress.valueOf("A1:B2");
CellRangeAddress region_2 = CellRangeAddress.valueOf("C3:D4");
CellRangeAddress region_3 = CellRangeAddress.valueOf("E5:F6");
sheet.addMergedRegion(region_1);
sheet.addMergedRegion(region_2);
sheet.addMergedRegion(region_3);
@ -691,7 +691,7 @@ public class TestXSSFSheet extends TestCase {
public void testSetDefaultColumnStyle() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getWorksheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
StylesTable stylesTable = workbook.getStylesSource();
XSSFFont font = new XSSFFont();
font.setFontName("Cambria");
@ -750,7 +750,7 @@ public class TestXSSFSheet extends TestCase {
//one level
sheet.groupColumn((short)2,(short)7);
sheet.groupColumn((short)10,(short)11);
CTCols cols=sheet.getWorksheet().getColsArray(0);
CTCols cols=sheet.getCTWorksheet().getColsArray(0);
assertEquals(2,cols.sizeOfColArray());
CTCol[]colArray=cols.getColArray();
assertNotNull(colArray);
@ -760,7 +760,7 @@ public class TestXSSFSheet extends TestCase {
//two level
sheet.groupColumn((short)1,(short)2);
cols=sheet.getWorksheet().getColsArray(0);
cols=sheet.getCTWorksheet().getColsArray(0);
assertEquals(4,cols.sizeOfColArray());
colArray=cols.getColArray();
assertEquals(2, colArray[1].getOutlineLevel());
@ -768,7 +768,7 @@ public class TestXSSFSheet extends TestCase {
//three level
sheet.groupColumn((short)6,(short)8);
sheet.groupColumn((short)2,(short)3);
cols=sheet.getWorksheet().getColsArray(0);
cols=sheet.getCTWorksheet().getColsArray(0);
assertEquals(7,cols.sizeOfColArray());
colArray=cols.getColArray();
assertEquals(3, colArray[1].getOutlineLevel());
@ -792,7 +792,7 @@ public class TestXSSFSheet extends TestCase {
//one level
sheet.groupRow(9,10);
assertEquals(2,sheet.rows.size());
assertEquals(2,sheet.getPhysicalNumberOfRows());
CTRow ctrow = sheet.getRow(8).getCTRow();
assertNotNull(ctrow);
@ -802,7 +802,7 @@ public class TestXSSFSheet extends TestCase {
//two level
sheet.groupRow(10,13);
assertEquals(5,sheet.rows.size());
assertEquals(5,sheet.getPhysicalNumberOfRows());
ctrow = sheet.getRow(9).getCTRow();
assertNotNull(ctrow);
assertEquals(10,ctrow.getR());
@ -811,11 +811,11 @@ public class TestXSSFSheet extends TestCase {
sheet.ungroupRow(8, 10);
assertEquals(4,sheet.rows.size());
assertEquals(4,sheet.getPhysicalNumberOfRows());
assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
sheet.ungroupRow(10,10);
assertEquals(3,sheet.rows.size());
assertEquals(3,sheet.getPhysicalNumberOfRows());
assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
}

View File

@ -54,6 +54,8 @@ public final class TestXSSFWorkbook extends TestCase {
public void testGetSetActiveSheet(){
XSSFWorkbook workbook = new XSSFWorkbook();
assertEquals(0, workbook.getActiveSheetIndex());
workbook.createSheet("sheet1");
workbook.createSheet("sheet2");
workbook.createSheet("sheet3");
@ -84,7 +86,7 @@ public final class TestXSSFWorkbook extends TestCase {
assertSame(sheet2, workbook.getSheetAt(0));
assertSame(sheet1, workbook.getSheetAt(1));
// Test reordering of CTSheets
CTWorkbook ctwb = workbook.getWorkbook();
CTWorkbook ctwb = workbook.getCTWorkbook();
CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
assertEquals("sheet2", ctsheets[0].getName());
assertEquals("sheet1", ctsheets[1].getName());
@ -334,19 +336,19 @@ public final class TestXSSFWorkbook extends TestCase {
assertNotNull(cellStyleAt);
//get custom style
StylesSource styleSource = workbook.getStylesSource();
StylesTable styleSource = workbook.getStylesSource();
CellStyle customStyle = new XSSFCellStyle(styleSource);
Font font = new XSSFFont();
font.setFontName("Verdana");
customStyle.setFont(font);
Long x = styleSource.putStyle(customStyle);
cellStyleAt = workbook.getCellStyleAt(x.shortValue());
int x = styleSource.putStyle(customStyle);
cellStyleAt = workbook.getCellStyleAt((short)x);
assertNotNull(cellStyleAt);
}
public void testGetFontAt(){
XSSFWorkbook workbook = new XSSFWorkbook();
StylesSource styleSource = workbook.getStylesSource();
StylesTable styleSource = workbook.getStylesSource();
short i = 0;
//get default font
Font fontAt = workbook.getFontAt(i);
@ -355,8 +357,8 @@ public final class TestXSSFWorkbook extends TestCase {
//get customized font
Font customFont = new XSSFFont();
customFont.setItalic(true);
Long x = styleSource.putFont(customFont);
fontAt = workbook.getFontAt(x.shortValue());
int x = styleSource.putFont(customFont);
fontAt = workbook.getFontAt((short)x);
assertNotNull(fontAt);
}
@ -432,7 +434,7 @@ public final class TestXSSFWorkbook extends TestCase {
public void testStyles() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
StylesSource ss = workbook.getStylesSource();
StylesTable ss = workbook.getStylesSource();
assertNotNull(ss);
assertTrue(ss instanceof StylesTable);
StylesTable st = (StylesTable)ss;

View File

@ -25,7 +25,6 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
@ -64,7 +63,7 @@ public final class TestColumnHelper extends TestCase {
// Remember - POI column 0 == OOXML column 1
assertEquals((double) 88, helper.getColumn(0, false).getWidth());
assertTrue(helper.getColumn(0, false).getHidden());
assertEquals((double) 00, helper.getColumn(1, false).getWidth());
assertEquals((double)0, helper.getColumn(1, false).getWidth());
assertFalse(helper.getColumn(1, false).getHidden());
}
@ -260,7 +259,7 @@ public final class TestColumnHelper extends TestCase {
public void testGetSetColDefaultStyle() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getWorksheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
ColumnHelper columnHelper = sheet.getColumnHelper();
// POI column 3, OOXML column 4
@ -271,7 +270,7 @@ public final class TestColumnHelper extends TestCase {
columnHelper.setColDefaultStyle(3, 2);
assertEquals(2, columnHelper.getColDefaultStyle(3));
assertEquals(-1, columnHelper.getColDefaultStyle(4));
StylesTable stylesTable = (StylesTable) workbook.getStylesSource();
StylesTable stylesTable = workbook.getStylesSource();
CTXf cellXf = CTXf.Factory.newInstance();
cellXf.setFontId(0);
cellXf.setFillId(0);