Search in sources :

Example 1 with CTSheet

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet in project poi by apache.

the class HybridStreaming method main.

public static void main(String[] args) throws IOException, SAXException {
    InputStream sourceBytes = new FileInputStream("workbook.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {

        /** Avoid DOM parse of large sheet */
        @Override
        public void parseSheet(java.util.Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
            if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
                super.parseSheet(shIdMap, ctSheet);
            }
        }
    };
    // Having avoided a DOM-based parse of the sheet, we can stream it instead.
    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
    new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
    workbook.close();
    sourceBytes.close();
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) FileInputStream(java.io.FileInputStream) XSSFSheetXMLHandler(org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler)

Example 2 with CTSheet

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet in project poi by apache.

the class XSSFWorkbook method isSheetHidden.

@Override
public boolean isSheetHidden(int sheetIx) {
    validateSheetIndex(sheetIx);
    CTSheet ctSheet = sheets.get(sheetIx).sheet;
    return ctSheet.getState() == STSheetState.HIDDEN;
}
Also used : CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet)

Example 3 with CTSheet

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet in project poi by apache.

the class XSSFWorkbook method getSheetVisibility.

@Override
public SheetVisibility getSheetVisibility(int sheetIx) {
    validateSheetIndex(sheetIx);
    final CTSheet ctSheet = sheets.get(sheetIx).sheet;
    final STSheetState.Enum state = ctSheet.getState();
    if (state == STSheetState.VISIBLE) {
        return SheetVisibility.VISIBLE;
    }
    if (state == STSheetState.HIDDEN) {
        return SheetVisibility.HIDDEN;
    }
    if (state == STSheetState.VERY_HIDDEN) {
        return SheetVisibility.VERY_HIDDEN;
    }
    throw new IllegalArgumentException("This should never happen");
}
Also used : CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) STSheetState(org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState)

Example 4 with CTSheet

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet in project poi by apache.

the class XSSFWorkbook method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    try {
        WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        this.workbook = doc.getWorkbook();
        ThemesTable theme = null;
        Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
        Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof SharedStringsTable) {
                sharedStringSource = (SharedStringsTable) p;
            } else if (p instanceof StylesTable) {
                stylesSource = (StylesTable) p;
            } else if (p instanceof ThemesTable) {
                theme = (ThemesTable) p;
            } else if (p instanceof CalculationChain) {
                calcChain = (CalculationChain) p;
            } else if (p instanceof MapInfo) {
                mapInfo = (MapInfo) p;
            } else if (p instanceof XSSFSheet) {
                shIdMap.put(rp.getRelationship().getId(), (XSSFSheet) p);
            } else if (p instanceof ExternalLinksTable) {
                elIdMap.put(rp.getRelationship().getId(), (ExternalLinksTable) p);
            }
        }
        boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
        if (stylesSource == null) {
            // Create Styles if it is missing
            if (packageReadOnly) {
                stylesSource = new StylesTable();
            } else {
                stylesSource = (StylesTable) createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
            }
        }
        stylesSource.setWorkbook(this);
        stylesSource.setTheme(theme);
        if (sharedStringSource == null) {
            // Create SST if it is missing
            if (packageReadOnly) {
                sharedStringSource = new SharedStringsTable();
            } else {
                sharedStringSource = (SharedStringsTable) createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
            }
        }
        // Load individual sheets. The order of sheets is defined by the order
        //  of CTSheet elements in the workbook
        sheets = new ArrayList<XSSFSheet>(shIdMap.size());
        for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
            parseSheet(shIdMap, ctSheet);
        }
        // Load the external links tables. Their order is defined by the order 
        //  of CTExternalReference elements in the workbook
        externalLinks = new ArrayList<ExternalLinksTable>(elIdMap.size());
        if (this.workbook.isSetExternalReferences()) {
            for (CTExternalReference er : this.workbook.getExternalReferences().getExternalReferenceArray()) {
                ExternalLinksTable el = elIdMap.get(er.getId());
                if (el == null) {
                    logger.log(POILogger.WARN, "ExternalLinksTable with r:id " + er.getId() + " was defined, but didn't exist in package, skipping");
                    continue;
                }
                externalLinks.add(el);
            }
        }
        // Process the named ranges
        reprocessNamedRanges();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : WorkbookDocument(org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument) ThemesTable(org.apache.poi.xssf.model.ThemesTable) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) CTExternalReference(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) StylesTable(org.apache.poi.xssf.model.StylesTable) POIXMLException(org.apache.poi.POIXMLException) CalculationChain(org.apache.poi.xssf.model.CalculationChain) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) XmlException(org.apache.xmlbeans.XmlException) MapInfo(org.apache.poi.xssf.model.MapInfo) ExternalLinksTable(org.apache.poi.xssf.model.ExternalLinksTable)

Example 5 with CTSheet

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet in project poi by apache.

the class XSSFWorkbook method setSheetVisibility.

@Override
public void setSheetVisibility(int sheetIx, SheetVisibility visibility) {
    validateSheetIndex(sheetIx);
    final CTSheet ctSheet = sheets.get(sheetIx).sheet;
    switch(visibility) {
        case VISIBLE:
            ctSheet.setState(STSheetState.VISIBLE);
            break;
        case HIDDEN:
            ctSheet.setState(STSheetState.HIDDEN);
            break;
        case VERY_HIDDEN:
            ctSheet.setState(STSheetState.VERY_HIDDEN);
            break;
        default:
            throw new IllegalArgumentException("This should never happen");
    }
}
Also used : CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet)

Aggregations

CTSheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet)9 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)1 POIXMLException (org.apache.poi.POIXMLException)1 ReadOnlySharedStringsTable (org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable)1 XSSFSheetXMLHandler (org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler)1 CalculationChain (org.apache.poi.xssf.model.CalculationChain)1 ExternalLinksTable (org.apache.poi.xssf.model.ExternalLinksTable)1 MapInfo (org.apache.poi.xssf.model.MapInfo)1 SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)1 StylesTable (org.apache.poi.xssf.model.StylesTable)1 ThemesTable (org.apache.poi.xssf.model.ThemesTable)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 XmlException (org.apache.xmlbeans.XmlException)1 XmlObject (org.apache.xmlbeans.XmlObject)1 CTDefinedName (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName)1