Exemplo n.º 1
0
        internal static void ImportCellData(XmlReader reader, Cell cell)
        {
            if (reader.IsEmptyElement)
            {
                cell.Value = "";
                return;
            }
            XmlReaderAttributeItem xa = reader.GetSingleAttribute("Type");

            if (xa != null)
            {
                reader.Read();
                if (reader.NodeType != XmlNodeType.Text)
                {
                    cell.Value = "";
                    return;
                }
                switch (xa.Value)
                {
                case "String":
                {
                    cell.Value = reader.Value;
                    break;
                }

                case "Number":
                {
                    decimal d;
                    if (reader.Value.ParseToInt(out d))
                    {
                        cell.Value = d;
                    }
                    else
                    {
                        cell.Value = reader.Value;
                    }
                    break;
                }

                case "DateTime":
                {
                    DateTime date;
                    if (DateTime.TryParseExact(reader.Value, "yyyy-MM-dd\\Thh:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                    {
                        cell.Value = date;
                    }
                    else
                    {
                        cell.Value = reader.Value;
                    }
                    break;
                }

                case "Boolean":
                {
                    cell.Value = reader.Value == "1";
                    break;
                }
                }
            }
        }
Exemplo n.º 2
0
        private void ImportCell(XmlReader reader, Row row)
        {
            bool     isEmpty     = reader.IsEmptyElement;
            int      cellIndex   = row._Cells.Count;
            int      mergeDown   = 0;
            int      mergeAcross = 0;
            XmlStyle style       = null;
            string   formula     = "";
            string   reference   = "";

            foreach (XmlReaderAttributeItem xa in reader.GetAttributes())
            {
                if (xa.LocalName == "Index" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out cellIndex);
                    cellIndex--;
                }
                if (xa.LocalName == "StyleID" && xa.HasValue)
                {
                    style = ParentBook.GetStyleByID(xa.Value);
                }
                if (xa.LocalName == "HRef" && xa.HasValue)
                {
                    reference = xa.Value;
                }
                if (xa.LocalName == "Formula" && xa.HasValue)
                {
                    formula = xa.Value;
                }
                if (xa.LocalName == "MergeAcross" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out mergeAcross);
                }
                if (xa.LocalName == "MergeDown" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out mergeDown);
                }
            }
            Cell cell = Cells(cellIndex, row.RowIndex);

            if (style != null)
            {
                cell.Style = style;
            }
            if (!reference.IsNullOrEmpty())
            {
                cell.HRef = reference;
            }
            if (!formula.IsNullOrEmpty())
            {
                FormulaParser.Parse(cell, formula);
                return;
            }
            if (isEmpty)
            {
                return;
            }
            if (mergeDown > 0 || mergeAcross > 0)
            {
                cell.MergeStart = true;
                Range range = new Range(cell, Cells(cellIndex + mergeAcross, row.RowIndex + mergeDown));
                _MergedCells.Add(range);
                cell.ColumnSpan = range.ColumnCount;
                cell.RowSpan    = range.RowCount;
            }
            while (reader.Read() && !(reader.Name == "Cell" && reader.NodeType == XmlNodeType.EndElement))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Data")
                    {
                        ImportCellData(reader, cell);
                    }
                    else if (reader.Name == "Comment")
                    {
                        ImportCellComment(reader, cell);
                    }
                }
            }
        }