示例#1
0
        public void Process(XmlWriter wtr, ISheet sheet)
        {
            if (String.IsNullOrEmpty(this.Namespace))
            {
                wtr.WriteStartElement(this.Name);
            }
            else
            {
                wtr.WriteStartElement("s", this.Name, this.Namespace);
            }


            foreach (KeyValuePair <int, ExcelRowSchema> row in this.Rows)
            {
                ExcelRowSchema eSchema = row.Value;

                if (eSchema.Occurrence > -1)
                {
                    for (int i = eSchema.Index; i < eSchema.Index + eSchema.Occurrence; i++)
                    {
                        IRow r = sheet.GetRow(i);

                        if (r == null)
                        {
                            continue;
                        }

                        eSchema.Process(wtr, sheet.GetRow(i));
                    }
                }
                else
                {
                    int length = sheet.LastRowNum + 1;

                    for (int i = eSchema.Index; i < length; i++)
                    {
                        IRow r = sheet.GetRow(i);

                        if (r == null)
                        {
                            continue;
                        }

                        eSchema.Process(wtr, r);
                    }
                }
            }

            wtr.WriteEndElement();
        }
        private void ProcessEnvelope(XmlWriter wtr, ISheet sheet)
        {
            //Row will act as group node

            int length = sheet.LastRowNum + 1;

            for (int x = EnvelopeRow; x < length; x++)
            {
                if (String.IsNullOrEmpty(this.Namespace))
                {
                    wtr.WriteStartElement(this.Name);
                }
                else
                {
                    wtr.WriteStartElement("s", this.Name, this.Namespace);
                }

                IRow er = sheet.GetRow(x);

                if (er == null)
                {
                    continue;
                }

                foreach (KeyValuePair <int, ExcelRowSchema> row in this.Rows)
                {
                    IRow r = null;

                    ExcelRowSchema eSchema = row.Value;

                    if (eSchema.Index == EnvelopeRow)
                    {
                        eSchema.Process(wtr, er);
                    }
                    else
                    {
                        if (eSchema.Occurrence > -1)
                        {
                            for (int i = eSchema.Index; i < eSchema.Index + eSchema.Occurrence; i++)
                            {
                                r = sheet.GetRow(i);

                                if (r == null)
                                {
                                    continue;
                                }

                                eSchema.Process(wtr, sheet.GetRow(i));
                            }
                        }
                        else
                        {
                            r = sheet.GetRow(eSchema.Index);

                            eSchema.Process(wtr, r);
                        }
                    }
                }


                wtr.WriteEndElement();
            }
        }
        public ExcelWorkBookSchema(XmlSchema schema, IFormulaEvaluator formulaEvaluator)
        {
            bool rootFound = false;

            XmlSchemaElement workbook = null;

            foreach (var item in schema.Items)
            {
                if (item is XmlSchemaAnnotation)
                {
                    //Check to see if its an envelope schema
                    //In that case all rows in a sheet is expected to grouped, only Sheet is the allowed to be unbounded
                    Envelope = IsEnvelope((XmlSchemaAnnotation)item);
                }
                else
                {
                    workbook = (XmlSchemaElement)item;

                    if (rootFound)
                    {
                        throw new ArgumentException("Source schema is only allowed to have one root node");
                    }

                    rootFound = true;
                }
            }

            this.Namespace = workbook.QualifiedName.Namespace;
            this.Name      = workbook.QualifiedName.Name;

            if (!(workbook.SchemaType is XmlSchemaComplexType))
            {
                throw new ArgumentException("Nodes bellow root node must be of record type");
            }

            //first must be complex type containing sequences of Sheet type objects
            XmlSchemaComplexType tp = (XmlSchemaComplexType)workbook.SchemaType;

            XmlSchemaSequence seq = (XmlSchemaSequence)tp.Particle;

            for (int i = 0; i < seq.Items.Count; i++)
            {
                XmlSchemaElement sheet = (XmlSchemaElement)seq.Items[i];

                if (sheet.Name == null)
                {
                    sheet = (XmlSchemaElement)schema.Elements[sheet.RefName];
                }

                int sheetIndex = GetIndex(sheet.Annotation, sheet.Name);

                if (sheetIndex == -1)
                {
                    sheetIndex = i;
                }

                ExcelSheetSchema eSheet = new ExcelSheetSchema
                {
                    Name       = sheet.Name,
                    Namespace  = sheet.QualifiedName.Namespace,
                    Index      = sheetIndex,
                    IsEnvelope = Envelope
                };

                this.Sheets.Add(i, eSheet);



                XmlSchemaComplexType sheetType = (XmlSchemaComplexType)sheet.SchemaType;

                XmlSchemaSequence sheetSequence = (XmlSchemaSequence)sheetType.Particle;

                for (int x = 0; x < sheetSequence.Items.Count; x++)
                {
                    XmlSchemaElement row = (XmlSchemaElement)sheetSequence.Items[x];

                    int rowIndex = GetIndex(row.Annotation, row.Name);

                    if (rowIndex == -1)
                    {
                        rowIndex = x;
                    }

                    XmlSchemaComplexType rowType = (XmlSchemaComplexType)row.SchemaType;

                    int occurrence = row.MaxOccursString == "unbounded" ? -1 : (int)row.MaxOccurs;//Only last Row is allowed to have unbounded

                    ExcelRowSchema eRow = new ExcelRowSchema
                    {
                        Name       = row.Name,
                        Namespace  = row.QualifiedName.Namespace,
                        Index      = rowIndex,
                        Occurrence = occurrence
                    };

                    if (row.MinOccurs == 0 && row.MaxOccurs == 1)//Last row with this configuration is expected to be an repetative record
                    {
                        eSheet.EnvelopeRow = rowIndex;
                    }

                    if (rowType.Attributes.Count > 0)
                    {
                        foreach (XmlSchemaAttribute item in rowType.Attributes)
                        {
                            XmlSchemaSimpleType sp = item.AttributeSchemaType;

                            //  XmlSchemaDatatype tp;
                            //  XmlTypeCode.DateTime
                            int cellIndex = GetIndex(item.Annotation, item.Name);

                            if (cellIndex > -1)
                            {
                                eRow.Cells.Add(cellIndex, new ExcelCellSchema
                                {
                                    XmlType          = sp.Datatype.TypeCode,
                                    Index            = cellIndex,
                                    Name             = item.Name,
                                    NodeType         = 'A',
                                    FormulaEvaluator = formulaEvaluator
                                });
                            }
                        }
                    }

                    if (rowType.Particle != null)
                    {
                        XmlSchemaSequence rowSequence = (XmlSchemaSequence)rowType.Particle;

                        foreach (XmlSchemaElement item in rowSequence.Items)
                        {
                            int cellIndex = GetIndex(item.Annotation, item.Name);

                            if (cellIndex > -1)
                            {
                                eRow.Cells.Add(cellIndex, new ExcelCellSchema
                                {
                                    XmlType  = item.ElementSchemaType.TypeCode,
                                    Index    = cellIndex,
                                    Name     = item.Name,
                                    NodeType = 'E'
                                });
                            }
                        }
                    }

                    eSheet.Rows.Add(x, eRow);
                }
            }
        }