Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

XSSFReader.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.eventusermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import org.apache.poi.POIXMLException;
  23. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  24. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  25. import org.apache.poi.openxml4j.opc.OPCPackage;
  26. import org.apache.poi.openxml4j.opc.PackagePart;
  27. import org.apache.poi.openxml4j.opc.PackagePartName;
  28. import org.apache.poi.openxml4j.opc.PackageRelationship;
  29. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  30. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  31. import org.apache.poi.xssf.model.SharedStringsTable;
  32. import org.apache.poi.xssf.model.StylesTable;
  33. import org.apache.poi.xssf.usermodel.XSSFRelation;
  34. import org.apache.xmlbeans.XmlException;
  35. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
  36. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
  37. import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
  38. /**
  39. * This class makes it easy to get at individual parts
  40. * of an OOXML .xlsx file, suitable for low memory sax
  41. * parsing or similar.
  42. * It makes up the core part of the EventUserModel support
  43. * for XSSF.
  44. */
  45. public class XSSFReader {
  46. private OPCPackage pkg;
  47. private PackagePart workbookPart;
  48. /**
  49. * Creates a new XSSFReader, for the given package
  50. */
  51. public XSSFReader(OPCPackage pkg) throws IOException, OpenXML4JException {
  52. this.pkg = pkg;
  53. PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
  54. PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
  55. // Get the part that holds the workbook
  56. workbookPart = this.pkg.getPart(coreDocRelationship);
  57. }
  58. /**
  59. * Opens up the Shared Strings Table, parses it, and
  60. * returns a handy object for working with
  61. * shared strings.
  62. */
  63. public SharedStringsTable getSharedStringsTable() throws IOException, InvalidFormatException {
  64. ArrayList<PackagePart> parts = pkg.getPartsByContentType( XSSFRelation.SHARED_STRINGS.getContentType());
  65. return parts.size() == 0 ? null : new SharedStringsTable(parts.get(0), null);
  66. }
  67. /**
  68. * Opens up the Styles Table, parses it, and
  69. * returns a handy object for working with cell styles
  70. */
  71. public StylesTable getStylesTable() throws IOException, InvalidFormatException {
  72. ArrayList<PackagePart> parts = pkg.getPartsByContentType( XSSFRelation.STYLES.getContentType());
  73. return parts.size() == 0 ? null : new StylesTable(parts.get(0), null);
  74. }
  75. /**
  76. * Returns an InputStream to read the contents of the
  77. * shared strings table.
  78. */
  79. public InputStream getSharedStringsData() throws IOException, InvalidFormatException {
  80. return XSSFRelation.SHARED_STRINGS.getContents(workbookPart);
  81. }
  82. /**
  83. * Returns an InputStream to read the contents of the
  84. * styles table.
  85. */
  86. public InputStream getStylesData() throws IOException, InvalidFormatException {
  87. return XSSFRelation.STYLES.getContents(workbookPart);
  88. }
  89. /**
  90. * Returns an InputStream to read the contents of the
  91. * main Workbook, which contains key overall data for
  92. * the file, including sheet definitions.
  93. */
  94. public InputStream getWorkbookData() throws IOException, InvalidFormatException {
  95. return workbookPart.getInputStream();
  96. }
  97. /**
  98. * Returns an InputStream to read the contents of the
  99. * specified Sheet.
  100. * @param relId The relationId of the sheet, from a r:id on the workbook
  101. */
  102. public InputStream getSheet(String relId) throws IOException, InvalidFormatException {
  103. PackageRelationship rel = workbookPart.getRelationship(relId);
  104. if(rel == null) {
  105. throw new IllegalArgumentException("No Sheet found with r:id " + relId);
  106. }
  107. PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
  108. PackagePart sheet = pkg.getPart(relName);
  109. if(sheet == null) {
  110. throw new IllegalArgumentException("No data found for Sheet with r:id " + relId);
  111. }
  112. return sheet.getInputStream();
  113. }
  114. /**
  115. * Returns an Iterator which will let you get at all the
  116. * different Sheets in turn.
  117. * Each sheet's InputStream is only opened when fetched
  118. * from the Iterator. It's up to you to close the
  119. * InputStreams when done with each one.
  120. */
  121. public Iterator<InputStream> getSheetsData() throws IOException, InvalidFormatException {
  122. return new SheetIterator(workbookPart);
  123. }
  124. /**
  125. * Iterator over sheet data.
  126. */
  127. public static class SheetIterator implements Iterator<InputStream> {
  128. /**
  129. * Maps relId and the corresponding PackagePart
  130. */
  131. private Map<String, PackagePart> sheetMap;
  132. /**
  133. * Current CTSheet bean
  134. */
  135. private CTSheet ctSheet;
  136. /**
  137. * Iterator over CTSheet objects, returns sheets in <tt>logical</tt> order.
  138. * We can't rely on the Ooxml4J's relationship iterator because it returns objects in physical order,
  139. * i.e. as they are stored in the underlying package
  140. */
  141. private Iterator<CTSheet> sheetIterator;
  142. /**
  143. * Construct a new SheetIterator
  144. *
  145. * @param wb package part holding workbook.xml
  146. */
  147. private SheetIterator(PackagePart wb) throws IOException {
  148. /**
  149. * The order of sheets is defined by the order of CTSheet elements in workbook.xml
  150. */
  151. try {
  152. //step 1. Map sheet's relationship Id and the corresponding PackagePart
  153. sheetMap = new HashMap<String, PackagePart>();
  154. for(PackageRelationship rel : wb.getRelationships()){
  155. if(rel.getRelationshipType().equals(XSSFRelation.WORKSHEET.getRelation()) ||
  156. rel.getRelationshipType().equals(XSSFRelation.CHARTSHEET.getRelation())){
  157. PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
  158. sheetMap.put(rel.getId(), wb.getPackage().getPart(relName));
  159. }
  160. }
  161. //step 2. Read array of CTSheet elements, wrap it in a ArayList and construct an iterator
  162. //Note, using XMLBeans might be expensive, consider refactoring to use SAX or a plain regexp search
  163. CTWorkbook wbBean = WorkbookDocument.Factory.parse(wb.getInputStream()).getWorkbook();
  164. sheetIterator = wbBean.getSheets().getSheetList().iterator();
  165. } catch (InvalidFormatException e){
  166. throw new POIXMLException(e);
  167. } catch (XmlException e){
  168. throw new POIXMLException(e);
  169. }
  170. }
  171. /**
  172. * Returns <tt>true</tt> if the iteration has more elements.
  173. *
  174. * @return <tt>true</tt> if the iterator has more elements.
  175. */
  176. public boolean hasNext() {
  177. return sheetIterator.hasNext();
  178. }
  179. /**
  180. * Returns input stream of the next sheet in the iteration
  181. *
  182. * @return input stream of the next sheet in the iteration
  183. */
  184. public InputStream next() {
  185. ctSheet = sheetIterator.next();
  186. String sheetId = ctSheet.getId();
  187. try {
  188. PackagePart sheetPkg = sheetMap.get(sheetId);
  189. return sheetPkg.getInputStream();
  190. } catch(IOException e) {
  191. throw new POIXMLException(e);
  192. }
  193. }
  194. /**
  195. * Returns name of the current sheet
  196. *
  197. * @return name of the current sheet
  198. */
  199. public String getSheetName() {
  200. return ctSheet.getName();
  201. }
  202. public void remove() {
  203. throw new IllegalStateException("Not supported");
  204. }
  205. }
  206. }