コード例 #1
0
        /// <summary>
        /// Reads the XLSX file from a file path or a file stream
        /// </summary>
        /// <exception cref="Exceptions.IOException">
        /// Throws IOException in case of an error
        /// </exception>
        public void Read()
        {
            try
            {
                using (memoryStream = new MemoryStream())
                {
                    ZipArchive zf;
                    if (inputStream == null || string.IsNullOrEmpty(filePath) == false)
                    {
                        using (FileStream fs = new FileStream(filePath, FileMode.Open))
                        {
                            fs.CopyTo(memoryStream);
                        }
                    }
                    else if (inputStream != null)
                    {
                        using (inputStream)
                        {
                            inputStream.CopyTo(memoryStream);
                        }
                    }
                    else
                    {
                        throw new IOException("LoadException", "No valid stream or file path was provided to open");
                    }

                    memoryStream.Position = 0;
                    zf = new ZipArchive(memoryStream, ZipArchiveMode.Read);
                    MemoryStream        ms;
                    SharedStringsReader sharedStrings = new SharedStringsReader();
                    ms = GetEntryStream("xl/sharedStrings.xml", zf);
                    sharedStrings.Read(ms);

                    workbook = new WorkbookReader();
                    ms       = GetEntryStream("xl/workbook.xml", zf);
                    workbook.Read(ms);

                    int             worksheetIndex = 1;
                    string          name, nameTemplate;
                    WorksheetReader wr;
                    nameTemplate = "sheet" + worksheetIndex.ToString(CultureInfo.InvariantCulture) + ".xml";
                    name         = "xl/worksheets/" + nameTemplate;
                    for (int i = 0; i < workbook.WorksheetDefinitions.Count; i++)
                    {
                        ms = GetEntryStream(name, zf);
                        wr = new WorksheetReader(sharedStrings, nameTemplate, worksheetIndex);
                        wr.Read(ms);
                        worksheets.Add(worksheetIndex - 1, wr);
                        worksheetIndex++;
                        nameTemplate = "sheet" + worksheetIndex.ToString(CultureInfo.InvariantCulture) + ".xml";
                        name         = "xl/worksheets/" + nameTemplate;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new IOException("LoadException", "There was an error while reading an XLSX file. Please see the inner exception:", ex);
            }
        }
コード例 #2
0
ファイル: XlsxReader.cs プロジェクト: wuzzeb/NanoXLSX
        /// <summary>
        /// Reads the XLSX file from a file path or a file stream
        /// </summary>
        /// <exception cref="Exceptions.IOException">
        /// Throws IOException in case of an error
        /// </exception>
        public void Read()
        {
            try
            {
                using (memoryStream = new MemoryStream())
                {
                    ZipArchive zf;
                    if (inputStream == null && !string.IsNullOrEmpty(filePath))
                    {
                        using (FileStream fs = new FileStream(filePath, FileMode.Open))
                        {
                            fs.CopyTo(memoryStream);
                        }
                    }
                    else if (inputStream != null)
                    {
                        using (inputStream)
                        {
                            inputStream.CopyTo(memoryStream);
                        }
                    }
                    else
                    {
                        throw new IOException("LoadException", "No valid stream or file path was provided to open");
                    }

                    memoryStream.Position = 0;
                    zf = new ZipArchive(memoryStream, ZipArchiveMode.Read);
                    MemoryStream ms;

                    SharedStringsReader sharedStrings = new SharedStringsReader();
                    ms = GetEntryStream("xl/sharedStrings.xml", zf);
                    if (ms.Length > 0) // If length == 0, no shared strings are defined (no text in file)
                    {
                        sharedStrings.Read(ms);
                    }

                    StyleReader styleReader = new StyleReader();
                    ms = GetEntryStream("xl/styles.xml", zf);
                    styleReader.Read(ms);
                    styleReaderContainer = styleReader.StyleReaderContainer;

                    workbook = new WorkbookReader();
                    ms       = GetEntryStream("xl/workbook.xml", zf);
                    workbook.Read(ms);

                    int             worksheetIndex = 1;
                    string          name;
                    string          nameTemplate;
                    WorksheetReader wr;
                    nameTemplate = "sheet" + worksheetIndex.ToString(CultureInfo.InvariantCulture) + ".xml";
                    name         = "xl/worksheets/" + nameTemplate;
                    foreach (KeyValuePair <int, string> definition in workbook.WorksheetDefinitions)
                    {
                        ms = GetEntryStream(name, zf);
                        wr = new WorksheetReader(sharedStrings, nameTemplate, worksheetIndex, styleReaderContainer, importOptions);
                        wr.Read(ms);
                        worksheets.Add(definition.Key, wr);
                        worksheetIndex++;
                        nameTemplate = "sheet" + worksheetIndex.ToString(CultureInfo.InvariantCulture) + ".xml";
                        name         = "xl/worksheets/" + nameTemplate;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new IOException("LoadException", "There was an error while reading an XLSX file. Please see the inner exception:", ex);
            }
        }