コード例 #1
0
        /// <summary>
        /// Read the worksheet
        /// </summary>
        /// <param name="existingHeadingRows"></param>
        public void Read(int existingHeadingRows = 0)
        {
            FastExcel.CheckFiles();
            FastExcel.PrepareArchive();

            ExistingHeadingRows = existingHeadingRows;

            IEnumerable <Row> rows = null;

            var headings = new List <string>();

            using (Stream stream = FastExcel.Archive.GetEntry(FileName).Open())
            {
                var document = XDocument.Load(stream);
                int skipRows = 0;

                var rowElement = document.Descendants().Where(d => d.Name.LocalName == "row").FirstOrDefault();
                if (rowElement != null)
                {
                    var possibleHeadingRow = new Row(rowElement, this);
                    if (ExistingHeadingRows == 1 && possibleHeadingRow.RowNumber == 1)
                    {
                        foreach (var headerCell in possibleHeadingRow.Cells)
                        {
                            headings.Add(headerCell.Value.ToString());
                        }
                    }
                }
                rows = GetRows(document.Descendants().Where(d => d.Name.LocalName == "row").Skip(skipRows));
            }

            Headings = headings;
            Rows     = rows;
        }
コード例 #2
0
        private bool GetWorksheetPropertiesAndValidateNewName(FastExcel fastExcel, int?sheetNumber = null, string sheetName = null, string newSheetName = null)
        {
            FastExcel = fastExcel;
            bool newSheetNameExists = false;

            FastExcel.CheckFiles();
            FastExcel.PrepareArchive();

            //If index has already been loaded then we can skip this function
            if (Index != 0)
            {
                return(true);
            }

            if (!sheetNumber.HasValue && string.IsNullOrEmpty(sheetName))
            {
                throw new Exception("No worksheet name or number was specified");
            }

            using (Stream stream = FastExcel.Archive.GetEntry("xl/workbook.xml").Open())
            {
                var document = XDocument.Load(stream);

                if (document == null)
                {
                    throw new Exception("Unable to load workbook.xml");
                }

                var sheetsElements = document.Descendants().Where(d => d.Name.LocalName == "sheet").ToList();

                XElement sheetElement = null;

                if (sheetNumber.HasValue)
                {
                    if (sheetNumber.Value <= sheetsElements.Count)
                    {
                        sheetElement = sheetsElements[sheetNumber.Value - 1];
                    }
                    else
                    {
                        throw new Exception(string.Format("There is no sheet at index '{0}'", sheetNumber));
                    }
                }
                else if (!string.IsNullOrEmpty(sheetName))
                {
                    sheetElement = (from sheet in sheetsElements
                                    from attribute in sheet.Attributes()
                                    where attribute.Name == "name" && attribute.Value.Equals(sheetName, StringComparison.OrdinalIgnoreCase)
                                    select sheet).FirstOrDefault();

                    if (sheetElement == null)
                    {
                        throw new Exception(string.Format("There is no sheet named '{0}'", sheetName));
                    }

                    if (!string.IsNullOrEmpty(newSheetName))
                    {
                        newSheetNameExists = (from sheet in sheetsElements
                                              from attribute in sheet.Attributes()
                                              where attribute.Name == "name" && attribute.Value.Equals(newSheetName, StringComparison.OrdinalIgnoreCase)
                                              select sheet).Any();

                        if (FastExcel.MaxSheetNumber == 0)
                        {
                            FastExcel.MaxSheetNumber = (from sheet in sheetsElements
                                                        from attribute in sheet.Attributes()
                                                        where attribute.Name == "sheetId"
                                                        select int.Parse(attribute.Value)).Max();
                        }
                    }
                }

                Index = sheetsElements.IndexOf(sheetElement) + 1;

                Name = (from attribute in sheetElement.Attributes()
                        where attribute.Name == "name"
                        select attribute.Value).FirstOrDefault();
            }

            if (!Exists)
            {
                throw new Exception("No worksheet was found with the name or number was specified");
            }

            if (string.IsNullOrEmpty(newSheetName))
            {
                return(false);
            }
            else
            {
                return(!newSheetNameExists);
            }
        }