Example #1
0
        private void Load(ZipArchiveEntry entry)
        {
            XDocument document = XDocument.Load(entry.Open());
            XElement root = document.Root;
            XNamespace ns = NS_MAIN;

            // Find hidden columns
            List<int> hiddenColumns = GetHiddenColumns(root, ns);

            // Loop throgh rows
            XElement sheetData = root.Element(ns + "sheetData");
            foreach (XElement eRow in sheetData.Elements(ns + "row"))
            {
                // Skip empty rows
                if (eRow.Descendants(ns + "v").Count() == 0)
                    continue;

                // Set row properties
                XAttribute attr1 = eRow.Attribute("r");
                XAttribute attr2 = eRow.Attribute("hidden");
                int index = Convert.ToInt16(attr1.Value);
                bool hidden = (attr2 != null && attr2.Value == "1") ? true : false;
                Row row = new Row(index, hidden);

                // Loop through cells on row
                foreach (XElement eCell in eRow.Elements(ns + "c"))
                {
                    // Skip empty cells
                    XElement xValue = eCell.Element(ns + "v");
                    if (xValue == null)
                        continue;

                    // Get cell position
                    string position = eCell.Attribute("r").Value;
                    Match match = Regex.Match(position, @"([A-Z]+)(\d+)");
                    string letters = match.Groups[1].Value;
                    string numbers = match.Groups[2].Value;
                    int columnIndex = GetColumnIndex(letters);
                    int rowIndex = Convert.ToInt16(numbers);

                    // Get cell content
                    int number = Convert.ToInt16(xValue.Value);
                    string sharedString = string.Empty;
                    this.workbook.SharedStrings.TryGetValue(number, out sharedString);

                    // Make column
                    Column column = GetColumn(columnIndex);
                    column.Hidden = (hiddenColumns.Contains(columnIndex)) ? true : false;

                    // Make cell
                    Cell cell = new Cell(sharedString);
                    cell.Column = column;
                    cell.Row = row;

                    // Add cell to row and column
                    row.AddCell(cell);
                    column.AddCell(cell);

                    // Add rows and column to sheet
                    this.AddRow(row);
                    this.AddColumn(column);

                    /* We add rows and columns multiple times here and let
                     * the add methods filter out existing ones. */
                }
            }
        }
Example #2
0
        public void AddRow(Row row)
        {
            Row match = (from r in this.rows
                         where r.Index == row.Index
                         select r).SingleOrDefault();

            if (match == null)
            {
                row.Sheet = this;
                this.rows.Add(row);
            }
        }