Beispiel #1
0
        private void mapCellOnNode(XSSFCell cell, XElement node, ST_XmlDataType outputDataType)
        {
            String value = "";

            switch (cell.CellType)
            {
            case CellType.String: value = cell.StringCellValue; break;

            case CellType.Boolean: value += cell.BooleanCellValue; break;

            case CellType.Error: value = cell.ErrorCellString; break;

            case CellType.Formula: value = cell.StringCellValue; break;

            case CellType.Numeric: value += cell.GetRawValue(); break;

            default:
                break;
            }
            if (node is XElement)
            {
                XElement currentElement = (XElement)node;
                currentElement.Value = value;
            }
            else
            {
                node.Value = value;
            }
        }
        private void mapCellOnNode(XSSFCell cell, XmlNode node, ST_XmlDataType outputDataType)
        {
            String value = "";

            switch (cell.CellType)
            {
            case CellType.STRING: value = cell.StringCellValue; break;

            case CellType.BOOLEAN: value += cell.BooleanCellValue; break;

            case CellType.ERROR: value = cell.ErrorCellString; break;

            case CellType.FORMULA: value = cell.StringCellValue; break;

            case CellType.NUMERIC: value += cell.GetRawValue(); break;

            default:
                break;
            }
            if (node is XmlElement)
            {
                XmlElement currentElement = (XmlElement)node;
                currentElement.InnerText = value;
            }
            else
            {
                node.Value = value;
            }
        }
        private void mapCellOnNode(XSSFCell cell, XmlNode node, ST_XmlDataType outputDataType)
        {
            string str = "";

            switch (cell.CellType)
            {
            case CellType.NUMERIC:
                str += cell.GetRawValue();
                break;

            case CellType.STRING:
                str = cell.StringCellValue;
                break;

            case CellType.FORMULA:
                str = cell.StringCellValue;
                break;

            case CellType.BOOLEAN:
                str += (string)(object)cell.BooleanCellValue;
                break;

            case CellType.ERROR:
                str = cell.ErrorCellString;
                break;
            }
            if (node is XmlElement)
            {
                node.InnerText = str;
            }
            else
            {
                node.Value = str;
            }
        }
Beispiel #4
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);
                }
            }
        }
        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);
        }