All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.xlsx.Workbook Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.xlsx;


import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;

import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedMap;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.OfficeOpenXmlException;
import com.hfg.xml.msofficexml.xlsx.part.WorksheetPart;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorkbookProperties;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorksheet;


//------------------------------------------------------------------------------
/**
 Represents an Office Open XML Excel workbook.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class Workbook
{
   private Xlsx mParentDoc;
   private OrderedMap mWorksheets = new OrderedMap<>(10);

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   protected Workbook(Xlsx inParentDoc)
   {
      mParentDoc = inParentDoc;
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public Xlsx getParentDoc()
   {
      return mParentDoc;
   }

   //---------------------------------------------------------------------------
   public SsmlWorkbookProperties getProperties()
   {
      return getParentDoc().getWorkbookPart().getRootNode().getProperties();
   }

   //---------------------------------------------------------------------------
   public SsmlWorksheet addWorksheet()
   {
      return addWorksheet((String) null);
   }

   //---------------------------------------------------------------------------
   public SsmlWorksheet addWorksheet(String inName)
   {
      WorksheetPart worksheetPart = new WorksheetPart(getParentDoc()).setSheetIndex(mWorksheets.size() + 1);
      if (StringUtil.isSet(inName))
      {
         worksheetPart.getRootNode().setName(inName);
      }

      addWorksheetPart(worksheetPart);

      return worksheetPart.getRootNode();
   }
/*
   //---------------------------------------------------------------------------
   public boolean updateWorksheetName(WorksheetPart inWorksheetPart, String inNewName)
   {
      boolean result = false;
      WorksheetPart worksheetPart = mWorksheets.get(inName);
      if (worksheetPart != null)
      {
         getParentDoc().getWorkbookPart().getRootNode().
      }
      return mWorksheets.remove(inName);
   }
*/
   //---------------------------------------------------------------------------
   public SsmlWorksheet addWorksheet(String inName, Integer inSheetIndex, XMLTag inSheetTag)
   {
      WorksheetPart worksheetPart = new WorksheetPart(getParentDoc(), inName, inSheetTag).setSheetIndex(inSheetIndex != null ? inSheetIndex : mWorksheets.size() + 1);

      addWorksheetPart(worksheetPart);

      return worksheetPart.getRootNode();
   }

/*
   //---------------------------------------------------------------------------
   public Workbook addWorksheet(SsmlWorksheet inValue)
   {
      WorksheetPart worksheetPart = new WorksheetPart(getParentDoc()).setSheetIndex(mWorksheets.size() + 1);
      worksheetPart.setRootNode(inValue);

      addWorksheetPart(worksheetPart);

      return this;
   }
*/
   //---------------------------------------------------------------------------
   public void addWorksheetPart(WorksheetPart inValue)
   {
      if (inValue != null)
      {
         int sheetIndex = mWorksheets.size() + 1;
         inValue.setSheetIndex(sheetIndex);

         SsmlWorksheet worksheet = inValue.getRootNode();

         // Ensure that the sheet has a unique name
         if (! StringUtil.isSet(worksheet.getName()))
         {
            worksheet.setName("Sheet" + sheetIndex);
         }

         if (mWorksheets.containsKey(worksheet.getName()))
         {
            throw new OfficeOpenXmlException("The sheet name " + StringUtil.singleQuote(worksheet.getName()) + " must be unique!");
         }

         mWorksheets.put(worksheet.getName(), inValue);

         String relationshipId = getParentDoc().getWorkbookRelationshipPart().addWorksheet(inValue);

         getParentDoc().getWorkbookPart().getRootNode().addSheet(worksheet.getName(), relationshipId);
      }
   }

   //---------------------------------------------------------------------------
   /**
    Returns all worksheets.
    */
   public List getWorksheets()
   {
      ArrayList worksheets = new ArrayList<>(mWorksheets.size());
      if (CollectionUtil.hasValues(mWorksheets))
      {
         for (WorksheetPart part : mWorksheets.values())
         {
            worksheets.add(part.getRootNode());
         }
      }

      return worksheets;
   }

   //---------------------------------------------------------------------------
   /**
    Returns a worksheet by name.
    * @param inWorksheetName the name of the worksheet
    * @return SmlWorksheet
    */
   public SsmlWorksheet getWorksheet(String inWorksheetName)
   {
      WorksheetPart part =  mWorksheets.get(inWorksheetName);
      return (part != null ? part.getRootNode() : null);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy