/src/xlsx4j/java/org/docx4j/openpackaging/packages/SpreadsheetMLPackage.java

http://github.com/plutext/docx4j · Java · 219 lines · 114 code · 44 blank · 61 comment · 8 complexity · ab0a96157af69c65235a9c02cc81b80f MD5 · raw file

  1. /*
  2. * Copyright 2010, Plutext Pty Ltd.
  3. *
  4. * This file is part of docx4j.
  5. docx4j is licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. 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.docx4j.openpackaging.packages;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import javax.xml.bind.JAXBException;
  22. import org.apache.log4j.Logger;
  23. import org.docx4j.XmlUtils;
  24. import org.docx4j.model.styles.StyleTree;
  25. import org.docx4j.openpackaging.contenttype.ContentType;
  26. import org.docx4j.openpackaging.contenttype.ContentTypeManager;
  27. import org.docx4j.openpackaging.contenttype.ContentTypes;
  28. import org.docx4j.openpackaging.exceptions.Docx4JException;
  29. import org.docx4j.openpackaging.exceptions.InvalidFormatException;
  30. import org.docx4j.openpackaging.io.SaveToZipFile;
  31. import org.docx4j.openpackaging.parts.DocPropsCorePart;
  32. import org.docx4j.openpackaging.parts.DocPropsCustomPart;
  33. import org.docx4j.openpackaging.parts.DocPropsExtendedPart;
  34. import org.docx4j.openpackaging.parts.Part;
  35. import org.docx4j.openpackaging.parts.PartName;
  36. import org.docx4j.openpackaging.parts.ThemePart;
  37. import org.docx4j.openpackaging.parts.SpreadsheetML.WorkbookPart;
  38. import org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart;
  39. import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
  40. import org.docx4j.openpackaging.parts.relationships.Namespaces;
  41. import org.docx4j.relationships.Relationship;
  42. import org.xlsx4j.jaxb.Context;
  43. import org.xlsx4j.sml.Sheet;
  44. import org.xlsx4j.sml.SheetData;
  45. import org.xlsx4j.sml.Sheets;
  46. import org.xlsx4j.sml.Worksheet;
  47. /**
  48. * @author jharrop
  49. *
  50. */
  51. public class SpreadsheetMLPackage extends OpcPackage {
  52. protected static Logger log = Logger.getLogger(PresentationMLPackage.class);
  53. /**
  54. * Constructor. Also creates a new content type manager
  55. *
  56. */
  57. public SpreadsheetMLPackage() {
  58. super();
  59. setContentType(new ContentType(ContentTypes.PRESENTATIONML_MAIN));
  60. }
  61. /**
  62. * Constructor.
  63. *
  64. * @param contentTypeManager
  65. * The content type manager to use
  66. */
  67. public SpreadsheetMLPackage(ContentTypeManager contentTypeManager) {
  68. super(contentTypeManager);
  69. setContentType(new ContentType(ContentTypes.PRESENTATIONML_MAIN));
  70. }
  71. // Workbook part
  72. WorkbookPart wb;
  73. public WorkbookPart getWorkbookPart() {
  74. return wb;
  75. }
  76. /**
  77. * Convenience method to create a SpreadsheetMLPackage
  78. * from an existing File (.xlsx or .xml Flat OPC).
  79. *
  80. * @param xlsxFile
  81. * The xlsx file
  82. */
  83. public static SpreadsheetMLPackage load(java.io.File xlsxFile) throws Docx4JException {
  84. return (SpreadsheetMLPackage)OpcPackage.load(xlsxFile);
  85. }
  86. public boolean setPartShortcut(Part part, String relationshipType) {
  87. if (relationshipType.equals(Namespaces.PROPERTIES_CORE)) {
  88. docPropsCorePart = (DocPropsCorePart)part;
  89. log.info("Set shortcut for docPropsCorePart");
  90. return true;
  91. } else if (relationshipType.equals(Namespaces.PROPERTIES_EXTENDED)) {
  92. docPropsExtendedPart = (DocPropsExtendedPart)part;
  93. log.info("Set shortcut for docPropsExtendedPart");
  94. return true;
  95. } else if (relationshipType.equals(Namespaces.PROPERTIES_CUSTOM)) {
  96. docPropsCustomPart = (DocPropsCustomPart)part;
  97. log.info("Set shortcut for docPropsCustomPart");
  98. return true;
  99. } else if (relationshipType.equals(Namespaces.SPREADSHEETML_WORKBOOK)) {
  100. wb = (WorkbookPart)part;
  101. log.info("Set shortcut for WorkbookPart");
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. }
  107. /**
  108. * Create an empty presentation.
  109. *
  110. * @return
  111. * @throws InvalidFormatException
  112. */
  113. public static SpreadsheetMLPackage createPackage() throws InvalidFormatException {
  114. // Create a package
  115. SpreadsheetMLPackage xlsPack = new SpreadsheetMLPackage();
  116. try {
  117. xlsPack.wb = new WorkbookPart();
  118. xlsPack.wb.setJaxbElement(
  119. Context.getsmlObjectFactory().createWorkbook()
  120. );
  121. xlsPack.addTargetPart(xlsPack.wb);
  122. xlsPack.wb.getJaxbElement().setSheets(
  123. Context.getsmlObjectFactory().createSheets()
  124. );
  125. } catch (Exception e) {
  126. e.printStackTrace();
  127. throw new InvalidFormatException("Couldn't create package", e);
  128. }
  129. // Return the new package
  130. return xlsPack;
  131. }
  132. /**
  133. * Create a worksheet and add it to the package
  134. *
  135. * @param wb
  136. * @param partName
  137. * @param sheetName
  138. * @param sheetId
  139. * @return
  140. * @throws InvalidFormatException
  141. * @throws JAXBException
  142. */
  143. public WorksheetPart createWorksheetPart(PartName partName,
  144. String sheetName, long sheetId)
  145. throws InvalidFormatException, JAXBException {
  146. WorksheetPart worksheetPart = new WorksheetPart(partName);
  147. Relationship r = wb.addTargetPart(worksheetPart);
  148. Sheets sheets = wb.getJaxbElement().getSheets();
  149. Sheet s = Context.getsmlObjectFactory().createSheet();
  150. s.setName(sheetName);
  151. s.setId(r.getId());
  152. s.setSheetId(sheetId);
  153. sheets.getSheet().add(s);
  154. // minimal content for the part
  155. Worksheet ws = Context.getsmlObjectFactory().createWorksheet();
  156. worksheetPart.setJaxbElement(ws);
  157. ws.setSheetData(
  158. Context.getsmlObjectFactory().createSheetData()
  159. );
  160. return worksheetPart;
  161. }
  162. public static void main(String[] args) throws Exception {
  163. String outputfilepath = System.getProperty("user.dir") + "/sample-docs/xlsx/test-out.xlsx";
  164. SpreadsheetMLPackage pkg = createPackage();
  165. pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
  166. SaveToZipFile saver = new SaveToZipFile(pkg);
  167. saver.save(outputfilepath);
  168. System.out.println("\n\n done .. " + outputfilepath);
  169. }
  170. }