2008-01-16 11:08:22 -05:00
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Licensed to the Apache Software Foundation ( ASF ) under one or more
contributor license agreements . See the NOTICE file distributed with
this work for additional information regarding copyright ownership .
The ASF licenses this file to You under the Apache License , Version 2 . 0
( the " License " ) ; you may not use this file except in compliance with
the License . You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an " AS IS " BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
package org.apache.poi.xssf.usermodel ;
import java.io.IOException ;
2008-03-16 19:30:51 -04:00
import java.io.InputStream ;
2008-01-16 11:08:22 -05:00
import java.io.OutputStream ;
2008-03-16 19:30:51 -04:00
import java.lang.reflect.Constructor ;
2008-07-29 19:03:25 -04:00
import java.util.ArrayList ;
2008-02-13 08:43:42 -05:00
import java.util.Iterator ;
2008-01-16 11:08:22 -05:00
import java.util.LinkedList ;
import java.util.List ;
import javax.xml.namespace.QName ;
2008-02-04 11:34:44 -05:00
import org.apache.poi.POIXMLDocument ;
2008-01-16 11:08:22 -05:00
import org.apache.poi.ss.usermodel.CellStyle ;
2008-03-31 19:30:56 -04:00
import org.apache.poi.ss.usermodel.CommentsSource ;
2008-03-16 12:51:58 -04:00
import org.apache.poi.ss.usermodel.CreationHelper ;
2008-01-16 11:08:22 -05:00
import org.apache.poi.ss.usermodel.DataFormat ;
import org.apache.poi.ss.usermodel.Font ;
2008-01-16 11:39:21 -05:00
import org.apache.poi.ss.usermodel.Palette ;
2008-02-19 12:34:39 -05:00
import org.apache.poi.ss.usermodel.PictureData ;
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659525 via svnmerge from
https://svn.apache.org:443/repos/asf/poi/trunk
........
r659525 | nick | 2008-05-23 13:58:56 +0100 (Fri, 23 May 2008) | 1 line
Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level
........
Also, port this to XSSF
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@659533 13f79535-47bb-0310-9956-ffa450edef68
2008-05-23 09:12:43 -04:00
import org.apache.poi.ss.usermodel.Row ;
2008-02-13 08:43:42 -05:00
import org.apache.poi.ss.usermodel.SharedStringSource ;
2008-01-16 11:08:22 -05:00
import org.apache.poi.ss.usermodel.Sheet ;
2008-03-16 18:07:38 -04:00
import org.apache.poi.ss.usermodel.StylesSource ;
2008-01-16 11:08:22 -05:00
import org.apache.poi.ss.usermodel.Workbook ;
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659525 via svnmerge from
https://svn.apache.org:443/repos/asf/poi/trunk
........
r659525 | nick | 2008-05-23 13:58:56 +0100 (Fri, 23 May 2008) | 1 line
Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level
........
Also, port this to XSSF
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@659533 13f79535-47bb-0310-9956-ffa450edef68
2008-05-23 09:12:43 -04:00
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy ;
2008-04-15 19:18:25 -04:00
import org.apache.poi.ss.util.SheetReferences ;
2008-02-19 07:56:51 -05:00
import org.apache.poi.util.POILogFactory ;
import org.apache.poi.util.POILogger ;
2008-07-18 14:22:25 -04:00
import org.apache.poi.xssf.model.BinaryPart ;
2008-03-31 19:30:56 -04:00
import org.apache.poi.xssf.model.CommentsTable ;
2008-07-29 19:03:25 -04:00
import org.apache.poi.xssf.model.Control ;
import org.apache.poi.xssf.model.Drawing ;
2008-03-16 14:57:15 -04:00
import org.apache.poi.xssf.model.SharedStringsTable ;
2008-03-16 18:07:38 -04:00
import org.apache.poi.xssf.model.StylesTable ;
2008-03-16 18:44:10 -04:00
import org.apache.poi.xssf.model.XSSFModel ;
2008-01-25 12:16:14 -05:00
import org.apache.xmlbeans.XmlException ;
2008-01-22 08:28:48 -05:00
import org.apache.xmlbeans.XmlObject ;
2008-01-16 11:08:22 -05:00
import org.apache.xmlbeans.XmlOptions ;
import org.openxml4j.exceptions.InvalidFormatException ;
import org.openxml4j.opc.Package ;
import org.openxml4j.opc.PackagePart ;
import org.openxml4j.opc.PackagePartName ;
2008-02-07 09:08:32 -05:00
import org.openxml4j.opc.PackageRelationship ;
2008-02-13 08:43:42 -05:00
import org.openxml4j.opc.PackageRelationshipCollection ;
2008-01-16 11:08:22 -05:00
import org.openxml4j.opc.PackageRelationshipTypes ;
import org.openxml4j.opc.PackagingURIHelper ;
import org.openxml4j.opc.TargetMode ;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView ;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews ;
2008-03-31 18:30:56 -04:00
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName ;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames ;
2008-02-29 09:18:06 -05:00
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet ;
2008-01-16 11:08:22 -05:00
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet ;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook ;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet ;
2008-01-25 12:16:14 -05:00
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument ;
2008-02-07 09:08:32 -05:00
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument ;
2008-01-16 11:08:22 -05:00
2008-02-04 11:34:44 -05:00
public class XSSFWorkbook extends POIXMLDocument implements Workbook {
2008-03-21 13:31:14 -04:00
public static final XSSFRelation WORKBOOK = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook " ,
" /xl/workbook.xml " ,
null
) ;
2008-07-29 18:02:09 -04:00
public static final XSSFRelation MACROS_WORKBOOK = new XSSFRelation (
" application/vnd.ms-excel.sheet.macroEnabled.main+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument " ,
" /xl/workbook.xml " ,
null
) ;
2008-03-16 18:44:10 -04:00
public static final XSSFRelation WORKSHEET = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet " ,
" /xl/worksheets/sheet#.xml " ,
null
) ;
public static final XSSFRelation SHARED_STRINGS = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings " ,
" /xl/sharedStrings.xml " ,
SharedStringsTable . class
) ;
public static final XSSFRelation STYLES = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles " ,
" /xl/styles.xml " ,
StylesTable . class
) ;
public static final XSSFRelation DRAWINGS = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.drawingml.chart+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing " ,
" /xl/drawings/drawing#.xml " ,
null
) ;
2008-07-29 19:03:25 -04:00
public static final XSSFRelation VML_DRAWINGS = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.vmlDrawing " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing " ,
" /xl/drawings/vmlDrawing#.vml " ,
null
) ;
2008-03-16 18:44:10 -04:00
public static final XSSFRelation IMAGES = new XSSFRelation (
2008-07-29 19:03:25 -04:00
" image/x-emf " , // TODO
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/image " ,
" /xl/media/image#.emf " ,
2008-03-16 18:44:10 -04:00
null
) ;
2008-03-31 19:30:56 -04:00
public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation (
" application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments " ,
" /xl/comments#.xml " ,
CommentsTable . class
) ;
public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation (
null ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink " ,
null ,
null
) ;
2008-05-28 09:32:03 -04:00
public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation (
null ,
OLE_OBJECT_REL_TYPE ,
null ,
2008-07-18 14:22:25 -04:00
BinaryPart . class
2008-05-28 09:32:03 -04:00
) ;
public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation (
null ,
PACK_OBJECT_REL_TYPE ,
null ,
2008-07-18 14:22:25 -04:00
BinaryPart . class
) ;
2008-07-29 19:03:25 -04:00
2008-07-18 14:22:25 -04:00
public static final XSSFRelation VBA_MACROS = new XSSFRelation (
" application/vnd.ms-office.vbaProject " ,
" http://schemas.microsoft.com/office/2006/relationships/vbaProject " ,
" /xl/vbaProject.bin " ,
BinaryPart . class
2008-05-28 09:32:03 -04:00
) ;
2008-07-29 19:03:25 -04:00
public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation (
" application/vnd.ms-office.activeX+xml " ,
" http://schemas.openxmlformats.org/officeDocument/2006/relationships/control " ,
" /xl/activeX/activeX#.xml " ,
null
) ;
public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation (
" application/vnd.ms-office.activeX " ,
" http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary " ,
" /xl/activeX/activeX#.bin " ,
BinaryPart . class
) ;
2008-05-28 09:32:03 -04:00
2008-03-16 19:30:51 -04:00
public static class XSSFRelation {
private String TYPE ;
private String REL ;
private String DEFAULT_NAME ;
private Class < ? extends XSSFModel > CLASS ;
private XSSFRelation ( String TYPE , String REL , String DEFAULT_NAME , Class < ? extends XSSFModel > CLASS ) {
this . TYPE = TYPE ;
this . REL = REL ;
this . DEFAULT_NAME = DEFAULT_NAME ;
this . CLASS = CLASS ;
}
public String getContentType ( ) { return TYPE ; }
public String getRelation ( ) { return REL ; }
public String getDefaultFileName ( ) { return DEFAULT_NAME ; }
2008-04-02 20:23:59 -04:00
2008-07-18 14:22:25 -04:00
/ * *
* Does one of these exist for the given core
* package part ?
* /
public boolean exists ( PackagePart corePart ) throws IOException , InvalidFormatException {
if ( corePart = = null ) {
// new file, can't exist
return false ;
}
PackageRelationshipCollection prc =
corePart . getRelationshipsByType ( REL ) ;
Iterator < PackageRelationship > it = prc . iterator ( ) ;
if ( it . hasNext ( ) ) {
return true ;
} else {
return false ;
}
}
2008-04-02 20:23:59 -04:00
/ * *
* Returns the filename for the nth one of these ,
* eg / xl / comments4 . xml
* /
public String getFileName ( int index ) {
if ( DEFAULT_NAME . indexOf ( " # " ) = = - 1 ) {
// Generic filename in all cases
return getDefaultFileName ( ) ;
}
return DEFAULT_NAME . replace ( " # " , Integer . toString ( index ) ) ;
}
2008-03-16 19:30:51 -04:00
/ * *
2008-03-30 12:25:52 -04:00
* Fetches the InputStream to read the contents , based
* of the specified core part
2008-03-16 19:30:51 -04:00
* /
2008-03-30 12:25:52 -04:00
public InputStream getContents ( PackagePart corePart ) throws IOException , InvalidFormatException {
2008-03-16 19:30:51 -04:00
PackageRelationshipCollection prc =
corePart . getRelationshipsByType ( REL ) ;
Iterator < PackageRelationship > it = prc . iterator ( ) ;
if ( it . hasNext ( ) ) {
PackageRelationship rel = it . next ( ) ;
PackagePartName relName = PackagingURIHelper . createPartName ( rel . getTargetURI ( ) ) ;
PackagePart part = corePart . getPackage ( ) . getPart ( relName ) ;
2008-03-30 12:25:52 -04:00
return part . getInputStream ( ) ;
} else {
log . log ( POILogger . WARN , " No part " + DEFAULT_NAME + " found " ) ;
return null ;
}
}
/ * *
* Load , off the specified core part
* /
public XSSFModel load ( PackagePart corePart ) throws Exception {
Constructor < ? extends XSSFModel > c = CLASS . getConstructor ( InputStream . class ) ;
XSSFModel model = null ;
InputStream inp = getContents ( corePart ) ;
if ( inp ! = null ) {
2008-03-16 19:30:51 -04:00
try {
2008-03-30 12:25:52 -04:00
model = c . newInstance ( inp ) ;
2008-03-16 19:30:51 -04:00
} finally {
2008-03-30 12:25:52 -04:00
inp . close ( ) ;
2008-03-16 19:30:51 -04:00
}
}
return model ;
}
2008-03-30 12:25:52 -04:00
2008-03-16 19:30:51 -04:00
/ * *
* Save , with the default name
2008-03-21 13:31:14 -04:00
* @return The internal reference ID it was saved at , normally then used as an r : id
2008-03-16 19:30:51 -04:00
* /
2008-03-21 13:31:14 -04:00
private String save ( XSSFModel model , PackagePart corePart ) throws IOException {
return save ( model , corePart , DEFAULT_NAME ) ;
2008-03-16 19:30:51 -04:00
}
/ * *
* Save , with the specified name
2008-03-21 13:31:14 -04:00
* @return The internal reference ID it was saved at , normally then used as an r : id
2008-03-16 19:30:51 -04:00
* /
2008-03-21 13:31:14 -04:00
private String save ( XSSFModel model , PackagePart corePart , String name ) throws IOException {
2008-03-16 19:30:51 -04:00
PackagePartName ppName = null ;
try {
ppName = PackagingURIHelper . createPartName ( name ) ;
} catch ( InvalidFormatException e ) {
throw new IllegalStateException ( e ) ;
}
2008-03-21 13:31:14 -04:00
PackageRelationship rel =
corePart . addRelationship ( ppName , TargetMode . INTERNAL , REL ) ;
2008-03-16 19:30:51 -04:00
PackagePart part = corePart . getPackage ( ) . createPart ( ppName , TYPE ) ;
2008-03-21 15:54:35 -04:00
2008-03-16 19:30:51 -04:00
OutputStream out = part . getOutputStream ( ) ;
model . writeTo ( out ) ;
out . close ( ) ;
2008-03-21 13:31:14 -04:00
return rel . getId ( ) ;
2008-03-16 19:30:51 -04:00
}
}
2008-07-29 18:02:09 -04:00
/** Are we a normal workbook, or a macro enabled one? */
private boolean isMacroEnabled = false ;
2008-03-16 19:30:51 -04:00
2008-01-16 11:08:22 -05:00
private CTWorkbook workbook ;
private List < XSSFSheet > sheets = new LinkedList < XSSFSheet > ( ) ;
2008-03-31 18:30:56 -04:00
private List < XSSFName > namedRanges = new LinkedList < XSSFName > ( ) ;
2008-02-13 08:43:42 -05:00
private SharedStringSource sharedStringSource ;
2008-03-16 18:07:38 -04:00
private StylesSource stylesSource ;
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659525 via svnmerge from
https://svn.apache.org:443/repos/asf/poi/trunk
........
r659525 | nick | 2008-05-23 13:58:56 +0100 (Fri, 23 May 2008) | 1 line
Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level
........
Also, port this to XSSF
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@659533 13f79535-47bb-0310-9956-ffa450edef68
2008-05-23 09:12:43 -04:00
private MissingCellPolicy missingCellPolicy = Row . RETURN_NULL_AND_BLANK ;
2008-02-19 07:56:51 -05:00
private static POILogger log = POILogFactory . getLogger ( XSSFWorkbook . class ) ;
2008-02-13 08:43:42 -05:00
2008-01-16 11:08:22 -05:00
public XSSFWorkbook ( ) {
this . workbook = CTWorkbook . Factory . newInstance ( ) ;
CTBookViews bvs = this . workbook . addNewBookViews ( ) ;
CTBookView bv = bvs . addNewWorkbookView ( ) ;
bv . setActiveTab ( 0 ) ;
this . workbook . addNewSheets ( ) ;
2008-03-21 15:54:35 -04:00
// We always require styles and shared strings
sharedStringSource = new SharedStringsTable ( ) ;
stylesSource = new StylesTable ( ) ;
2008-01-16 11:08:22 -05:00
}
2008-01-25 12:16:14 -05:00
public XSSFWorkbook ( String path ) throws IOException {
2008-03-08 12:39:56 -05:00
this ( openPackage ( path ) ) ;
}
public XSSFWorkbook ( Package pkg ) throws IOException {
super ( pkg ) ;
2008-01-25 12:16:14 -05:00
try {
2008-02-04 11:34:44 -05:00
WorkbookDocument doc = WorkbookDocument . Factory . parse ( getCorePart ( ) . getInputStream ( ) ) ;
2008-01-25 12:16:14 -05:00
this . workbook = doc . getWorkbook ( ) ;
2008-03-16 18:07:38 -04:00
2008-07-29 18:02:09 -04:00
// Are we macro enabled, or just normal?
isMacroEnabled =
getCorePart ( ) . getContentType ( ) . equals ( MACROS_WORKBOOK . getContentType ( ) ) ;
2008-03-16 19:30:51 -04:00
try {
// Load shared strings
this . sharedStringSource = ( SharedStringSource )
SHARED_STRINGS . load ( getCorePart ( ) ) ;
2008-03-21 15:54:35 -04:00
} catch ( Exception e ) {
throw new IOException ( " Unable to load shared strings - " + e . toString ( ) ) ;
}
try {
2008-03-16 19:30:51 -04:00
// Load styles source
this . stylesSource = ( StylesSource )
STYLES . load ( getCorePart ( ) ) ;
} catch ( Exception e ) {
2008-03-21 15:54:35 -04:00
e . printStackTrace ( ) ;
throw new IOException ( " Unable to load styles - " + e . toString ( ) ) ;
2008-03-16 18:07:38 -04:00
}
2008-02-13 08:43:42 -05:00
// Load individual sheets
2008-02-07 09:08:32 -05:00
for ( CTSheet ctSheet : this . workbook . getSheets ( ) . getSheetArray ( ) ) {
2008-02-19 12:34:39 -05:00
PackagePart part = getPackagePart ( ctSheet ) ;
if ( part = = null ) {
2008-03-21 13:31:14 -04:00
log . log ( POILogger . WARN , " Sheet with name " + ctSheet . getName ( ) + " and r:id " + ctSheet . getId ( ) + " was defined, but didn't exist in package, skipping " ) ;
2008-02-19 07:56:51 -05:00
continue ;
}
2008-03-31 19:30:56 -04:00
// Get the comments for the sheet, if there are any
CommentsSource comments = null ;
PackageRelationshipCollection commentsRel =
part . getRelationshipsByType ( SHEET_COMMENTS . REL ) ;
if ( commentsRel ! = null & & commentsRel . size ( ) > 0 ) {
PackagePart commentsPart =
getTargetPart ( commentsRel . getRelationship ( 0 ) ) ;
comments = new CommentsTable ( commentsPart . getInputStream ( ) ) ;
}
2008-07-29 19:03:25 -04:00
// Get the drawings for the sheet, if there are any
ArrayList < Drawing > drawings = new ArrayList < Drawing > ( ) ;
for ( PackageRelationship rel : part . getRelationshipsByType ( VML_DRAWINGS . REL ) ) {
PackagePart drawingPart = getTargetPart ( rel ) ;
Drawing drawing = new Drawing ( drawingPart . getInputStream ( ) , rel . getId ( ) ) ;
drawing . findChildren ( drawingPart ) ;
drawings . add ( drawing ) ;
}
// Get the activeX controls for the sheet, if there are any
ArrayList < Control > controls = new ArrayList < Control > ( ) ;
for ( PackageRelationship rel : part . getRelationshipsByType ( ACTIVEX_CONTROLS . REL ) ) {
PackagePart controlPart = getTargetPart ( rel ) ;
Control control = new Control ( controlPart . getInputStream ( ) , rel . getId ( ) ) ;
control . findChildren ( controlPart ) ;
controls . add ( control ) ;
}
2008-03-31 19:30:56 -04:00
// Now create the sheet
2008-02-07 09:08:32 -05:00
WorksheetDocument worksheetDoc = WorksheetDocument . Factory . parse ( part . getInputStream ( ) ) ;
2008-07-29 19:03:25 -04:00
XSSFSheet sheet = new XSSFSheet ( ctSheet , worksheetDoc . getWorksheet ( ) , this , comments , drawings , controls ) ;
2008-02-07 09:08:32 -05:00
this . sheets . add ( sheet ) ;
2008-04-05 17:43:53 -04:00
// Process external hyperlinks for the sheet,
// if there are any
PackageRelationshipCollection hyperlinkRels =
part . getRelationshipsByType ( SHEET_HYPERLINKS . REL ) ;
sheet . initHyperlinks ( hyperlinkRels ) ;
2008-05-28 09:32:03 -04:00
// Get the embeddings for the workbook
for ( PackageRelationship rel : part . getRelationshipsByType ( OLEEMBEDDINGS . REL ) )
embedds . add ( getTargetPart ( rel ) ) ; // TODO: Add this reference to each sheet as well
for ( PackageRelationship rel : part . getRelationshipsByType ( PACKEMBEDDINGS . REL ) )
embedds . add ( getTargetPart ( rel ) ) ;
2008-02-07 09:08:32 -05:00
}
2008-01-25 12:16:14 -05:00
} catch ( XmlException e ) {
throw new IOException ( e . toString ( ) ) ;
2008-02-07 09:08:32 -05:00
} catch ( InvalidFormatException e ) {
throw new IOException ( e . toString ( ) ) ;
2008-01-25 12:16:14 -05:00
}
2008-03-31 18:30:56 -04:00
// Process the named ranges
if ( workbook . getDefinedNames ( ) ! = null ) {
for ( CTDefinedName ctName : workbook . getDefinedNames ( ) . getDefinedNameArray ( ) ) {
namedRanges . add ( new XSSFName ( ctName , this ) ) ;
}
}
2008-01-25 12:16:14 -05:00
}
2008-01-22 08:28:48 -05:00
protected CTWorkbook getWorkbook ( ) {
return this . workbook ;
}
2008-02-19 12:34:39 -05:00
/ * *
* Get the PackagePart corresponding to a given sheet .
*
* @param ctSheet The sheet
* @return A PackagePart , or null if no matching part found .
* @throws InvalidFormatException
* /
private PackagePart getPackagePart ( CTSheet ctSheet ) throws InvalidFormatException {
PackageRelationship rel = this . getCorePart ( ) . getRelationship ( ctSheet . getId ( ) ) ;
if ( rel = = null ) {
2008-03-21 13:31:14 -04:00
log . log ( POILogger . WARN , " No relationship found for sheet " + ctSheet . getId ( ) + " - core part has " + this . getCorePart ( ) . getRelationships ( ) . size ( ) + " relations defined " ) ;
2008-02-19 12:34:39 -05:00
return null ;
}
return getTargetPart ( rel ) ;
}
2008-01-22 08:28:48 -05:00
2008-01-16 11:08:22 -05:00
public int addPicture ( byte [ ] pictureData , int format ) {
// TODO Auto-generated method stub
return 0 ;
}
public int addSSTString ( String string ) {
// TODO Auto-generated method stub
return 0 ;
}
public Sheet cloneSheet ( int sheetNum ) {
2008-01-23 10:52:59 -05:00
XSSFSheet srcSheet = sheets . get ( sheetNum ) ;
String srcName = getSheetName ( sheetNum ) ;
if ( srcSheet ! = null ) {
XSSFSheet clonedSheet = srcSheet . cloneSheet ( ) ;
sheets . add ( clonedSheet ) ;
CTSheet newcts = this . workbook . getSheets ( ) . addNewSheet ( ) ;
newcts . set ( clonedSheet . getSheet ( ) ) ;
int i = 1 ;
while ( true ) {
//Try and find the next sheet name that is unique
String name = srcName ;
String index = Integer . toString ( i + + ) ;
if ( name . length ( ) + index . length ( ) + 2 < 31 ) {
name = name + " ( " + index + " ) " ;
} else {
name = name . substring ( 0 , 31 - index . length ( ) - 2 ) + " ( " + index + " ) " ;
}
//If the sheet name is unique, then set it otherwise move on to the next number.
if ( getSheetIndex ( name ) = = - 1 ) {
setSheetName ( sheets . size ( ) - 1 , name ) ;
break ;
}
}
return clonedSheet ;
}
2008-01-16 11:08:22 -05:00
return null ;
}
public CellStyle createCellStyle ( ) {
2008-03-22 10:38:43 -04:00
return new XSSFCellStyle ( stylesSource ) ;
2008-01-16 11:08:22 -05:00
}
public DataFormat createDataFormat ( ) {
2008-03-22 10:38:43 -04:00
return getCreationHelper ( ) . createDataFormat ( ) ;
2008-01-16 11:08:22 -05:00
}
public Font createFont ( ) {
// TODO Auto-generated method stub
return null ;
}
2008-03-31 18:30:56 -04:00
public XSSFName createName ( ) {
XSSFName name = new XSSFName ( this ) ;
namedRanges . add ( name ) ;
return name ;
2008-01-16 11:08:22 -05:00
}
public Sheet createSheet ( ) {
return createSheet ( null ) ;
}
public Sheet createSheet ( String sheetname ) {
2008-02-29 09:18:06 -05:00
return createSheet ( sheetname , null ) ;
}
public Sheet createSheet ( String sheetname , CTWorksheet worksheet ) {
CTSheet sheet = addSheet ( sheetname ) ;
XSSFWorksheet wrapper = new XSSFWorksheet ( sheet , worksheet , this ) ;
2008-01-16 11:08:22 -05:00
this . sheets . add ( wrapper ) ;
return wrapper ;
}
2008-02-29 09:18:06 -05:00
public Sheet createDialogsheet ( String sheetname , CTDialogsheet dialogsheet ) {
CTSheet sheet = addSheet ( sheetname ) ;
XSSFDialogsheet wrapper = new XSSFDialogsheet ( sheet , dialogsheet , this ) ;
this . sheets . add ( wrapper ) ;
return wrapper ;
}
private CTSheet addSheet ( String sheetname ) {
CTSheet sheet = workbook . getSheets ( ) . addNewSheet ( ) ;
if ( sheetname ! = null ) {
sheet . setName ( sheetname ) ;
}
return sheet ;
}
2008-01-16 11:08:22 -05:00
public void dumpDrawingGroupRecords ( boolean fat ) {
// TODO Auto-generated method stub
}
public Font findFont ( short boldWeight , short color , short fontHeight , String name , boolean italic , boolean strikeout , short typeOffset , byte underline ) {
// TODO Auto-generated method stub
return null ;
}
public List getAllEmbeddedObjects ( ) {
// TODO Auto-generated method stub
return null ;
}
2008-02-19 12:34:39 -05:00
public List < PictureData > getAllPictures ( ) {
// In OOXML pictures are referred to in sheets
List < PictureData > pictures = new LinkedList < PictureData > ( ) ;
for ( CTSheet ctSheet : this . workbook . getSheets ( ) . getSheetArray ( ) ) {
try {
PackagePart sheetPart = getPackagePart ( ctSheet ) ;
if ( sheetPart = = null ) {
continue ;
}
2008-03-16 18:44:10 -04:00
PackageRelationshipCollection prc = sheetPart . getRelationshipsByType ( DRAWINGS . getRelation ( ) ) ;
2008-02-19 12:34:39 -05:00
for ( PackageRelationship rel : prc ) {
PackagePart drawingPart = getTargetPart ( rel ) ;
2008-03-16 18:44:10 -04:00
PackageRelationshipCollection prc2 = drawingPart . getRelationshipsByType ( IMAGES . getRelation ( ) ) ;
2008-02-19 12:34:39 -05:00
for ( PackageRelationship rel2 : prc2 ) {
PackagePart imagePart = getTargetPart ( rel2 ) ;
XSSFPictureData pd = new XSSFPictureData ( imagePart ) ;
pictures . add ( pd ) ;
}
}
} catch ( InvalidFormatException e ) {
throw new RuntimeException ( e . getMessage ( ) , e ) ;
}
}
return pictures ;
2008-01-16 11:08:22 -05:00
}
public boolean getBackupFlag ( ) {
// TODO Auto-generated method stub
return false ;
}
public byte [ ] getBytes ( ) {
// TODO Auto-generated method stub
return null ;
}
public CellStyle getCellStyleAt ( short idx ) {
// TODO Auto-generated method stub
return null ;
}
2008-01-16 11:39:21 -05:00
public Palette getCustomPalette ( ) {
2008-01-16 11:08:22 -05:00
// TODO Auto-generated method stub
return null ;
}
public short getDisplayedTab ( ) {
// TODO Auto-generated method stub
return 0 ;
}
public Font getFontAt ( short idx ) {
// TODO Auto-generated method stub
return null ;
}
2008-03-31 18:30:56 -04:00
public XSSFName getNameAt ( int index ) {
return namedRanges . get ( index ) ;
2008-01-16 11:08:22 -05:00
}
public String getNameName ( int index ) {
2008-03-31 18:30:56 -04:00
return getNameAt ( index ) . getNameName ( ) ;
}
public int getNameIndex ( String name ) {
for ( int i = 0 ; i < namedRanges . size ( ) ; i + + ) {
if ( namedRanges . get ( i ) . getNameName ( ) . equals ( name ) ) {
return i ;
}
}
return - 1 ;
2008-01-16 11:08:22 -05:00
}
2008-04-15 19:18:25 -04:00
/ * *
* TODO - figure out what the hell this methods does in
* HSSF . . .
* /
public String resolveNameXText ( int refIndex , int definedNameIndex ) {
// TODO Replace with something proper
return null ;
}
2008-01-16 11:08:22 -05:00
2008-04-15 19:18:25 -04:00
public short getNumCellStyles ( ) {
2008-01-16 11:08:22 -05:00
// TODO Auto-generated method stub
return 0 ;
}
public short getNumberOfFonts ( ) {
// TODO Auto-generated method stub
return 0 ;
}
public int getNumberOfNames ( ) {
2008-03-31 18:30:56 -04:00
return namedRanges . size ( ) ;
2008-01-16 11:08:22 -05:00
}
public int getNumberOfSheets ( ) {
return this . workbook . getSheets ( ) . sizeOfSheetArray ( ) ;
}
public String getPrintArea ( int sheetIndex ) {
// TODO Auto-generated method stub
return null ;
}
public String getSSTString ( int index ) {
2008-03-16 18:07:38 -04:00
return getSharedStringSource ( ) . getSharedStringAt ( index ) ;
2008-01-16 11:08:22 -05:00
}
public short getSelectedTab ( ) {
2008-01-22 08:28:48 -05:00
short i = 0 ;
for ( XSSFSheet sheet : this . sheets ) {
2008-02-29 09:18:06 -05:00
if ( sheet . isSelected ( ) ) {
2008-01-22 08:28:48 -05:00
return i ;
}
+ + i ;
}
return - 1 ;
2008-01-16 11:08:22 -05:00
}
2008-03-29 18:03:18 -04:00
/ * *
* Doesn ' t do anything - returns the same index
* TODO - figure out if this is a ole2 specific thing , or
* if we need to do something proper here too !
* /
public int getSheetIndexFromExternSheetIndex ( int externSheetNumber ) {
return externSheetNumber ;
}
2008-01-16 11:08:22 -05:00
2008-03-29 18:03:18 -04:00
public Sheet getSheet ( String name ) {
2008-01-24 09:09:34 -05:00
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 ) ;
}
}
2008-01-16 11:08:22 -05:00
return null ;
}
public Sheet getSheetAt ( int index ) {
2008-01-22 08:28:48 -05:00
return this . sheets . get ( index ) ;
2008-01-16 11:08:22 -05:00
}
public int getSheetIndex ( String name ) {
2008-01-22 08:28:48 -05:00
CTSheet [ ] sheets = this . workbook . getSheets ( ) . getSheetArray ( ) ;
for ( int i = 0 ; i < sheets . length ; + + i ) {
if ( name . equals ( sheets [ i ] . getName ( ) ) ) {
return i ;
}
}
return - 1 ;
2008-01-16 11:08:22 -05:00
}
public int getSheetIndex ( Sheet sheet ) {
2008-01-22 08:28:48 -05:00
return this . sheets . indexOf ( sheet ) ;
2008-01-16 11:08:22 -05:00
}
2008-04-15 19:18:25 -04:00
/ * *
* Returns the external sheet index of the sheet
* with the given internal index , creating one
* if needed .
* Used by some of the more obscure formula and
* named range things .
* Fairly easy on XSSF ( we think . . . ) since the
* internal and external indicies are the same
* /
public int getExternalSheetIndex ( int internalSheetIndex ) {
return internalSheetIndex ;
}
2008-01-16 11:08:22 -05:00
public String getSheetName ( int sheet ) {
2008-01-23 10:52:59 -05:00
return this . workbook . getSheets ( ) . getSheetArray ( sheet ) . getName ( ) ;
2008-01-16 11:08:22 -05:00
}
2008-04-15 19:18:25 -04:00
public SheetReferences getSheetReferences ( ) {
SheetReferences sr = new SheetReferences ( ) ;
for ( int i = 0 ; i < getNumberOfSheets ( ) ; i + + ) {
sr . addSheetReference ( getSheetName ( i ) , i ) ;
}
return sr ;
}
2008-07-29 18:02:09 -04:00
/ * *
* Are we a normal workbook ( . xlsx ) , or a
* macro enabled workbook ( . xlsm ) ?
* /
public boolean isMacroEnabled ( ) {
return isMacroEnabled ;
}
2008-01-16 11:08:22 -05:00
2008-04-15 19:18:25 -04:00
public void insertChartRecord ( ) {
2008-01-16 11:08:22 -05:00
// TODO Auto-generated method stub
}
public void removeName ( int index ) {
// TODO Auto-generated method stub
}
public void removeName ( String name ) {
// TODO Auto-generated method stub
}
public void removePrintArea ( int sheetIndex ) {
// TODO Auto-generated method stub
}
public void removeSheetAt ( int index ) {
2008-02-29 09:18:06 -05:00
this . sheets . remove ( index ) ;
2008-01-24 09:09:34 -05:00
this . workbook . getSheets ( ) . removeSheet ( index ) ;
2008-01-16 11:08:22 -05:00
}
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659525 via svnmerge from
https://svn.apache.org:443/repos/asf/poi/trunk
........
r659525 | nick | 2008-05-23 13:58:56 +0100 (Fri, 23 May 2008) | 1 line
Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level
........
Also, port this to XSSF
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@659533 13f79535-47bb-0310-9956-ffa450edef68
2008-05-23 09:12:43 -04:00
/ * *
* Retrieves the current policy on what to do when
* getting missing or blank cells from a row .
* The default is to return blank and null cells .
* { @link MissingCellPolicy }
* /
public MissingCellPolicy getMissingCellPolicy ( ) {
return missingCellPolicy ;
}
/ * *
* Sets the policy on what to do when
* getting missing or blank cells from a row .
* This will then apply to all calls to
* { @link Row . getCell ( ) } . See
* { @link MissingCellPolicy }
* /
public void setMissingCellPolicy ( MissingCellPolicy missingCellPolicy ) {
this . missingCellPolicy = missingCellPolicy ;
}
2008-01-16 11:08:22 -05:00
public void setBackupFlag ( boolean backupValue ) {
// TODO Auto-generated method stub
}
public void setDisplayedTab ( short index ) {
// TODO Auto-generated method stub
}
public void setPrintArea ( int sheetIndex , String reference ) {
// TODO Auto-generated method stub
}
public void setPrintArea ( int sheetIndex , int startColumn , int endColumn , int startRow , int endRow ) {
// TODO Auto-generated method stub
}
public void setRepeatingRowsAndColumns ( int sheetIndex , int startColumn , int endColumn , int startRow , int endRow ) {
// TODO Auto-generated method stub
}
2008-01-22 08:28:48 -05:00
/ * *
* We only set one sheet as selected for compatibility with HSSF .
* /
2008-01-16 11:08:22 -05:00
public void setSelectedTab ( short index ) {
2008-01-22 08:28:48 -05:00
for ( int i = 0 ; i < this . sheets . size ( ) ; + + i ) {
XSSFSheet sheet = this . sheets . get ( i ) ;
2008-02-29 09:18:06 -05:00
sheet . setSelected ( i = = index ) ;
2008-01-22 08:28:48 -05:00
}
2008-01-16 11:08:22 -05:00
}
public void setSheetName ( int sheet , String name ) {
2008-01-23 10:52:59 -05:00
this . workbook . getSheets ( ) . getSheetArray ( sheet ) . setName ( name ) ;
2008-01-16 11:08:22 -05:00
}
public void setSheetName ( int sheet , String name , short encoding ) {
2008-01-23 10:52:59 -05:00
this . workbook . getSheets ( ) . getSheetArray ( sheet ) . setName ( name ) ;
2008-01-16 11:08:22 -05:00
}
public void setSheetOrder ( String sheetname , int pos ) {
2008-01-22 08:28:48 -05:00
int idx = getSheetIndex ( sheetname ) ;
sheets . add ( pos , sheets . remove ( idx ) ) ;
// Reorder CTSheets
XmlObject cts = this . workbook . getSheets ( ) . getSheetArray ( idx ) . copy ( ) ;
this . workbook . getSheets ( ) . removeSheet ( idx ) ;
CTSheet newcts = this . workbook . getSheets ( ) . insertNewSheet ( pos ) ;
newcts . set ( cts ) ;
2008-01-16 11:08:22 -05:00
}
public void unwriteProtectWorkbook ( ) {
// TODO Auto-generated method stub
}
public void write ( OutputStream stream ) throws IOException {
2008-07-29 18:02:09 -04:00
// What kind of workbook are we?
XSSFRelation workbookRelation = WORKBOOK ;
if ( isMacroEnabled ) {
workbookRelation = MACROS_WORKBOOK ;
}
2008-01-16 11:08:22 -05:00
try {
// Create a package referring the temp file.
2008-01-25 07:34:33 -05:00
Package pkg = Package . create ( stream ) ;
2008-01-16 11:08:22 -05:00
// Main part
2008-07-29 18:02:09 -04:00
PackagePartName corePartName = PackagingURIHelper . createPartName ( workbookRelation . getDefaultFileName ( ) ) ;
2008-01-16 11:08:22 -05:00
// Create main part relationship
pkg . addRelationship ( corePartName , TargetMode . INTERNAL , PackageRelationshipTypes . CORE_DOCUMENT , " rId1 " ) ;
// Create main document part
2008-07-29 18:02:09 -04:00
PackagePart corePart = pkg . createPart ( corePartName , workbookRelation . getContentType ( ) ) ;
2008-03-21 13:31:14 -04:00
OutputStream out ;
2008-01-16 11:08:22 -05:00
XmlOptions xmlOptions = new XmlOptions ( ) ;
2008-03-21 13:31:14 -04:00
// Requests use of whitespace for easier reading
xmlOptions . setSavePrettyPrint ( ) ;
xmlOptions . setSaveOuter ( ) ;
xmlOptions . setUseDefaultNamespace ( ) ;
// Write out our sheets, updating the references
// to them in the main workbook as we go
for ( int i = 0 ; i < this . getNumberOfSheets ( ) ; i + + ) {
int sheetNumber = ( i + 1 ) ;
XSSFSheet sheet = ( XSSFSheet ) this . getSheetAt ( i ) ;
2008-04-02 20:23:59 -04:00
PackagePartName partName = PackagingURIHelper . createPartName (
WORKSHEET . getFileName ( sheetNumber ) ) ;
2008-03-21 13:31:14 -04:00
PackageRelationship rel =
corePart . addRelationship ( partName , TargetMode . INTERNAL , WORKSHEET . getRelation ( ) , " rSheet " + sheetNumber ) ;
PackagePart part = pkg . createPart ( partName , WORKSHEET . getContentType ( ) ) ;
2008-01-16 11:08:22 -05:00
2008-03-21 13:31:14 -04:00
// XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
xmlOptions . setSaveSyntheticDocumentElement ( new QName ( CTWorksheet . type . getName ( ) . getNamespaceURI ( ) , " worksheet " ) ) ;
2008-04-05 17:43:53 -04:00
sheet . save ( part , xmlOptions ) ;
2008-01-16 11:08:22 -05:00
2008-03-21 13:31:14 -04:00
// Update our internal reference for the package part
workbook . getSheets ( ) . getSheetArray ( i ) . setId ( rel . getId ( ) ) ;
2008-03-21 15:54:35 -04:00
workbook . getSheets ( ) . getSheetArray ( i ) . setSheetId ( sheetNumber ) ;
2008-03-31 19:30:56 -04:00
// If our sheet has comments, then write out those
2008-04-02 20:23:59 -04:00
if ( sheet . hasComments ( ) ) {
CommentsTable ct = ( CommentsTable ) sheet . getCommentsSourceIfExists ( ) ;
PackagePartName ctName = PackagingURIHelper . createPartName (
SHEET_COMMENTS . getFileName ( sheetNumber ) ) ;
part . addRelationship ( ctName , TargetMode . INTERNAL , SHEET_COMMENTS . getRelation ( ) , " rComments " ) ;
PackagePart ctPart = pkg . createPart ( ctName , SHEET_COMMENTS . getContentType ( ) ) ;
out = ctPart . getOutputStream ( ) ;
ct . writeTo ( out ) ;
out . close ( ) ;
}
2008-07-29 19:03:25 -04:00
// If our sheet has drawings, then write out those
if ( sheet . getDrawings ( ) ! = null ) {
int drawingIndex = 1 ;
for ( Drawing drawing : sheet . getDrawings ( ) ) {
PackagePartName drName = PackagingURIHelper . createPartName (
VML_DRAWINGS . getFileName ( drawingIndex ) ) ;
part . addRelationship ( drName , TargetMode . INTERNAL , VML_DRAWINGS . getRelation ( ) , drawing . getOriginalId ( ) ) ;
PackagePart drPart = pkg . createPart ( drName , VML_DRAWINGS . getContentType ( ) ) ;
drawing . writeChildren ( drPart ) ;
out = drPart . getOutputStream ( ) ;
drawing . writeTo ( out ) ;
out . close ( ) ;
drawingIndex + + ;
}
}
// If our sheet has controls, then write out those
if ( sheet . getControls ( ) ! = null ) {
int controlIndex = 1 ;
for ( Control control : sheet . getControls ( ) ) {
PackagePartName crName = PackagingURIHelper . createPartName (
ACTIVEX_CONTROLS . getFileName ( controlIndex ) ) ;
part . addRelationship ( crName , TargetMode . INTERNAL , ACTIVEX_CONTROLS . getRelation ( ) , control . getOriginalId ( ) ) ;
PackagePart crPart = pkg . createPart ( crName , ACTIVEX_CONTROLS . getContentType ( ) ) ;
control . writeChildren ( crPart ) ;
out = crPart . getOutputStream ( ) ;
control . writeTo ( out ) ;
out . close ( ) ;
controlIndex + + ;
}
}
2008-03-21 13:31:14 -04:00
}
2008-01-16 11:08:22 -05:00
2008-03-21 13:31:14 -04:00
// Write shared strings and styles
if ( sharedStringSource ! = null ) {
2008-03-16 18:44:10 -04:00
SharedStringsTable sst = ( SharedStringsTable ) sharedStringSource ;
2008-03-16 19:30:51 -04:00
SHARED_STRINGS . save ( sst , corePart ) ;
2008-03-21 13:31:14 -04:00
}
if ( stylesSource ! = null ) {
2008-03-16 18:44:10 -04:00
StylesTable st = ( StylesTable ) stylesSource ;
2008-03-16 19:30:51 -04:00
STYLES . save ( st , corePart ) ;
2008-03-21 13:31:14 -04:00
}
2008-03-31 18:30:56 -04:00
// Named ranges
if ( namedRanges . size ( ) > 0 ) {
CTDefinedNames names = CTDefinedNames . Factory . newInstance ( ) ;
CTDefinedName [ ] nr = new CTDefinedName [ namedRanges . size ( ) ] ;
for ( int i = 0 ; i < namedRanges . size ( ) ; i + + ) {
nr [ i ] = namedRanges . get ( i ) . getCTName ( ) ;
}
names . setDefinedNameArray ( nr ) ;
workbook . setDefinedNames ( names ) ;
} else {
if ( workbook . isSetDefinedNames ( ) ) {
workbook . setDefinedNames ( null ) ;
}
}
2008-07-18 14:22:25 -04:00
2008-07-29 18:02:09 -04:00
// Macro related bits
if ( isMacroEnabled ) {
// Copy VBA Macros if present
if ( VBA_MACROS . exists ( getCorePart ( ) ) ) {
try {
XSSFModel vba = VBA_MACROS . load ( getCorePart ( ) ) ;
VBA_MACROS . save ( vba , corePart ) ;
} catch ( Exception e ) {
throw new RuntimeException ( " Unable to copy vba macros over " , e ) ;
}
}
2008-07-18 14:22:25 -04:00
}
2008-03-16 18:44:10 -04:00
2008-03-21 13:31:14 -04:00
// Now we can write out the main Workbook, with
// the correct references to the other parts
out = corePart . getOutputStream ( ) ;
// XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
xmlOptions . setSaveSyntheticDocumentElement ( new QName ( CTWorkbook . type . getName ( ) . getNamespaceURI ( ) , " workbook " ) ) ;
workbook . save ( out , xmlOptions ) ;
out . close ( ) ;
// All done
pkg . close ( ) ;
2008-01-16 11:08:22 -05:00
} catch ( InvalidFormatException e ) {
// TODO: replace with more meaningful exception
throw new RuntimeException ( e ) ;
}
}
public void writeProtectWorkbook ( String password , String username ) {
// TODO Auto-generated method stub
}
2008-02-13 08:43:42 -05:00
public SharedStringSource getSharedStringSource ( ) {
return this . sharedStringSource ;
}
protected void setSharedStringSource ( SharedStringSource sharedStringSource ) {
this . sharedStringSource = sharedStringSource ;
}
2008-03-16 18:07:38 -04:00
public StylesSource getStylesSource ( ) {
return this . stylesSource ;
}
protected void setStylesSource ( StylesSource stylesSource ) {
this . stylesSource = stylesSource ;
}
2008-02-13 08:43:42 -05:00
2008-03-16 12:51:58 -04:00
public CreationHelper getCreationHelper ( ) {
return new XSSFCreationHelper ( this ) ;
}
2008-01-16 11:08:22 -05:00
}