You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SingleXmlCells.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.model;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.List;
  20. import java.util.Vector;
  21. import org.apache.poi.POIXMLDocumentPart;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackageRelationship;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
  26. import org.apache.xmlbeans.XmlException;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSingleXmlCell;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSingleXmlCells;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.SingleXmlCellsDocument;
  30. /**
  31. *
  32. * This class implements the Single Cell Tables Part (Open Office XML Part 4:
  33. * chapter 3.5.2)
  34. *
  35. *
  36. * @author Roberto Manicardi
  37. */
  38. public class SingleXmlCells extends POIXMLDocumentPart {
  39. private CTSingleXmlCells singleXMLCells;
  40. public SingleXmlCells() {
  41. super();
  42. singleXMLCells = CTSingleXmlCells.Factory.newInstance();
  43. }
  44. public SingleXmlCells(PackagePart part, PackageRelationship rel)
  45. throws IOException {
  46. super(part, rel);
  47. readFrom(part.getInputStream());
  48. }
  49. public void readFrom(InputStream is) throws IOException {
  50. try {
  51. SingleXmlCellsDocument doc = SingleXmlCellsDocument.Factory.parse(is);
  52. singleXMLCells = doc.getSingleXmlCells();
  53. } catch (XmlException e) {
  54. throw new IOException(e.getLocalizedMessage());
  55. }
  56. }
  57. public XSSFSheet getXSSFSheet(){
  58. return (XSSFSheet) getParent();
  59. }
  60. protected void writeTo(OutputStream out) throws IOException {
  61. SingleXmlCellsDocument doc = SingleXmlCellsDocument.Factory.newInstance();
  62. doc.setSingleXmlCells(singleXMLCells);
  63. doc.save(out, DEFAULT_XML_OPTIONS);
  64. }
  65. @Override
  66. protected void commit() throws IOException {
  67. PackagePart part = getPackagePart();
  68. OutputStream out = part.getOutputStream();
  69. writeTo(out);
  70. out.close();
  71. }
  72. public CTSingleXmlCells getCTSingleXMLCells(){
  73. return singleXMLCells;
  74. }
  75. /**
  76. *
  77. * @return all the SimpleXmlCell contained in this SingleXmlCells element
  78. */
  79. @SuppressWarnings("deprecation")
  80. public List<XSSFSingleXmlCell> getAllSimpleXmlCell(){
  81. List<XSSFSingleXmlCell> list = new Vector<XSSFSingleXmlCell>();
  82. for(CTSingleXmlCell singleXmlCell: singleXMLCells.getSingleXmlCellArray()){
  83. list.add(new XSSFSingleXmlCell(singleXmlCell,this));
  84. }
  85. return list;
  86. }
  87. }