Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved. Copyright 2009, 2010 IBM. All rights reserved. Use is subject to license terms. Licensed 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. You can also obtain a copy of the License at http://odftoolkit.org/docs/license.txt 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.odftoolkit.simple;
 
 import java.io.File;
 import java.util.List;
 
 
This class represents an empty ODF spreadsheet document.
 
 public class SpreadsheetDocument extends Document implements ChartContainer {
 
 	private static final String EMPTY_SPREADSHEET_DOCUMENT_PATH = "/OdfSpreadsheetDocument.ods";
This enum contains all possible media types of SpreadsheetDocument documents.
 
 	public enum OdfMediaType implements MediaType {
 
 		private final Document.OdfMediaType mMediaType;
 
 			this. = mediaType;
 		}

Returns:
the mediatype of this document
 
 		public String getMediaTypeString() {
 		}

Returns:
the ODF filesuffix of this document
 
 		public String getSuffix() {
 			return .getSuffix();
 		}

Parameters:
mediaType string defining an ODF document
Returns:
the according OdfMediatype encapuslating the given string and the suffix
 
 		public static Document.OdfMediaType getOdfMediaType(String mediaType) {
 			return Document.OdfMediaType.getOdfMediaType(mediaType);
 		}
 	}

Creates an empty spreadsheet document.

Returns:
ODF spreadsheet document based on a default template*
Throws:
java.lang.Exception - if the document could not be created
	}

Creates an empty spreadsheet template.

Returns:
ODF spreadsheet template based on a default
Throws:
java.lang.Exception - if the template could not be created
		return doc;
	}

To avoid data duplication a new document is only created, if not already opened. A document is cached by this constructor using the internalpath as key.
	protected SpreadsheetDocument(OdfPackage pkgString internalPathSpreadsheetDocument.OdfMediaType odfMediaType) {
		super(pkginternalPathodfMediaType.mMediaType);
	}

Creates an SpreadsheetDocument from the OpenDocument provided by a resource Stream.

Since an InputStream does not provide the arbitrary (non sequentiell) read access needed by SpreadsheetDocument, the InputStream is cached. This usually takes more time compared to the other createInternalDocument methods. An advantage of caching is that there are no problems overwriting an input file.

If the resource stream is not a ODF spreadsheet document, ClassCastException might be thrown.

Parameters:
inputStream - the InputStream of the ODF spreadsheet document.
Returns:
the spreadsheet document created from the given InputStream
Throws:
java.lang.Exception - if the document could not be created.
	public static SpreadsheetDocument loadDocument(InputStream inputStreamthrows Exception {
		return (SpreadsheetDocument) Document.loadDocument(inputStream);
	}

Loads an SpreadsheetDocument from the provided path.

SpreadsheetDocument relies on the file being available for read access over the whole lifecycle of SpreadsheetDocument.

If the resource stream is not a ODF spreadsheet document, ClassCastException might be thrown.

Parameters:
documentPath - the path from where the document can be loaded
Returns:
the spreadsheet document from the given path or NULL if the media type is not supported by SIMPLE.
Throws:
java.lang.Exception - if the document could not be created.
	public static SpreadsheetDocument loadDocument(String documentPaththrows Exception {
		return (SpreadsheetDocument) Document.loadDocument(documentPath);
	}

Creates an SpreadsheetDocument from the OpenDocument provided by a File.

SpreadsheetDocument relies on the file being available for read access over the whole lifecycle of SpreadsheetDocument.

If the resource stream is not a ODF spreadsheet document, ClassCastException might be thrown.

Parameters:
file - a file representing the ODF spreadsheet document.
Returns:
the spreadsheet document created from the given File
Throws:
java.lang.Exception - if the document could not be created.
	public static SpreadsheetDocument loadDocument(File filethrows Exception {
		return (SpreadsheetDocument) Document.loadDocument(file);
	}

Get the content root of a spreadsheet document.

Returns:
content root, representing the office:spreadsheet tag
Throws:
java.lang.Exception if the file DOM could not be created.
	}

Changes the document to the given mediatype. This method can only be used to convert a document to a related mediatype, e.g. template.

Parameters:
mediaType the related ODF mimetype
	public void changeMode(OdfMediaType mediaType) {
		setOdfMediaType(mediaType.mMediaType);
	}

Retrieves sheet by index.

Parameters:
index the index of the retrieved sheet, which starts from 0. If the index value is out of range (index >= sheet count or index < 0), this method would return null.
Since:
0.6
	public Table getSheetByIndex(int index) {
		if (index < 0) {
			return null;
		}
		int count = 0;
		try {
			OfficeSpreadsheetElement spreadsheetElement = getContentRoot();
			Node child = spreadsheetElement.getFirstChild();
			while ((child != null) && (count <= index)) {
				if (child instanceof TableTableElement) {
					if (count == index) {
else {
						count++;
					}
				}
				child = child.getNextSibling();
			}
catch (Exception e) {
			Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
		}
		return null;
	}

Retrieves sheet by name.

Parameters:
name the name of the retrieved sheet.
Since:
0.6
	public Table getSheetByName(String name) {
		return getTableByName(name);
	}


Adds a new blank sheet with the specified name to this document.

Parameters:
name the name of the new sheet.
Returns:
added sheet.
Since:
0.6
	public Table appendSheet(String name) {
		Table newTable = addTable();
		newTable.setTableName(name);
		return newTable;
	}

Adds a new sheet with data from existing table.

NOTE: This method copies data from existing table, including linked resources and styles, if the source table is not in the target document. If these data has dependencies to other data of the source document, the data dependencies will not be copied. For example, document A has two sheets, "Sheet1" and "Sheet2". In "Sheet2", there is a cell with formula, "=sum(Sheet1.A1:Sheet1.A10)". After copy the data of "Sheet2" to the new sheet in document B, the result of this formula would be different or even invalid in document B.

Parameters:
refTable the reference table, which is the data source of the new sheet.
name the name of the new sheet.
Returns:
added sheet.
Since:
0.6
	public Table appendSheet(Table refTableString name) {
		TableTableElement refTableElement = refTable.getOdfElement();
		try {
			OdfContentDom contentDom = getContentDom();
			TableTableElement newTableEle = (TableTableElement) (refTableElement.cloneNode(true));
			// not in a same document
			if (refTableElement.getOwnerDocument() != contentDom) {
				Document ownerDocument = refTable.getOwnerDocument();
				copyLinkedRefInBatch(newTableEleownerDocument);
				copyForeignStyleRef(newTableEleownerDocument);
				newTableEle = (TableTableElementcloneForeignElement(newTableElecontentDomtrue);
			}
			updateNames(newTableEle);
			updateXMLIds(newTableEle);
			Table tableInstance = getTableBuilder().getTableInstance(newTableEle);
			tableInstance.setTableName(name);
			return tableInstance;
catch (Exception e) {
			Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
		}
		return null;
	}

Inserts a new blank sheet before the reference index.

Parameters:
before the reference index, which starts from 0. If the index value is out of range (index >= sheet count or index < 0), this method would return null.
Returns:
inserted sheet.
Since:
0.6
	public Table insertSheet(int before) {
		if (before < 0) {
			return null;
		}
		int count = 0;
		try {
			OfficeSpreadsheetElement spreadsheetElement = getContentRoot();
			Node child = spreadsheetElement.getFirstChild();
			while ((child != null) && (count <= before)) {
				if (child instanceof TableTableElement) {
					if (count == before) {
						return table;
else {
						count++;
					}
				}
				child = child.getNextSibling();
			}
catch (Exception e) {
			Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
		}
		return null;
	}

Inserts a new sheet with data from existing table.

NOTE: This method copies data from existing table, including linked resources and styles, if the source table is not in the target document. If these data has dependencies to other data of the source document, the data dependencies will not be copied. For example, document A has two sheets, "Sheet1" and "Sheet2". In "Sheet2", there is a cell with formula, "=sum(Sheet1.A1:Sheet1.A10)". After copy the data of "Sheet2" to the new sheet in document B, the result of this formula would be different or even invalid in document B.

Parameters:
refTable the reference table, which is the data source of the new sheet.
before the reference index, which starts from 0 and new sheet would be inserted before it. If the index value is out of range (index >= sheet count or index < 0), this method would return null.
Returns:
inserted sheet.
Since:
0.6
	public Table insertSheet(Table refTableint before) {
		if (before < 0) {
			return null;
		}
		int count = 0;
		try {
			OfficeSpreadsheetElement spreadsheetElement = getContentRoot();
			Node child = spreadsheetElement.getFirstChild();
			while ((child != null) && (count <= before)) {
				if (child instanceof TableTableElement) {
					if (count == before) {
						TableTableElement refTableElement = refTable.getOdfElement();
						try {
							OdfContentDom contentDom = getContentDom();
							TableTableElement newTableEle = (TableTableElement) (refTableElement.cloneNode(true));
							//foreign node not in a same document
							if (refTableElement.getOwnerDocument() != contentDom) {
								Document ownerDocument = refTable.getOwnerDocument();
								copyLinkedRefInBatch(newTableEleownerDocument);
								copyForeignStyleRef(newTableEleownerDocument);
								newTableEle = (TableTableElementcloneForeignElement(newTableElecontentDomtrue);
							}
							updateNames(newTableEle);
							updateXMLIds(newTableEle);
							getContentRoot().insertBefore(newTableElechild);
							return getTableBuilder().getTableInstance(newTableEle);
catch (Exception e) {
							Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
						}
else {
						count++;
					}
				}
				child = child.getNextSibling();
			}
catch (Exception e) {
			Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
		}
		return null;
	}

Removes the sheet in the specified index.

Parameters:
index the index of the removed sheet, which starts from 0. If the index value is out of range (index >= sheet count or index < 0), this method would do nothing.
Since:
0.6
	public void removeSheet(int index) {
		if (index < 0) {
			return;
		}
		int count = 0;
		try {
			OfficeSpreadsheetElement spreadsheetElement = getContentRoot();
			Node child = spreadsheetElement.getFirstChild();
			while ((child != null) && (count <= index)) {
				if (child instanceof TableTableElement) {
					if (count == index) {
						spreadsheetElement.removeChild(child);
						return;
else {
						count++;
					}
				}
				child = child.getNextSibling();
			}
catch (Exception e) {
			Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
		}
	}

Returns the sheet count of this document.

Returns:
the sheet count of this document.
Since:
0.6
	public int getSheetCount() {
		int count = 0;
		try {
			OfficeSpreadsheetElement spreadsheetElement = getContentRoot();
			Node child = spreadsheetElement.getFirstChild();
			while (child != null) {
				if (child instanceof TableTableElement) {
					count++;
				}
				child = child.getNextSibling();
			}
catch (Exception e) {
			Logger.getLogger(SpreadsheetDocument.class.getName()).log(.nulle);
		}
		return count;
	}
	}

Creates a new Chart for this spreadsheet document.

Parameters:
title chart title.
dataset chart data set.
rect chart rectangle.
Returns:
the created chart.
Since:
0.6
	public Chart createChart(String titleDataSet datasetRectangle rect) {
		return getChartContainerImpl().createChart(titledatasetrect);
	}

Creates a new Chart for this spreadsheet document.

Parameters:
title chart title.
document the data source spreadsheet document.
cellRangeAddr the cell range address list which is used as chart data set.
firstRowAsLabel whether uses first row as label.
firstColumnAsLabel whether uses first column as label.
rowAsDataSeries whether uses data as series.
rect chart rectangle.
Returns:
the created chart.
Since:
0.6
	public Chart createChart(String titleSpreadsheetDocument documentCellRangeAddressList cellRangeAddrboolean firstRowAsLabel,
			boolean firstColumnAsLabelboolean rowAsDataSeriesRectangle rect) {
		return getChartContainerImpl().createChart(titledocumentcellRangeAddrfirstRowAsLabelfirstColumnAsLabel,
				rowAsDataSeriesrect);
	}

Creates a new Chart for this spreadsheet document.

Parameters:
title chart rectangle.
labels label strings
legends legend strings
data chart data set.
rect chart rectangle.
Returns:
the created chart.
Since:
0.6
	public Chart createChart(String titleString[] labelsString[] legendsdouble[][] dataRectangle rect) {
		return getChartContainerImpl().createChart(titlelabelslegendsdatarect);
	}

Creates a new Chart for this spreadsheet document.

Parameters:
title chart rectangle.
labels label strings
legends legend strings
data chart data set.
rect chart rectangle.
cell the position cell where the new chart is inserted.
Returns:
the created chart.
Since:
0.6
	public Chart createChart(String titleSpreadsheetDocument documentCellRangeAddressList cellRangeAddrboolean firstRowAsLabel,
			boolean firstColumnAsLabelboolean rowAsDataSeriesRectangle rectCell cell) {
		return getChartContainerImpl().createChart(titledocumentcellRangeAddrfirstRowAsLabelfirstColumnAsLabel,
				rowAsDataSeriesrectcell);
	}
	public void deleteChartById(String chartId) {
	}
	public void deleteChartByTitle(String title) {
	}
	public Chart getChartById(String chartId) {
	}
	public List<ChartgetChartByTitle(String title) {
	}
	public int getChartCount() {
	}
	private static String getUniqueSheetName(TableContainer container) {
		List<TabletableList = container.getTableList();
		boolean notUnique = true;
		String tablename = "Sheet" + (tableList.size() + 1);
		while (notUnique) {
			notUnique = false;
			for (int i = 0; i < tableList.size(); i++) {
				if (tableList.get(i).getTableName() != null) {
					if (tableList.get(i).getTableName().equalsIgnoreCase(tablename)) {
						notUnique = true;
						break;
					}
				}
			}
			if (notUnique) {
				tablename = tablename + Math.round(Math.random() * 10);
			}
		}
		return tablename;
	}
		if ( == null) {
		}
	}
	private class ChartContainerImpl extends AbstractChartContainer {
		protected ChartContainerImpl(Document doc) {
			super(doc);
		}
		protected DrawFrameElement getChartFrame() throws Exception {
			OdfContentDom contentDom2 = .getContentDom();
			DrawFrameElement drawFrame = contentDom2.newOdfElement(DrawFrameElement.class);
					"//table:table-cell[last()]"contentDom2.);
			lastCell.appendChild(drawFrame);
			drawFrame.removeAttribute("text:anchor-type");
			this. = drawFrame;
			return drawFrame;
		}
		private Chart createChart(String titleSpreadsheetDocument documentCellRangeAddressList cellRangeAddrboolean firstRowAsLabel,
				boolean firstColumnAsLabelboolean rowAsDataSeriesRectangle rectCell cell) {
			Chart chart = getChartContainerImpl().createChart(titledocumentcellRangeAddrfirstRowAsLabelfirstColumnAsLabel,
					rowAsDataSeriesrect);
			return chart;
		}
	}
New to GrepCode? Check out our FAQ X