Beispiel #1
0
        internal override void OnDocumentRead()
        {
            try
            {
                XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream());
                doc = WorkbookDocument.Parse(xmldoc, NamespaceManager);
                this.workbook = doc.Workbook;

                Dictionary<String, XSSFSheet> shIdMap = new Dictionary<String, XSSFSheet>();
                Dictionary<String, ExternalLinksTable> elIdMap = new Dictionary<String, ExternalLinksTable>();
                foreach (POIXMLDocumentPart p in GetRelations())
                {
                    if (p is SharedStringsTable) sharedStringSource = (SharedStringsTable)p;
                    else if (p is StylesTable) stylesSource = (StylesTable)p;
                    else if (p is ThemesTable) theme = (ThemesTable)p;
                    else if (p is CalculationChain) calcChain = (CalculationChain)p;
                    else if (p is MapInfo) mapInfo = (MapInfo)p;
                    else if (p is XSSFSheet)
                    {
                        shIdMap.Add(p.GetPackageRelationship().Id,(XSSFSheet)p);
                    }
                    else if (p is ExternalLinksTable)
                    {
                        elIdMap.Add(p.GetPackageRelationship().Id, (ExternalLinksTable)p);
                    }
                }

                bool packageReadOnly = (Package.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.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 List<XSSFSheet>(shIdMap.Count);
                foreach (CT_Sheet ctSheet in this.workbook.sheets.sheet)
                {
                    XSSFSheet sh = null;
                    if(shIdMap.ContainsKey(ctSheet.id))
                        sh = shIdMap[ctSheet.id];
                    if (sh == null)
                    {
                        logger.Log(POILogger.WARN, "Sheet with name " + ctSheet.name + " and r:id " + ctSheet.id + " was defined, but didn't exist in package, skipping");
                        continue;
                    }
                    sh.sheet = ctSheet;
                    sh.OnDocumentRead();
                    sheets.Add(sh);
                }
                // Load the external links tables. Their order is defined by the order 
                //  of CTExternalReference elements in the workbook
                externalLinks = new List<ExternalLinksTable>(elIdMap.Count);
                if (this.workbook.IsSetExternalReferences())
                {
                    foreach (CT_ExternalReference er in this.workbook.externalReferences.externalReference)
                    {
                        ExternalLinksTable el = null;
                        if(elIdMap.ContainsKey(er.id))
                            el = elIdMap[(er.id)];
                        if (el == null)
                        {
                            logger.Log(POILogger.WARN, "ExternalLinksTable with r:id " + er.id + " 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);
            }
        }
Beispiel #2
0
        /**
         * Create a new CT_Workbook with all values Set to default
         */
        private void OnWorkbookCreate()
        {

            doc = new WorkbookDocument();
            workbook = doc.Workbook;
            // don't EVER use the 1904 date system
            CT_WorkbookPr workbookPr = workbook.AddNewWorkbookPr();
            workbookPr.date1904 = (false);

            CT_BookViews bvs = workbook.AddNewBookViews();
            CT_BookView bv = bvs.AddNewWorkbookView();
            bv.activeTab = 0;
            workbook.AddNewSheets();

            ExtendedProperties expProps = GetProperties().ExtendedProperties;
            CT_ExtendedProperties ctExtendedProp= expProps.GetUnderlyingProperties();
            ctExtendedProp.Application = DOCUMENT_CREATOR;
            ctExtendedProp.DocSecurity = 0;
            ctExtendedProp.DocSecuritySpecified = true;
            ctExtendedProp.ScaleCrop = false;
            ctExtendedProp.ScaleCropSpecified = true;
            ctExtendedProp.LinksUpToDate = false;
            ctExtendedProp.LinksUpToDateSpecified = true;
            ctExtendedProp.HyperlinksChanged = false;
            ctExtendedProp.HyperlinksChangedSpecified = true;
            ctExtendedProp.SharedDoc = false;
            ctExtendedProp.SharedDocSpecified = true;

            sharedStringSource = (SharedStringsTable)CreateRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.GetInstance());
            stylesSource = (StylesTable)CreateRelationship(XSSFRelation.STYLES, XSSFFactory.GetInstance());

            namedRanges = new List<XSSFName>();
            sheets = new List<XSSFSheet>();

            pivotTables = new List<XSSFPivotTable>();
        }
Beispiel #3
0
        internal override void OnDocumentRead()
        {
            try
            {
                XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream());
                doc = WorkbookDocument.Parse(xmldoc, NamespaceManager);
                this.workbook = doc.Workbook;

                Dictionary<String, XSSFSheet> shIdMap = new Dictionary<String, XSSFSheet>();
                foreach (POIXMLDocumentPart p in GetRelations())
                {
                    if (p is SharedStringsTable) sharedStringSource = (SharedStringsTable)p;
                    else if (p is StylesTable) stylesSource = (StylesTable)p;
                    else if (p is ThemesTable) theme = (ThemesTable)p;
                    else if (p is CalculationChain) calcChain = (CalculationChain)p;
                    else if (p is MapInfo) mapInfo = (MapInfo)p;
                    else if (p is XSSFSheet)
                    {
                        shIdMap.Add(p.GetPackageRelationship().Id,(XSSFSheet)p);
                    }
                }
                if (null != stylesSource) { stylesSource.SetTheme(theme); }

                if (sharedStringSource == null)
                {
                    //Create SST if it is missing
                    sharedStringSource = (SharedStringsTable)CreateRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.GetInstance());
                }

                // Load individual sheets. The order of sheets is defined by the order of CT_Sheet elements in the workbook
                sheets = new List<XSSFSheet>(shIdMap.Count);
                foreach (CT_Sheet ctSheet in this.workbook.sheets.sheet)
                {
                    XSSFSheet sh = shIdMap[ctSheet.id];
                    if (sh == null)
                    {
                        logger.Log(POILogger.WARN, "Sheet with name " + ctSheet.name + " and r:id " + ctSheet.id + " was defined, but didn't exist in package, skipping");
                        continue;
                    }
                    sh.sheet = ctSheet;
                    sh.OnDocumentRead();
                    sheets.Add(sh);
                }

                // Process the named ranges
                namedRanges = new List<XSSFName>();
                if (workbook.IsSetDefinedNames())
                {
                    foreach (CT_DefinedName ctName in workbook.definedNames.definedName)
                    {
                        namedRanges.Add(new XSSFName(ctName, this));
                    }
                }

            }
            catch (XmlException e)
            {
                throw new POIXMLException(e);
            }
        }