Esempio n. 1
0
 public IEnumerable <SheetInfo> EnumSheetInfos()
 {
     for (int i = 0; i < sheetFiles.Length; i++)
     {
         var sheetFile = sheetFiles[i];
         using (var stream = xmlFiles.OpenReadFile(sheetFile))
             using (var reader = XmlReader.Create(stream))
             {
                 var sheetCells = XmlItem.Read(reader);
                 yield return(new SheetInfo()
                 {
                     reader = this, sheetFile = sheetFile, sheetCells = sheetCells, sheetName = sheetNames[i]
                 });
             }
     }
 }
Esempio n. 2
0
            public Reader(string xlsxFileName)
            {
                filename = xlsxFileName;

                var zipFiles = new FilesFromZip(new ZipFile(new FileStream(xlsxFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)));

                xmlFiles = zipFiles;
                resource = zipFiles;

                const string xlDir          = "xl";
                const string sCalcChainFile = "calcChain.xml";

                if (xmlFiles.FileExists(Path.Combine(xlDir, sCalcChainFile)))
                {
                    xmlFiles.RewriteFile(Path.Combine(xlDir, sCalcChainFile), null); // "delete" file
                    XmlItem rel;
                    var     sRelsFile = Path.Combine(xlDir, "_rels", "workbook.xml.rels");
                    long    nRelsFileSize;
                    using (var stream = xmlFiles.OpenReadFile(sRelsFile))
                    {
                        nRelsFileSize = stream.Length;
                        using (var reader = XmlReader.Create(stream))
                            rel = XmlItem.Read(reader).First();
                    }
                    using (var stream = new MemoryStream((int)nRelsFileSize))
                    {
                        using (var writer = new XmlTextWriter(stream, Encoding.UTF8))
                        {
                            writer.WriteStartDocument();
                            rel.WriteStartElementAndAttrs(writer);
                            foreach (var r in rel.subItems)
                            {
                                if (!r.Attrs.Any(xa => xa.Name == "Target" && xa.Value == sCalcChainFile))
                                {
                                    r.WritePart(writer);
                                }
                            }
                            writer.WriteEndElement();
                            writer.WriteEndDocument();
                        }
                        xmlFiles.RewriteFile(sRelsFile, stream.GetBuffer());
                    }
                }

                using (var stream = xmlFiles.OpenReadFile(Path.Combine(xlDir, "styles.xml")))
                    using (var reader = XmlReader.Create(stream))
                        styles = StyleInfo.ReadStyles(XmlItem.Read(reader));

                using (var stream = xmlFiles.OpenReadFile(Path.Combine(xlDir, "sharedStrings.xml")))
                    stringTable = ReadStringTable(stream).ToArray();

                var sheetsDir = Path.Combine(xlDir, "worksheets");

                var lstSheetsNames = new List <string>(8);

                using (var stream = xmlFiles.OpenReadFile(Path.Combine(xlDir, "workbook.xml")))
                    using (var reader = XmlReader.Create(stream))
                    {
                        var wbook  = XmlItem.Read(reader).ToArray();
                        var sheets = wbook.First(x => x.Name == "workbook").subItems.First(x => x.Name == "sheets").subItems.Where(x => x.Name == "sheet");
                        foreach (var name in sheets.Select(x => x.Attrs.First(a => a.Name == "name").Value))
                        {
                            lstSheetsNames.Add(name);
                        }
                    }
                sheetNames = lstSheetsNames.ToArray();

                sheetFiles = xmlFiles.EnumerateFiles(sheetsDir)
                             // sort filenames like "xl/worksheets/sheet1.xml", ..., "xl/worksheets/sheet10.xml" in numerical order
                             .OrderBy(sf =>
                {
                    var s = sf.Substring(19, sf.Length - 23);
                    return(int.Parse(s));
                }).ToArray();
            }