Example #1
0
        internal StringBuilder ToXmlString(SharedStrings sharedStrings, int rowNumber)
        {
            StringBuilder cell = new StringBuilder();

            if (this.Value != null)
            {
                bool isString = false;
                object value = this.Value;

                if (this.Value is int)
                {
                    isString = false;
                }
                else if (this.Value is double)
                {
                    isString = false;
                }
                else if (this.Value is string)
                {
                    isString = true;
                }

                if (isString)
                {
                    value = sharedStrings.AddString(value.ToString());
                }

                cell.AppendFormat("<c r=\"{0}{1}\"{2}>", GetExcelColumnName(this.ColumnNumber), rowNumber, (isString ? " t=\"s\"" : string.Empty));
                cell.AppendFormat("<v>{0}</v>", value);
                cell.Append("</c>");
            }

            return cell;
        }
Example #2
0
        internal StringBuilder ToXmlString(SharedStrings sharedStrings, int rowNumber)
        {
            StringBuilder cell = new StringBuilder();

            if (Value != null)
            {
                bool   isString = false;
                object value    = Value;

                if (Value is int)
                {
                    isString = false;
                }
                else if (Value is double)
                {
                    isString = false;
                }
                else if (Value is string)
                {
                    isString = true;
                }

                if (isString)
                {
                    value = sharedStrings.AddString(value.ToString());
                }

                cell.AppendFormat("<c r=\"{0}{1}\"{2}>", GetExcelColumnName(ColumnNumber), rowNumber, (isString ? " t=\"s\"" : string.Empty));
                cell.AppendFormat("<v>{0}</v>", value);
                cell.Append("</c>");
            }

            return(cell);
        }
Example #3
0
        /// <summary>
        /// Main disposal function
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (Archive == null)
            {
                return;
            }

            if (Archive.Mode != ZipArchiveMode.Read)
            {
                bool ensureSharedStrings = false;

                // Update or create xl/sharedStrings.xml file
                if (SharedStrings != null)
                {
                    ensureSharedStrings = SharedStrings.PendingChanges;
                    SharedStrings.Write();
                }

                // Update xl/_rels/workbook.xml.rels file
                UpdateRelations(ensureSharedStrings);

                // Update xl/workbook.xml file
                string[] sheetNames = UpdateWorkbook();

                // Update [Content_Types].xml file
                UpdateContentTypes(ensureSharedStrings);

                // Update docProps/app.xml file
                UpdateDocPropsApp(sheetNames);
            }

            Archive.Dispose();
        }
Example #4
0
 private IEnumerable <Cell> GetCells(XElement rowElement, SharedStrings sharedStrings)
 {
     foreach (XElement cellElement in rowElement.Elements())
     {
         Cell cell = new Cell(cellElement, sharedStrings);
         if (cell.Value != null)
         {
             yield return(cell);
         }
     }
 }
Example #5
0
 private IEnumerable<Cell> GetCells(XElement rowElement, SharedStrings sharedStrings)
 {
     foreach (XElement cellElement in rowElement.Elements())
     {
         Cell cell = new Cell(cellElement, sharedStrings);
         if (cell.Value != null)
         {
             yield return cell;
         }
     }
 }
Example #6
0
        public Row(XElement rowElement, SharedStrings sharedStrings)
        {
            try
            {
                this.RowNumber = (from a in rowElement.Attributes("r")
                                  select int.Parse(a.Value)).First();
            }
            catch (Exception ex)
            {
                throw new Exception("Row Number not found", ex);
            }

            if (rowElement.HasElements)
            {
                this.Cells = GetCells(rowElement, sharedStrings);
            }
        }
Example #7
0
 public Row(XElement rowElement, SharedStrings sharedStrings)
 {
     try
     {
         this.RowNumber = (from a in rowElement.Attributes("r")
                      select int.Parse(a.Value)).First();
     }
     catch (Exception ex)
     {
         throw new Exception("Row Number not found", ex);
     }
                 
     if (rowElement.HasElements)
     {
         this.Cells = GetCells(rowElement, sharedStrings);
     }
 }
Example #8
0
        /// <summary>
        /// Create a new Cell
        /// </summary>
        /// <param name="cellElement">Cell</param>
        /// <param name="sharedStrings">The collection of shared strings used by this document</param>
        public Cell(XElement cellElement, SharedStrings sharedStrings)
        {
            bool isTextRow = (from a in cellElement.Attributes("t")
                              where a.Value == "s"
                              select a).Any();
            string columnName = (from a in cellElement.Attributes("r")
                                 select a.Value).FirstOrDefault();

            this.ColumnNumber = GetExcelColumnNumber(columnName);

            if (isTextRow)
            {
                this.Value = sharedStrings.GetString(cellElement.Value);
            }
            else
            {
                this.Value = cellElement.Value;
            }
        }
Example #9
0
        /// <summary>
        /// Create a new Cell
        /// </summary>
        /// <param name="cellElement">Cell</param>
        /// <param name="sharedStrings">The collection of shared strings used by this document</param>
        public Cell(XElement cellElement, SharedStrings sharedStrings)
        {
            bool isTextRow = (from a in cellElement.Attributes("t")
                              where a.Value == "s"
                              select a).Any();
            string columnName = (from a in cellElement.Attributes("r")
                                 select a.Value).FirstOrDefault();

            this.ColumnNumber = GetExcelColumnNumber(columnName);

            if (isTextRow)
            {
                this.Value = sharedStrings.GetString(cellElement.Value);
            }
            else
            {
                this.Value = cellElement.Value;
            }
        }
Example #10
0
        internal void PrepareArchive(bool openSharedStrings = true)
        {
            if (Archive == null)
            {
                if (ReadOnly)
                {
                    Archive = new ZipArchive(ExcelFileStream, ZipArchiveMode.Read);
                }
                else
                {
                    Archive = new ZipArchive(ExcelFileStream, ZipArchiveMode.Update);
                }
            }

            // Get Strings file
            if (SharedStrings == null && openSharedStrings)
            {
                SharedStrings = new SharedStrings(Archive);
            }
        }
Example #11
0
        // <summary>
        // Output Row as an Xml element with cells nested
        // </summary>
        internal StringBuilder ToXmlString(SharedStrings sharedStrings)
        {
            StringBuilder row = new StringBuilder();

            if (Cells != null && Cells.Any())
            {
                row.AppendFormat("<row r=\"{0}\">", RowNumber);
                try
                {
                    foreach (Cell cell in Cells)
                    {
                        row.Append(cell.ToXmlString(sharedStrings, RowNumber));
                    }
                }
                finally
                {
                    row.Append("</row>");
                }
            }

            return(row);
        }
Example #12
0
        internal StringBuilder ToXmlString(SharedStrings sharedStrings)
        {
            StringBuilder row = new StringBuilder();

            if (this.Cells != null && Cells.Any())
            {
                row.AppendFormat("<row r=\"{0}\">", this.RowNumber);
                try
                {
                    foreach (Cell cell in this.Cells)
                    {
                        row.Append(cell.ToXmlString(sharedStrings, this.RowNumber));
                    }
                }
                finally
                {
                    row.Append("</row>");
                }
            }

            return row;
        }