Пример #1
0
        public void RecalcId()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            Assert.IsFalse(wb.GetForceFormulaRecalculation());
            CT_Workbook ctWorkbook = wb.GetCTWorkbook();

            Assert.IsFalse(ctWorkbook.IsSetCalcPr());

            wb.SetForceFormulaRecalculation(true); // resets the EngineId flag to zero

            CT_CalcPr calcPr = ctWorkbook.calcPr;

            Assert.IsNotNull(calcPr);
            Assert.AreEqual(0, (int)calcPr.calcId);

            calcPr.calcId = 100;
            Assert.IsTrue(wb.GetForceFormulaRecalculation());

            wb.SetForceFormulaRecalculation(true); // resets the EngineId flag to zero
            Assert.AreEqual(0, (int)calcPr.calcId);
            Assert.IsFalse(wb.GetForceFormulaRecalculation());

            // calcMode="manual" is unset when forceFormulaRecalculation=true
            calcPr.calcMode = (ST_CalcMode.manual);
            wb.SetForceFormulaRecalculation(true);
            Assert.AreEqual(ST_CalcMode.auto, calcPr.calcMode);
        }
Пример #2
0
        public void SetForceFormulaRecalculation(bool value)
        {
            CT_Workbook ctWorkbook = this.GetCTWorkbook();
            CT_CalcPr   ctCalcPr   = ctWorkbook.IsSetCalcPr() ? ctWorkbook.calcPr : ctWorkbook.AddNewCalcPr();

            ctCalcPr.calcId = 0U;
            if (!value || ctCalcPr.calcMode != ST_CalcMode.manual)
            {
                return;
            }
            ctCalcPr.calcMode = ST_CalcMode.auto;
        }
Пример #3
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());
        }
Пример #4
0
        public void AddPivotCache()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            try
            {
                CT_Workbook   ctWb       = wb.GetCTWorkbook();
                CT_PivotCache pivotCache = wb.AddPivotCache("0");
                //Ensures that pivotCaches is Initiated
                Assert.IsTrue(ctWb.IsSetPivotCaches());
                Assert.AreSame(pivotCache, ctWb.pivotCaches.GetPivotCacheArray(0));
                Assert.AreEqual("0", pivotCache.id);
            }
            finally
            {
                wb.Close();
            }
        }
Пример #5
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());
        }
Пример #6
0
        public void TestRecalcId()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            Assert.IsFalse(wb.GetForceFormulaRecalculation());
            CT_Workbook ctWorkbook = wb.GetCTWorkbook();

            Assert.IsFalse(ctWorkbook.IsSetCalcPr());

            wb.SetForceFormulaRecalculation(true); // resets the EngineId flag to zero

            CT_CalcPr calcPr = ctWorkbook.calcPr;

            Assert.IsNotNull(calcPr);
            Assert.AreEqual(0, (int)calcPr.calcId);

            calcPr.calcId = 100;
            Assert.IsTrue(wb.GetForceFormulaRecalculation());

            wb.SetForceFormulaRecalculation(true); // resets the EngineId flag to zero
            Assert.AreEqual(0, (int)calcPr.calcId);
            Assert.IsFalse(wb.GetForceFormulaRecalculation());
        }
Пример #7
0
        private void OnWorkbookCreate()
        {
            this.workbook = new CT_Workbook();
            this.workbook.AddNewWorkbookPr().date1904 = false;
            this.workbook.AddNewBookViews().AddNewWorkbookView().activeTab = 0U;
            this.workbook.AddNewSheets();
            CT_ExtendedProperties underlyingProperties = this.GetProperties().GetExtendedProperties().GetUnderlyingProperties();

            underlyingProperties.Application                = POIXMLDocument.DOCUMENT_CREATOR;
            underlyingProperties.DocSecurity                = 0;
            underlyingProperties.DocSecuritySpecified       = true;
            underlyingProperties.ScaleCrop                  = false;
            underlyingProperties.ScaleCropSpecified         = true;
            underlyingProperties.LinksUpToDate              = false;
            underlyingProperties.LinksUpToDateSpecified     = true;
            underlyingProperties.HyperlinksChanged          = false;
            underlyingProperties.HyperlinksChangedSpecified = true;
            underlyingProperties.SharedDoc                  = false;
            underlyingProperties.SharedDocSpecified         = true;
            this.sharedStringSource = (SharedStringsTable)this.CreateRelationship((POIXMLRelation)XSSFRelation.SHARED_STRINGS, (POIXMLFactory)XSSFFactory.GetInstance());
            this.stylesSource       = (StylesTable)this.CreateRelationship((POIXMLRelation)XSSFRelation.STYLES, (POIXMLFactory)XSSFFactory.GetInstance());
            this.namedRanges        = new List <XSSFName>();
            this.sheets             = new List <XSSFSheet>();
        }
Пример #8
0
        void WriteWorkbook()
        {
            rels.Add(new CT_Relationship {
                Type = Relationships.Workbook, Target = "xl/workbook.xml"
            });
            OverrideContentType("/xl/workbook.xml", ContentTypes.Workbook);

            sharedStrings = new SharedStrings();
            ClearStyles();

            workbookRelationships = new Relationships();

            int sheetIndex = 1;

            contentTypes.Add(new CT_Default {
                Extension = "xml", ContentType = ContentTypes.Xml
            });
            contentTypes.Add(new CT_Default {
                Extension = "rels", ContentType = ContentTypes.Rels
            });

            var sheets = new List <CT_Sheet>();

            foreach (var sheet in workbook.Sheets)
            {
                var relativePath = String.Format("worksheets/sheet{0}.xml", sheetIndex);
                var sheetPath    = "xl/" + relativePath;
                var rid          = workbookRelationships.Add(new CT_Relationship
                {
                    Target     = relativePath,
                    TargetMode = ST_TargetMode.Internal,
                    Type       = Relationships.Sheet
                });
                OverrideContentType(sheetPath, ContentTypes.Sheet);
                var st = new CT_Sheet
                {
                    id      = rid,
                    name    = sheet.SheetName,
                    sheetId = (uint)sheetIndex
                };
                sheets.Add(st);
                WriteSheet(sheetPath, sheet);
                sheetIndex++;
            }

            var wb = new CT_Workbook()
            {
                sheets    = sheets.ToArray(),
                bookViews = new[] { new CT_BookView() }
            };

            var definedNames = new List <CT_DefinedName>();

            foreach (var definedName in workbook.DefinedNames)
            {
                var dn = new CT_DefinedName
                {
                    name  = definedName.Name,
                    Value = definedName.Value
                };
                definedNames.Add(dn);
            }

            for (var i = 0; i < workbook.Sheets.Count; i++)
            {
                foreach (var definedName in workbook.DefinedNames)
                {
                    var dn = new CT_DefinedName
                    {
                        name                  = definedName.Name,
                        Value                 = definedName.Value,
                        localSheetId          = (uint)i,
                        localSheetIdSpecified = true
                    };
                    definedNames.Add(dn);
                }
            }

            if (definedNames.Count > 0)
            {
                wb.definedNames = definedNames.ToArray();
            }


            WriteFile("xl/workbook.xml", wb, SpreadsheetNs(true));

            if (sharedStrings.Count > 0)
            {
                WriteSharedStrings();
            }

            if (!styles.Empty)
            {
                OverrideContentType("xl/styles.xml", ContentTypes.Styles);
                workbookRelationships.Add(new CT_Relationship {
                    Target = "styles.xml", Type = Relationships.Styles
                });
                WriteStyles("xl/styles.xml");
            }

            WriteRelationsips("xl/_rels/workbook.xml.rels", workbookRelationships);
        }
 public XMLDefinedNamesManager(CT_Workbook workbook)
 {
     _workbook = workbook;
 }
Пример #10
0
 internal override void OnDocumentRead()
 {
     try
     {
         this.workbook = WorkbookDocument.Parse(this.GetPackagePart().GetInputStream()).Workbook;
         Dictionary <string, XSSFSheet> dictionary = new Dictionary <string, XSSFSheet>();
         foreach (POIXMLDocumentPart relation in this.GetRelations())
         {
             if (relation is SharedStringsTable)
             {
                 this.sharedStringSource = (SharedStringsTable)relation;
             }
             else if (relation is StylesTable)
             {
                 this.stylesSource = (StylesTable)relation;
             }
             else if (relation is ThemesTable)
             {
                 this.theme = (ThemesTable)relation;
             }
             else if (relation is CalculationChain)
             {
                 this.calcChain = (CalculationChain)relation;
             }
             else if (relation is MapInfo)
             {
                 this.mapInfo = (MapInfo)relation;
             }
             else if (relation is XSSFSheet)
             {
                 dictionary.Add(relation.GetPackageRelationship().Id, (XSSFSheet)relation);
             }
         }
         if (this.stylesSource != null)
         {
             this.stylesSource.SetTheme(this.theme);
         }
         if (this.sharedStringSource == null)
         {
             this.sharedStringSource = (SharedStringsTable)this.CreateRelationship((POIXMLRelation)XSSFRelation.SHARED_STRINGS, (POIXMLFactory)XSSFFactory.GetInstance());
         }
         this.sheets = new List <XSSFSheet>(dictionary.Count);
         foreach (CT_Sheet ctSheet in this.workbook.sheets.sheet)
         {
             XSSFSheet xssfSheet = dictionary[ctSheet.id];
             if (xssfSheet == null)
             {
                 XSSFWorkbook.logger.Log(5, (object)("Sheet with name " + ctSheet.name + " and r:id " + ctSheet.id + " was defined, but didn't exist in package, skipping"));
             }
             else
             {
                 xssfSheet.sheet = ctSheet;
                 xssfSheet.OnDocumentRead();
                 this.sheets.Add(xssfSheet);
             }
         }
         this.namedRanges = new List <XSSFName>();
         if (!this.workbook.IsSetDefinedNames())
         {
             return;
         }
         foreach (CT_DefinedName name in this.workbook.definedNames.definedName)
         {
             this.namedRanges.Add(new XSSFName(name, this));
         }
     }
     catch (XmlException ex)
     {
         throw new POIXMLException((Exception)ex);
     }
 }
Пример #11
0
        public PartManager(XMLWorkbookModel workbookModel)
        {
            _workbookmodel    = workbookModel;
            _relationshipTree = new OPCRelationshipTree("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", ((IStreambookModel)_workbookmodel).ZipPackage);
            WorkbookPart workbookPart = new WorkbookPart();
            CT_Workbook  obj          = (CT_Workbook)workbookPart.Root;

            obj.FileVersion = new CT_FileVersion();
            obj.FileVersion.AppName_Attr      = "xl";
            obj.FileVersion.LastEdited_Attr   = "4";
            obj.FileVersion.LowestEdited_Attr = "4";
            obj.FileVersion.RupBuild_Attr     = "4506";
            obj.WorkbookPr = new CT_WorkbookPr();
            obj.WorkbookPr.DefaultThemeVersion_Attr = 124226u;
            obj.BookViews = new CT_BookViews();
            CT_BookView item = new CT_BookView
            {
                XWindow_Attr      = 240,
                YWindow_Attr      = 120,
                WindowWidth_Attr  = 18060u,
                WindowHeight_Attr = 7050u
            };

            obj.BookViews.WorkbookView.Add(item);
            obj.CalcPr             = new CT_CalcPr();
            obj.CalcPr.CalcId_Attr = 125725u;
            Relationship relationship = _relationshipTree.AddRootPartToTree(workbookPart, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "xl/workbook.xml");

            _workbook = workbookPart;
            StyleSheetPart styleSheetPart = new StyleSheetPart();
            CT_Stylesheet  obj2           = (CT_Stylesheet)styleSheetPart.Root;
            CT_Font        cT_Font        = new CT_Font();

            cT_Font.Sz              = new CT_FontSize();
            cT_Font.Sz.Val_Attr     = 11.0;
            cT_Font.Color           = new CT_Color();
            cT_Font.Color.Rgb_Attr  = "FF000000";
            cT_Font.Name            = new CT_FontName();
            cT_Font.Name.Val_Attr   = "Calibri";
            cT_Font.Family          = new CT_IntProperty();
            cT_Font.Family.Val_Attr = 2;
            cT_Font.Scheme          = new CT_FontScheme();
            cT_Font.Scheme.Val_Attr = ST_FontScheme.minor;
            obj2.Fonts              = new CT_Fonts();
            obj2.Fonts.Font.Add(cT_Font);
            obj2.Fonts.Count_Attr = 1u;
            CT_Fill cT_Fill = new CT_Fill();

            cT_Fill.PatternFill = new CT_PatternFill();
            cT_Fill.PatternFill.PatternType_Attr = ST_PatternType.none;
            CT_Fill cT_Fill2 = new CT_Fill();

            cT_Fill2.PatternFill = new CT_PatternFill();
            cT_Fill2.PatternFill.PatternType_Attr = ST_PatternType.gray125;
            obj2.Fills = new CT_Fills();
            obj2.Fills.Fill.Add(cT_Fill);
            obj2.Fills.Fill.Add(cT_Fill2);
            obj2.Fills.Count_Attr = 2u;
            CT_Border item2 = new CT_Border
            {
                Left     = new CT_BorderPr(),
                Right    = new CT_BorderPr(),
                Top      = new CT_BorderPr(),
                Bottom   = new CT_BorderPr(),
                Diagonal = new CT_BorderPr()
            };

            obj2.Borders = new CT_Borders();
            obj2.Borders.Border.Add(item2);
            obj2.Borders.Count_Attr = 1u;
            CT_Xf item3 = new CT_Xf
            {
                NumFmtId_Attr = 0u,
                FontId_Attr   = 0u,
                FillId_Attr   = 0u,
                BorderId_Attr = 0u
            };

            obj2.CellStyleXfs = new CT_CellStyleXfs();
            obj2.CellStyleXfs.Xf.Add(item3);
            obj2.CellXfs = new CT_CellXfs();
            obj2.CellXfs.Xf.Add(StyleManager.CreateDefaultXf());
            CT_CellStyle item4 = new CT_CellStyle
            {
                Name_Attr      = "Normal",
                XfId_Attr      = 0u,
                BuiltinId_Attr = 0u
            };

            obj2.CellStyles = new CT_CellStyles();
            obj2.CellStyles.CellStyle.Add(item4);
            obj2.Dxfs                               = new CT_Dxfs();
            obj2.Dxfs.Count_Attr                    = 0u;
            obj2.TableStyles                        = new CT_TableStyles();
            obj2.TableStyles.Count_Attr             = 0u;
            obj2.TableStyles.DefaultTableStyle_Attr = "TableStyleMedium9";
            obj2.TableStyles.DefaultPivotStyle_Attr = "PivotStyleLight16";
            _relationshipTree.AddPartToTree(styleSheetPart, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "xl/styles.xml", (XmlPart)_relationshipTree.GetPartByLocation(relationship.RelatedPart));
            _stylesheet = new StyleManager(styleSheetPart);
            OpcCorePropertiesPart part = new OpcCorePropertiesPart();

            _relationshipTree.AddRootPartToTree(part, "application/vnd.openxmlformats-package.core-properties+xml", "http://schemas.openxmlformats.org/package/2006/relationships/meatadata/core-properties", "docProps/core.xml");
            PropertiesPart propertiesPart = new PropertiesPart();
            CT_Properties  obj3           = (CT_Properties)propertiesPart.Root;

            obj3.Application                       = "Microsoft Excel";
            obj3.DocSecurity                       = 0;
            obj3.ScaleCrop                         = false;
            obj3.HeadingPairs                      = new CT_VectorVariant();
            obj3.HeadingPairs.Vector               = new CT_Vector();
            obj3.HeadingPairs.Vector.Size_Attr     = 2u;
            obj3.HeadingPairs.Vector.BaseType_Attr = ST_VectorBaseType.variant;
            CT_Variant item5 = new CT_Variant
            {
                Choice_0 = CT_Variant.ChoiceBucket_0.lpstr,
                Lpstr    = "Worksheets"
            };
            CT_Variant item6 = new CT_Variant
            {
                Choice_0 = CT_Variant.ChoiceBucket_0.i4,
                I4       = 1
            };

            obj3.HeadingPairs.Vector.Variant.Add(item5);
            obj3.HeadingPairs.Vector.Variant.Add(item6);
            obj3.TitlesOfParts                      = new CT_VectorLpstr();
            obj3.TitlesOfParts.Vector               = new CT_Vector();
            obj3.TitlesOfParts.Vector.Size_Attr     = 0u;
            obj3.TitlesOfParts.Vector.BaseType_Attr = ST_VectorBaseType.lpstr;
            obj3.LinksUpToDate                      = false;
            obj3.SharedDoc         = false;
            obj3.HyperlinksChanged = false;
            obj3.AppVersion        = "12.0000";
            _relationshipTree.AddRootPartToTree(propertiesPart, "application/vnd.openxmlformats-officedocument.extended-properties+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties", "docProps/app.xml");
        }