001package com.hfg.xml.msofficexml.xlsx; 002 003 004import java.io.BufferedInputStream; 005import java.util.ArrayList; 006import java.util.List; 007 008import com.hfg.util.collection.CollectionUtil; 009import com.hfg.util.collection.OrderedMap; 010import com.hfg.util.StringUtil; 011import com.hfg.xml.XMLTag; 012import com.hfg.xml.msofficexml.OfficeOpenXmlException; 013import com.hfg.xml.msofficexml.xlsx.part.WorksheetPart; 014import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorkbookProperties; 015import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorksheet; 016 017 018//------------------------------------------------------------------------------ 019/** 020 Represents an Office Open XML Excel workbook. 021 022 @author J. Alex Taylor, hairyfatguy.com 023 */ 024//------------------------------------------------------------------------------ 025// com.hfg XML/HTML Coding Library 026// 027// This library is free software; you can redistribute it and/or 028// modify it under the terms of the GNU Lesser General Public 029// License as published by the Free Software Foundation; either 030// version 2.1 of the License, or (at your option) any later version. 031// 032// This library is distributed in the hope that it will be useful, 033// but WITHOUT ANY WARRANTY; without even the implied warranty of 034// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 035// Lesser General Public License for more details. 036// 037// You should have received a copy of the GNU Lesser General Public 038// License along with this library; if not, write to the Free Software 039// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 040// 041// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 042// jataylor@hairyfatguy.com 043//------------------------------------------------------------------------------ 044 045public class Workbook 046{ 047 private Xlsx mParentDoc; 048 private OrderedMap<String, WorksheetPart> mWorksheets = new OrderedMap<>(10); 049 050 //########################################################################### 051 // CONSTRUCTORS 052 //########################################################################### 053 054 //--------------------------------------------------------------------------- 055 protected Workbook(Xlsx inParentDoc) 056 { 057 mParentDoc = inParentDoc; 058 } 059 060 //########################################################################### 061 // PUBLIC METHODS 062 //########################################################################### 063 064 //--------------------------------------------------------------------------- 065 public Xlsx getParentDoc() 066 { 067 return mParentDoc; 068 } 069 070 //--------------------------------------------------------------------------- 071 public SsmlWorkbookProperties getProperties() 072 { 073 return getParentDoc().getWorkbookPart().getRootNode().getProperties(); 074 } 075 076 //--------------------------------------------------------------------------- 077 public SsmlWorksheet addWorksheet() 078 { 079 return addWorksheet((String) null); 080 } 081 082 //--------------------------------------------------------------------------- 083 public SsmlWorksheet addWorksheet(String inName) 084 { 085 WorksheetPart worksheetPart = new WorksheetPart(getParentDoc()).setSheetIndex(mWorksheets.size() + 1); 086 if (StringUtil.isSet(inName)) 087 { 088 worksheetPart.getRootNode().setName(inName); 089 } 090 091 addWorksheetPart(worksheetPart); 092 093 return worksheetPart.getRootNode(); 094 } 095/* 096 //--------------------------------------------------------------------------- 097 public boolean updateWorksheetName(WorksheetPart inWorksheetPart, String inNewName) 098 { 099 boolean result = false; 100 WorksheetPart worksheetPart = mWorksheets.get(inName); 101 if (worksheetPart != null) 102 { 103 getParentDoc().getWorkbookPart().getRootNode(). 104 } 105 return mWorksheets.remove(inName); 106 } 107*/ 108 //--------------------------------------------------------------------------- 109 public SsmlWorksheet addWorksheet(String inName, Integer inSheetIndex, XMLTag inSheetTag) 110 { 111 WorksheetPart worksheetPart = new WorksheetPart(getParentDoc(), inName, inSheetTag).setSheetIndex(inSheetIndex != null ? inSheetIndex : mWorksheets.size() + 1); 112 113 addWorksheetPart(worksheetPart); 114 115 return worksheetPart.getRootNode(); 116 } 117 118/* 119 //--------------------------------------------------------------------------- 120 public Workbook addWorksheet(SsmlWorksheet inValue) 121 { 122 WorksheetPart worksheetPart = new WorksheetPart(getParentDoc()).setSheetIndex(mWorksheets.size() + 1); 123 worksheetPart.setRootNode(inValue); 124 125 addWorksheetPart(worksheetPart); 126 127 return this; 128 } 129*/ 130 //--------------------------------------------------------------------------- 131 public void addWorksheetPart(WorksheetPart inValue) 132 { 133 if (inValue != null) 134 { 135 int sheetIndex = mWorksheets.size() + 1; 136 inValue.setSheetIndex(sheetIndex); 137 138 SsmlWorksheet worksheet = inValue.getRootNode(); 139 140 // Ensure that the sheet has a unique name 141 if (! StringUtil.isSet(worksheet.getName())) 142 { 143 worksheet.setName("Sheet" + sheetIndex); 144 } 145 146 if (mWorksheets.containsKey(worksheet.getName())) 147 { 148 throw new OfficeOpenXmlException("The sheet name " + StringUtil.singleQuote(worksheet.getName()) + " must be unique!"); 149 } 150 151 mWorksheets.put(worksheet.getName(), inValue); 152 153 String relationshipId = getParentDoc().getWorkbookRelationshipPart().addWorksheet(inValue); 154 155 getParentDoc().getWorkbookPart().getRootNode().addSheet(worksheet.getName(), relationshipId); 156 } 157 } 158 159 //--------------------------------------------------------------------------- 160 /** 161 Returns all worksheets. 162 */ 163 public List<SsmlWorksheet> getWorksheets() 164 { 165 ArrayList<SsmlWorksheet> worksheets = new ArrayList<>(mWorksheets.size()); 166 if (CollectionUtil.hasValues(mWorksheets)) 167 { 168 for (WorksheetPart part : mWorksheets.values()) 169 { 170 worksheets.add(part.getRootNode()); 171 } 172 } 173 174 return worksheets; 175 } 176 177 //--------------------------------------------------------------------------- 178 /** 179 Returns a worksheet by name. 180 * @param inWorksheetName the name of the worksheet 181 * @return SmlWorksheet 182 */ 183 public SsmlWorksheet getWorksheet(String inWorksheetName) 184 { 185 WorksheetPart part = mWorksheets.get(inWorksheetName); 186 return (part != null ? part.getRootNode() : null); 187 } 188 189}