Beispiel #1
0
        public static WorkbookDocument Parse(XmlDocument xmlDoc, XmlNamespaceManager NameSpaceManager)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            CT_Workbook obj = CT_Workbook.Parse(xmlDoc.DocumentElement, NameSpaceManager);

            sw.Stop();
            Debug.WriteLine("CT_Workbook parse time: " + sw.ElapsedMilliseconds + "ms");
            return(new WorkbookDocument(obj));
        }
Beispiel #2
0
        public void SerializeEmptyWorksheetDocumentTest()
        {
            CT_Workbook worksheet = new CT_Workbook();

            StringWriter stream = new StringWriter();
            WorkbookDocument_Accessor.serializer.Serialize(stream, worksheet, WorkbookDocument_Accessor.namespaces);
            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<workbook xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
  <sheets />
</workbook>";
            Assert.AreEqual(expected, stream.ToString());
        }
Beispiel #3
0
 public static CT_Workbook Parse(XmlNode node, XmlNamespaceManager namespaceManager)
 {
     if (node == null)
         return null;
     CT_Workbook ctObj = new CT_Workbook();
     ctObj.fileRecoveryPr = new List<CT_FileRecoveryPr>();
     foreach (XmlNode childNode in node.ChildNodes)
     {
         if (childNode.LocalName == "fileVersion")
             ctObj.fileVersion = CT_FileVersion.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "fileSharing")
             ctObj.fileSharing = CT_FileSharing.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "workbookPr")
             ctObj.workbookPr = CT_WorkbookPr.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "workbookProtection")
             ctObj.workbookProtection = CT_WorkbookProtection.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "bookViews")
             ctObj.bookViews = CT_BookViews.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "sheets")
             ctObj.sheets = CT_Sheets.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "functionGroups")
             ctObj.functionGroups = CT_FunctionGroups.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "externalReferences")
             ctObj.externalReferences = CT_ExternalReferences.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "definedNames")
             ctObj.definedNames = CT_DefinedNames.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "calcPr")
             ctObj.calcPr = CT_CalcPr.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "oleSize")
             ctObj.oleSize = CT_OleSize.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "customWorkbookViews")
             ctObj.customWorkbookViews = CT_CustomWorkbookViews.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "pivotCaches")
             ctObj.pivotCaches = CT_PivotCaches.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "smartTagPr")
             ctObj.smartTagPr = CT_SmartTagPr.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "smartTagTypes")
             ctObj.smartTagTypes = CT_SmartTagTypes.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "webPublishing")
             ctObj.webPublishing = CT_WebPublishing.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "webPublishObjects")
             ctObj.webPublishObjects = CT_WebPublishObjects.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "extLst")
             ctObj.extLst = CT_ExtensionList.Parse(childNode, namespaceManager);
         else if (childNode.LocalName == "fileRecoveryPr")
             ctObj.fileRecoveryPr.Add(CT_FileRecoveryPr.Parse(childNode, namespaceManager));
     }
     return ctObj;
 }
Beispiel #4
0
 public WorkbookDocument(CT_Workbook workbook)
 {
     this.workbook = workbook;
 }
Beispiel #5
0
        public static WorkbookDocument Parse(XmlDocument xmlDoc, XmlNamespaceManager NameSpaceManager)
        {
            CT_Workbook obj = CT_Workbook.Parse(xmlDoc.DocumentElement, NameSpaceManager);

            return(new WorkbookDocument(obj));
        }
Beispiel #6
0
 public WorkbookDocument()
 {
     workbook = new CT_Workbook();
 }
Beispiel #7
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 #8
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 #9
0
        public void SerializeWorksheetDocumentTest()
        {
            CT_Workbook worksheet = new CT_Workbook();
            CT_Sheet sheet1 = new CT_Sheet();
            sheet1.name = "Sheet1";
            sheet1.sheetId = 1u;
            sheet1.id = "rId1";
            worksheet.sheets.sheet.Add(sheet1);

            var bks = worksheet.AddNewBookViews();
            var bk = bks.AddNewWorkbookView();
            bk.xWindow = 360;
            bk.xWindowSpecified = true;
            bk.yWindow = 60;
            bk.yWindowSpecified = true;
            bk.windowWidth = 11295;
            bk.windowWidthSpecified = true;
            bk.windowHeight = 5580;
            bk.windowHeightSpecified = true;



            StringWriter stream = new StringWriter();
            WorkbookDocument_Accessor.serializer.Serialize(stream, worksheet, WorkbookDocument_Accessor.namespaces);
            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<workbook xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
  <bookViews>
    <workbookView xWindow=""360"" yWindow=""60"" windowWidth=""11295"" windowHeight=""5580"" />
  </bookViews>
  <sheets>
    <sheet name=""Sheet1"" sheetId=""1"" r:id=""rId1"" />
  </sheets>
</workbook>";
            Assert.AreEqual(expected, stream.ToString());
        }
Beispiel #10
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);
            }
        }
Beispiel #11
0
        public static CT_Workbook Parse(XmlNode node, XmlNamespaceManager namespaceManager)
        {
            if (node == null)
            {
                return(null);
            }
            CT_Workbook ctObj = new CT_Workbook();

            ctObj.fileRecoveryPr = new List <CT_FileRecoveryPr>();
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (childNode.LocalName == "fileVersion")
                {
                    ctObj.fileVersion = CT_FileVersion.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "fileSharing")
                {
                    ctObj.fileSharing = CT_FileSharing.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "workbookPr")
                {
                    ctObj.workbookPr = CT_WorkbookPr.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "workbookProtection")
                {
                    ctObj.workbookProtection = CT_WorkbookProtection.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "bookViews")
                {
                    ctObj.bookViews = CT_BookViews.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "sheets")
                {
                    ctObj.sheets = CT_Sheets.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "functionGroups")
                {
                    ctObj.functionGroups = CT_FunctionGroups.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "externalReferences")
                {
                    ctObj.externalReferences = CT_ExternalReferences.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "definedNames")
                {
                    ctObj.definedNames = CT_DefinedNames.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "calcPr")
                {
                    ctObj.calcPr = CT_CalcPr.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "oleSize")
                {
                    ctObj.oleSize = CT_OleSize.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "customWorkbookViews")
                {
                    ctObj.customWorkbookViews = CT_CustomWorkbookViews.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "pivotCaches")
                {
                    ctObj.pivotCaches = CT_PivotCaches.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "smartTagPr")
                {
                    ctObj.smartTagPr = CT_SmartTagPr.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "smartTagTypes")
                {
                    ctObj.smartTagTypes = CT_SmartTagTypes.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "webPublishing")
                {
                    ctObj.webPublishing = CT_WebPublishing.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "webPublishObjects")
                {
                    ctObj.webPublishObjects = CT_WebPublishObjects.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "extLst")
                {
                    ctObj.extLst = CT_ExtensionList.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "fileRecoveryPr")
                {
                    ctObj.fileRecoveryPr.Add(CT_FileRecoveryPr.Parse(childNode, namespaceManager));
                }
            }
            return(ctObj);
        }
        public static WorkbookDocument Parse(Stream stream)
        {
            CT_Workbook obj = (CT_Workbook)serializer.Deserialize(stream);

            return(new WorkbookDocument(obj));
        }
Beispiel #13
0
 public WorkbookDocument(CT_Workbook workbook)
 {
     this.workbook = workbook;
 }
Beispiel #14
0
 public WorkbookDocument()
 {
     workbook = new CT_Workbook();
 }