Beispiel #1
0
        /**
         * If the XPath is, for example, /Node1/Node2/Node3 and /Node1/Node2 is the common XPath for the table, the local XPath is /Node3
         *
         * @return the local XPath
         */
        public String GetLocalXPath()
        {
            StringBuilder localXPath = new StringBuilder();
            int           numberOfCommonXPathAxis = table.GetCommonXpath().Split(new char[] { '/' }).Length - 1;

            String[] xPathTokens = ctXmlColumnPr.xpath.Split(new char[] { '/' });
            for (int i = numberOfCommonXPathAxis; i < xPathTokens.Length; i++)
            {
                localXPath.Append("/" + xPathTokens[i]);
            }
            return(localXPath.ToString());
        }
Beispiel #2
0
        /**
         * If the XPath is, for example, /Node1/Node2/Node3 and /Node1/Node2 is the common XPath for the table, the local XPath is /Node3
         *
         * @return the local XPath
         */
        public String GetLocalXPath()
        {
            String localXPath = "";
            int    numberOfCommonXPathAxis = table.GetCommonXpath().Split(new char[] { '/' }).Length - 1;

            String[] xPathTokens = ctXmlColumnPr.xpath.Split(new char[] { '/' });
            for (int i = numberOfCommonXPathAxis; i < xPathTokens.Length; i++)
            {
                localXPath += "/" + xPathTokens[i];
            }
            return(localXPath);
        }
        public void ExportToXML(Stream os, string encoding, bool validate)
        {
            List <XSSFSingleXmlCell> relatedSingleXmlCell = this.map.GetRelatedSingleXMLCell();
            List <XSSFTable>         relatedTables        = this.map.GetRelatedTables();
            string      rootElement   = this.map.GetCTMap().RootElement;
            XmlDocument emptyDocument = this.GetEmptyDocument();
            XmlElement  xmlElement    = !this.IsNamespaceDeclared() ? emptyDocument.CreateElement(rootElement) : emptyDocument.CreateElement(rootElement, this.GetNamespace());

            emptyDocument.AppendChild((XmlNode)xmlElement);
            List <string> stringList = new List <string>();
            Dictionary <string, XSSFSingleXmlCell> dictionary1 = new Dictionary <string, XSSFSingleXmlCell>();
            Dictionary <string, XSSFTable>         dictionary2 = new Dictionary <string, XSSFTable>();

            foreach (XSSFSingleXmlCell xssfSingleXmlCell in relatedSingleXmlCell)
            {
                stringList.Add(xssfSingleXmlCell.GetXpath());
                dictionary1[xssfSingleXmlCell.GetXpath()] = xssfSingleXmlCell;
            }
            foreach (XSSFTable xssfTable in relatedTables)
            {
                string commonXpath = xssfTable.GetCommonXpath();
                stringList.Add(commonXpath);
                dictionary2[commonXpath] = xssfTable;
            }
            stringList.Sort();
            foreach (string index in stringList)
            {
                XSSFSingleXmlCell xssfSingleXmlCell = !dictionary1.ContainsKey(index) ? (XSSFSingleXmlCell)null : dictionary1[index];
                XSSFTable         xssfTable         = !dictionary2.ContainsKey(index) ? (XSSFTable)null : dictionary2[index];
                if (!Regex.IsMatch(index, ".*\\[.*"))
                {
                    if (xssfSingleXmlCell != null)
                    {
                        XSSFCell referencedCell = (XSSFCell)xssfSingleXmlCell.GetReferencedCell();
                        if (referencedCell != null)
                        {
                            XmlNode        nodeByXpath = this.GetNodeByXPath(index, emptyDocument.FirstChild, emptyDocument, false);
                            ST_XmlDataType xmlDataType = xssfSingleXmlCell.GetXmlDataType();
                            this.mapCellOnNode(referencedCell, nodeByXpath, xmlDataType);
                        }
                    }
                    if (xssfTable != null)
                    {
                        List <XSSFXmlColumnPr> xmlColumnPrs = xssfTable.GetXmlColumnPrs();
                        XSSFSheet xssfSheet = xssfTable.GetXSSFSheet();
                        int       num       = xssfTable.GetStartCellReference().Row + 1;
                        int       row1      = xssfTable.GetEndCellReference().Row;
                        for (int rownum = num; rownum <= row1; ++rownum)
                        {
                            XSSFRow row2         = (XSSFRow)xssfSheet.GetRow(rownum);
                            XmlNode nodeByXpath1 = this.GetNodeByXPath(xssfTable.GetCommonXpath(), emptyDocument.FirstChild, emptyDocument, true);
                            short   col          = xssfTable.GetStartCellReference().Col;
                            for (int cellnum = (int)col; cellnum <= (int)xssfTable.GetEndCellReference().Col; ++cellnum)
                            {
                                XSSFCell cell = (XSSFCell)row2.GetCell(cellnum);
                                if (cell != null)
                                {
                                    XSSFXmlColumnPr xssfXmlColumnPr = xmlColumnPrs[cellnum - (int)col];
                                    XmlNode         nodeByXpath2    = this.GetNodeByXPath(xssfXmlColumnPr.GetLocalXPath(), nodeByXpath1, emptyDocument, false);
                                    ST_XmlDataType  xmlDataType     = xssfXmlColumnPr.GetXmlDataType();
                                    this.mapCellOnNode(cell, nodeByXpath2, xmlDataType);
                                }
                            }
                        }
                    }
                }
            }
            bool flag = true;

            if (validate)
            {
                flag = this.IsValid(emptyDocument);
            }
            if (!flag)
            {
                return;
            }
            using (XmlWriter w = XmlWriter.Create(os, new XmlWriterSettings()
            {
                Indent = true, Encoding = Encoding.GetEncoding(encoding)
            }))
                emptyDocument.WriteTo(w);
        }