Ejemplo n.º 1
0
 public XSSFXmlColumnPr GetXmlColumnPr()
 {
     if (xmlColumnPr == null)
     {
         CT_XmlColumnPr ctXmlColumnPr = ctTableColumn.xmlColumnPr;
         if (ctXmlColumnPr != null)
         {
             xmlColumnPr = new XSSFXmlColumnPr(this, ctXmlColumnPr);
         }
     }
     return(xmlColumnPr);
 }
Ejemplo n.º 2
0
 public List <XSSFXmlColumnPr> GetXmlColumnPrs()
 {
     if (xmlColumnPr == null)
     {
         xmlColumnPr = new List <XSSFXmlColumnPr>();
         foreach (CT_TableColumn column in ctTable.tableColumns.tableColumn)
         {
             if (column.xmlColumnPr != null)
             {
                 XSSFXmlColumnPr columnPr = new XSSFXmlColumnPr(this, column, column.xmlColumnPr);
                 xmlColumnPr.Add(columnPr);
             }
         }
     }
     return(xmlColumnPr);
 }
Ejemplo n.º 3
0
        /**
         * Exports the data in an XML stream
         *
         * @param os OutputStream in which will contain the output XML
         * @param encoding the output charset encoding
         * @param validate if true, validates the XML againts the XML Schema
         * @throws SAXException
         * @throws ParserConfigurationException
         * @throws TransformerException
         * @throws InvalidFormatException
         */
        public void ExportToXML(Stream os, String encoding, bool validate)
        {
            List <XSSFSingleXmlCell> SingleXMLCells = map.GetRelatedSingleXMLCell();
            List <XSSFTable>         tables         = map.GetRelatedTables();

            String rootElement = map.GetCTMap().RootElement;

            XDocument doc = GetEmptyDocument();


            XElement root = null;

            if (IsNamespaceDeclared())
            {
                root = new XElement((XNamespace)this.GetNamespace() + rootElement);
            }
            else
            {
                root = doc.CreateElement(rootElement);
            }
            doc.AppendChild(root);


            List <String> xpaths = new List <String>();
            Dictionary <String, XSSFSingleXmlCell> SingleXmlCellsMappings = new Dictionary <String, XSSFSingleXmlCell>();
            Dictionary <String, XSSFTable>         tableMappings          = new Dictionary <String, XSSFTable>();

            foreach (XSSFSingleXmlCell simpleXmlCell in SingleXMLCells)
            {
                xpaths.Add(simpleXmlCell.GetXpath());
                SingleXmlCellsMappings[simpleXmlCell.GetXpath()] = simpleXmlCell;
            }
            foreach (XSSFTable table in tables)
            {
                String commonXPath = table.GetCommonXpath();
                xpaths.Add(commonXPath);
                tableMappings[commonXPath] = table;
            }


            xpaths.Sort();

            foreach (String xpath in xpaths)
            {
                XSSFSingleXmlCell simpleXmlCell;
                if (SingleXmlCellsMappings.ContainsKey(xpath))
                {
                    simpleXmlCell = SingleXmlCellsMappings[xpath];
                }
                else
                {
                    simpleXmlCell = null;
                }
                XSSFTable table;
                if (tableMappings.ContainsKey(xpath))
                {
                    table = tableMappings[xpath];
                }
                else
                {
                    table = null;
                }

                if (!Regex.IsMatch(xpath, ".*\\[.*"))
                {
                    // Exports elements and attributes mapped with simpleXmlCell
                    if (simpleXmlCell != null)
                    {
                        XSSFCell cell = (XSSFCell)simpleXmlCell.GetReferencedCell();
                        if (cell != null)
                        {
                            XElement       currentNode = GetNodeByXPath(xpath, doc.Root.FirstNode as XElement, doc, false);
                            ST_XmlDataType dataType    = simpleXmlCell.GetXmlDataType();
                            mapCellOnNode(cell, currentNode, dataType);
                        }
                    }

                    // Exports elements and attributes mapped with tables
                    if (table != null)
                    {
                        List <XSSFXmlColumnPr> tableColumns = table.GetXmlColumnPrs();

                        XSSFSheet sheet = table.GetXSSFSheet();

                        int startRow = table.GetStartCellReference().Row;
                        // In mappings Created with Microsoft Excel the first row Contains the table header and must be Skipped
                        startRow += 1;

                        int endRow = table.GetEndCellReference().Row;

                        for (int i = startRow; i <= endRow; i++)
                        {
                            XSSFRow row = (XSSFRow)sheet.GetRow(i);

                            XElement tableRootNode = GetNodeByXPath(table.GetCommonXpath(), doc.Root.FirstNode as XElement, doc, true);

                            short startColumnIndex = table.GetStartCellReference().Col;
                            for (int j = startColumnIndex; j <= table.GetEndCellReference().Col; j++)
                            {
                                XSSFCell cell = (XSSFCell)row.GetCell(j);
                                if (cell != null)
                                {
                                    XSSFXmlColumnPr pointer     = tableColumns[j - startColumnIndex];
                                    String          localXPath  = pointer.GetLocalXPath();
                                    XElement        currentNode = GetNodeByXPath(localXPath, tableRootNode, doc, false);
                                    ST_XmlDataType  dataType    = pointer.GetXmlDataType();


                                    mapCellOnNode(cell, currentNode, dataType);
                                }
                            }
                        }
                    }
                }
                else
                {
                    // TODO:  implement filtering management in xpath
                }
            }

            bool isValid = true;

            if (validate)
            {
                isValid = this.IsValid(doc);
            }



            if (isValid)
            {
                /////////////////
                //Output the XML
                XmlWriterSettings settings = new XmlWriterSettings();
                //settings.OmitXmlDeclaration=false;
                settings.Indent   = true;
                settings.Encoding = Encoding.GetEncoding(encoding);
                //create string from xml tree
                using (XmlWriter xmlWriter = XmlWriter.Create(os, settings))
                {
                    doc.WriteTo(xmlWriter);
                }
            }
        }
Ejemplo n.º 4
0
        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);
        }