Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static void WriteDMToTable(this ISheet sheet, List <DefineSelectItem> lst, string tableName, int numCol)
        {
            if (lst == null || lst.Count == 0)
            {
                return;
            }
            XSSFSheet s   = (XSSFSheet)sheet;
            XSSFTable tbl = s.GetTables().Where(a => a.Name == tableName).FirstOrDefault();

            if (tbl == null)
            {
                return;
            }
            CT_Table ctTBl = tbl.GetCTTable();

            if (numCol < 2 || ctTBl.tableColumns.count < numCol)
            {
                return;
            }
            CellReference cellRefStart = tbl.GetStartCellReference();
            int           rowStart     = cellRefStart.Row + 1;
            short         colStart     = cellRefStart.Col;
            CellReference cellRefEnd   = tbl.GetEndCellReference();
            AreaReference reference    = new AreaReference(cellRefStart, new CellReference(cellRefStart.Row + lst.Count, cellRefEnd.Col));

            ctTBl.insertRow      = true;
            ctTBl.insertRowShift = true;
            ctTBl.@ref           = reference.FormatAsString();
            IRow row = GetCreateRow(sheet, rowStart);

            switch (numCol)
            {
            case 3:
                foreach (DefineSelectItem item in lst)
                {
                    row = GetCreateRow(sheet, rowStart++);

                    row.GetCreateCell(colStart).SetCellValue(item.Value);

                    row.GetCreateCell(colStart + 1).SetCellValue(item.Value2);

                    row.GetCreateCell(colStart + 2).SetCellValue(item.Text);
                }
                break;

            case 4:
                foreach (DefineSelectItem item in lst)
                {
                    row = GetCreateRow(sheet, rowStart++);

                    row.GetCreateCell(colStart).SetCellValue(item.Value);

                    row.GetCreateCell(colStart + 1).SetCellValue(item.Value2);

                    row.GetCreateCell(colStart + 2).SetCellValue(item.Value3);

                    row.GetCreateCell(colStart + 3).SetCellValue(item.Text);
                }
                break;

            case 2:
            default:
                foreach (DefineSelectItem item in lst)
                {
                    row = GetCreateRow(sheet, rowStart++);

                    row.GetCreateCell(colStart).SetCellValue(item.Value);

                    row.GetCreateCell(colStart + 1).SetCellValue(item.Text);
                }
                break;
            }
        }