Example #1
0
        private static AbsXLSRecords ReadStreamHelper(ExcelFile excelFile, Stream inputStream, bool readSummaryStreams, ref byte[] ss, ref byte[] dss, ref byte[] macroses)
        {
            MemoryStream stream = null;

            using (Ole2CompoundFile file = new Ole2CompoundFile())
            {
                file.Load(inputStream, false);
                foreach (Ole2DirectoryEntry entry in file.Root)
                {
                    Ole2Stream stream2 = entry as Ole2Stream;
                    if (stream2 != null)
                    {
                        string name = stream2.Name;
                        if (name != null)
                        {
                            name = string.IsInterned(name);
                            if (name == "Workbook")
                            {
                                stream = new MemoryStream(stream2.GetData());
                            }
                            else if (name == "\x0005SummaryInformation")
                            {
                                if (readSummaryStreams)
                                {
                                    ss = stream2.GetData();
                                }
                            }
                            else if ((name == "\x0005DocumentSummaryInformation") && readSummaryStreams)
                            {
                                dss = stream2.GetData();
                            }
                        }
                    }
                }
            }
            if (stream == null)
            {
                throw new Exception("Provided file is not a valid BIFF8 file. Only XLS files from Excel 97 and on are supported.");
            }
            AbsXLSRecords records = new AbsXLSRecords();

            using (BinaryReader reader = new BinaryReader(stream, new UnicodeEncoding()))
            {
                records.Read(reader, new IoOperationInfo(excelFile, excelFile.FileName, IoOperation.XlsReading));
            }
            stream.Close();
            return(records);
        }
Example #2
0
 private static void SaveXLSInternal(Stream stream, AbsXLSRecords records, byte[] ss, byte[] dss, byte[] macrosesStream, bool hasMacroses)
 {
     using (Ole2CompoundFile file = new Ole2CompoundFile())
     {
         file.Root.AddStream("Workbook", XLSFileWriter.GetStream(records));
         if (ss != null)
         {
             file.Root.AddStream("\x0005SummaryInformation", ss);
         }
         if (dss != null)
         {
             file.Root.AddStream("\x0005DocumentSummaryInformation", dss);
         }
         file.Save(stream);
     }
 }