Пример #1
0
        //description: load stream as xdocument and then into the spreadsheet
        //parameter: stream
        //return:
        public void LoadFromFile(Stream loadFile)
        {
            XDocument mFile = XDocument.Load(loadFile);                                     //load stream into xdocument

            var elemCells = from Cell in mFile.Root.Element("Cells").Descendants("Cell")    //create list of anonymous type of cells from xdocument
                            select new
            {
                Col     = int.Parse(Cell.Attribute("Col").Value),                           //create fields for the anonymous type with the cell properties
                Row     = int.Parse(Cell.Attribute("Row").Value),
                Text    = Cell.Element("Text").Value,
                BGColor = uint.Parse(Cell.Element("BGColor").Value)
            };

            DeleteSpreadsheet();

            foreach (var elemCell in elemCells)                         //iterate through all cells from xdocument
            {
                Cell loadCell = GetCell(elemCell.Col, elemCell.Row);    //determine logic cell

                loadCell.Text    = elemCell.Text;
                loadCell.BGColor = elemCell.BGColor;
            }

            #region Other implementation: does not delete all cells first / uses FirstorDefault() many times
            //foreach(Cell loadCell in mCells)                                                //for all cells in the spreadsheet
            //{
            //    var elemCell = elemCells.FirstOrDefault(x =>
            //                                            loadCell.ColumnIndex == x.Col &&
            //                                            loadCell.RowIndex == x.Row);        //attempt to access the cell from the xdocument

            //    if (elemCell != null)                                                       //if it exists load the data into the cell
            //    {
            //        loadCell.Text = elemCell.Text;
            //        loadCell.BGColor = elemCell.BGColor;
            //    }

            //    else                                                                        //else default the cell
            //    {
            //        loadCell.Text = "";
            //        loadCell.BGColor = 4294967295;
            //    }
            //}
            #endregion

            mHistory.Reset();
        }