Exemplo n.º 1
0
        /**
         * Create a XSSFHyperlink amd Initialize it from the supplied CTHyperlink bean and namespace relationship
         *
         * @param ctHyperlink the xml bean Containing xml properties
         * @param hyperlinkRel the relationship in the underlying OPC namespace which stores the actual link's Address
         */
        public XSSFHyperlink(CT_Hyperlink ctHyperlink, PackageRelationship hyperlinkRel)
        {
            _ctHyperlink = ctHyperlink;
            _externalRel = hyperlinkRel;

            // Figure out the Hyperlink type and distination

            // If it has a location, it's internal
            if (ctHyperlink.location != null)
            {
                _type = HyperlinkType.Document;
                _location = ctHyperlink.location;
            }
            else
            {
                // Otherwise it's somehow external, check
                //  the relation to see how
                if (_externalRel == null)
                {
                    if (ctHyperlink.id != null)
                    {
                        throw new InvalidOperationException("The hyperlink for cell " +
                            ctHyperlink.@ref + " references relation " + ctHyperlink.id + ", but that didn't exist!");
                    }
                    // hyperlink is internal and is not related to other parts
                    _type = HyperlinkType.Document;
                }
                else
                {
                    Uri target = _externalRel.TargetUri;
                    try
                    {
                        _location = target.ToString();
                    }
                    catch (UriFormatException)
                    {
                        _location = target.OriginalString;
                    }

                    // Try to figure out the type
                    if (_location.StartsWith("http://") || _location.StartsWith("https://")
                            || _location.StartsWith("ftp://"))
                    {
                        _type = HyperlinkType.Url;
                    }
                    else if (_location.StartsWith("mailto:"))
                    {
                        _type = HyperlinkType.Email;
                    }
                    else
                    {
                        _type = HyperlinkType.File;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public CT_Hyperlink Copy()
        {
            CT_Hyperlink ctObj = new CT_Hyperlink();

            ctObj.@ref     = @ref;
            ctObj.id       = id;
            ctObj.location = location;
            ctObj.tooltip  = tooltip;
            ctObj.display  = display;
            return(ctObj);
        }
Exemplo n.º 3
0
        public static CT_Hyperlink Parse(XmlNode node, XmlNamespaceManager namespaceManager)
        {
            if (node == null)
            {
                return(null);
            }
            CT_Hyperlink ctObj = new CT_Hyperlink();

            ctObj.@ref     = XmlHelper.ReadString(node.Attributes["ref"]);
            ctObj.id       = XmlHelper.ReadString(node.Attributes["id", PackageNamespaces.SCHEMA_RELATIONSHIPS]);
            ctObj.location = XmlHelper.ReadString(node.Attributes["location"]);
            ctObj.tooltip  = XmlHelper.ReadString(node.Attributes["tooltip"]);
            ctObj.display  = XmlHelper.ReadString(node.Attributes["display"]);
            return(ctObj);
        }
Exemplo n.º 4
0
        /**
         * Create a XSSFHyperlink amd Initialize it from the supplied CTHyperlink bean and namespace relationship
         *
         * @param ctHyperlink the xml bean Containing xml properties
         * @param hyperlinkRel the relationship in the underlying OPC namespace which stores the actual link's Address
         */
        public XSSFHyperlink(CT_Hyperlink ctHyperlink, PackageRelationship hyperlinkRel)
        {
            _ctHyperlink = ctHyperlink;
            _externalRel = hyperlinkRel;

            // Figure out the Hyperlink type and distination

            // If it has a location, it's internal
            if (ctHyperlink.location != null)
            {
                _type = HyperlinkType.DOCUMENT;
                _location = ctHyperlink.location;
            }
            else
            {
                // Otherwise it's somehow external, check
                //  the relation to see how
                if (_externalRel == null)
                {
                    if (ctHyperlink.id != null)
                    {
                        throw new InvalidOperationException("The hyperlink for cell " + 
                            ctHyperlink.@ref + " references relation " + ctHyperlink.id + ", but that didn't exist!");
                    }
                    throw new InvalidOperationException("A sheet hyperlink must either have a location, or a relationship. Found:\n" + ctHyperlink);
                }

                Uri target = _externalRel.TargetUri;
                _location = target.ToString();

                // Try to figure out the type
                if (_location.StartsWith("http://") || _location.StartsWith("https://")
                        || _location.StartsWith("ftp://"))
                {
                    _type = HyperlinkType.URL;
                }
                else if (_location.StartsWith("mailto:"))
                {
                    _type = HyperlinkType.EMAIL;
                }
                else
                {
                    _type = HyperlinkType.FILE;
                }
            }
        }
Exemplo n.º 5
0
        public static CT_Hyperlink Parse(XmlNode node, XmlNamespaceManager namespaceManager)
    {
	if(node==null)
		return null;
	CT_Hyperlink ctObj = new CT_Hyperlink();
    ctObj.@ref = XmlHelper.ReadString(node.Attributes["ref"]);
	ctObj.id = XmlHelper.ReadString(node.Attributes["id"]);
	ctObj.location = XmlHelper.ReadString(node.Attributes["location"]);
	ctObj.tooltip = XmlHelper.ReadString(node.Attributes["tooltip"]);
	ctObj.display = XmlHelper.ReadString(node.Attributes["display"]);
	return ctObj;
    }
Exemplo n.º 6
0
        public void SerializeWorksheetDocumentTest()
        {
            CT_Worksheet worksheet = new CT_Worksheet();
            worksheet.dimension = new CT_SheetDimension();
            worksheet.dimension.@ref = "A1:C1";

            var sheetData = worksheet.AddNewSheetData();
            var row = sheetData.AddNewRow();
            row.r = 1u;
            row.spans = "1:3";
            {
                var c = row.AddNewC();
                c.r = "A1";
                c.t = ST_CellType.s;
                c.v = "0";
            }
            {
                var c = row.AddNewC();
                c.r = "B1";
                c.t = ST_CellType.s;
                c.v = "1";
            }
            {
                var c = row.AddNewC();
                c.r = "C1";
                c.t = ST_CellType.s;
                c.v = "8";
            }
            var hyper = worksheet.AddNewHyperlinks();
            var link = new CT_Hyperlink();
            link.@ref="B1";
            link.id="rId1";
            hyper.hyperlink.Add(link);
            StringWriter stream = new StringWriter();
            WorksheetDocument_Accessor.serializer.Serialize(stream, worksheet, WorksheetDocument_Accessor.namespaces);
            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
            <worksheet xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
              <dimension ref=""A1:C1"" />
              <sheetData>
            <row r=""1"" spans=""1:3"">
              <c r=""A1"" t=""s"">
            <v>0</v>
              </c>
              <c r=""B1"" t=""s"">
            <v>1</v>
              </c>
              <c r=""C1"" t=""s"">
            <v>8</v>
              </c>
            </row>
              </sheetData>
              <hyperlinks>
            <hyperlink ref=""B1"" r:id=""rId1"" />
              </hyperlinks>
            </worksheet>";
            Assert.AreEqual(expected, stream.ToString());
        }
Exemplo n.º 7
0
        internal virtual void Write(Stream out1)
        {

            if (worksheet.sizeOfColsArray() == 1)
            {
                CT_Cols col = worksheet.GetColsArray(0);
                if (col.sizeOfColArray() == 0)
                {
                    worksheet.SetColsArray(null);
                }
            }

            // Now re-generate our CT_Hyperlinks, if needed
            if (hyperlinks.Count > 0)
            {
                if (worksheet.hyperlinks == null)
                {
                    worksheet.AddNewHyperlinks();
                }
                NPOI.OpenXmlFormats.Spreadsheet.CT_Hyperlink[] ctHls
                    = new NPOI.OpenXmlFormats.Spreadsheet.CT_Hyperlink[hyperlinks.Count];
                for (int i = 0; i < ctHls.Length; i++)
                {
                    // If our sheet has hyperlinks, have them add
                    //  any relationships that they might need
                    XSSFHyperlink hyperlink = hyperlinks[i];
                    hyperlink.GenerateRelationIfNeeded(GetPackagePart());
                    // Now grab their underling object
                    ctHls[i] = hyperlink.GetCTHyperlink();
                }
                worksheet.hyperlinks.SetHyperlinkArray(ctHls);
            }

            foreach (XSSFRow row in _rows.Values)
            {
                row.OnDocumentWrite();
            }

            //XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
            //xmlOptions.SetSaveSyntheticDocumentElement(new QName(CT_Worksheet.type.GetName().getNamespaceURI(), "worksheet"));
            Dictionary<String, String> map = new Dictionary<String, String>();
            map[ST_RelationshipId.NamespaceURI] = "r";
            //xmlOptions.SetSaveSuggestedPrefixes(map);

            worksheet.Save(out1);
        }
Exemplo n.º 8
0
 /**
  * Create a new XSSFHyperlink. This method is protected to be used only by XSSFCreationHelper
  *
  * @param type - the type of hyperlink to create
  */
 public XSSFHyperlink(HyperlinkType type)
 {
     _type = type;
     _ctHyperlink = new CT_Hyperlink();
 }
Exemplo n.º 9
0
        internal virtual void Write(Stream stream)
        {
            bool setToNull = false;
            if (worksheet.sizeOfColsArray() == 1)
            {
                CT_Cols col = worksheet.GetColsArray(0);
                if (col.sizeOfColArray() == 0)
                {
                    setToNull = true;
                    // this is necessary so that we do not write an empty <cols/> item into the sheet-xml in the xlsx-file
                    // Excel complains about a corrupted file if this shows up there!
                    worksheet.SetColsArray(null);
                }
                else
                {
                    SetColWidthAttribute(col);
                }
            }
            

            // Now re-generate our CT_Hyperlinks, if needed
            if (hyperlinks.Count > 0)
            {
                if (worksheet.hyperlinks == null)
                {
                    worksheet.AddNewHyperlinks();
                }
                NPOI.OpenXmlFormats.Spreadsheet.CT_Hyperlink[] ctHls
                    = new NPOI.OpenXmlFormats.Spreadsheet.CT_Hyperlink[hyperlinks.Count];
                for (int i = 0; i < ctHls.Length; i++)
                {
                    // If our sheet has hyperlinks, have them add
                    //  any relationships that they might need
                    XSSFHyperlink hyperlink = hyperlinks[i];
                    hyperlink.GenerateRelationIfNeeded(GetPackagePart());
                    // Now grab their underling object
                    ctHls[i] = hyperlink.GetCTHyperlink();
                }
                worksheet.hyperlinks.SetHyperlinkArray(ctHls);
            }

            foreach (XSSFRow row in _rows.Values)
            {
                row.OnDocumentWrite();
            }

            //XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
            //xmlOptions.SetSaveSyntheticDocumentElement(new QName(CT_Worksheet.type.GetName().getNamespaceURI(), "worksheet"));
            Dictionary<String, String> map = new Dictionary<String, String>();
            map[ST_RelationshipId.NamespaceURI] = "r";
            //xmlOptions.SetSaveSuggestedPrefixes(map);

            new WorksheetDocument(worksheet).Save(stream);

            // Bug 52233: Ensure that we have a col-array even if write() removed it
            if (setToNull)
            {
                worksheet.AddNewCols();
            }
        }