0

I am implementing an interface that has 2 parameters: (System.IO.Stream input, System.IO.Stream output).

If the input stream was originated in an XLSX file I would be using DocumentFormat.OpenXml.Packaging.SpreadsheetDocument to read the document.

If it was CSV it would be even easier: StreamReader.ReadLine().

However the stream is coming from an XLS file and I am not quite sure how to read it correctly.

I was trying to figure out whether Microsoft.Office.Interop.Excel can read streams but I'm not sure it is possible.

Is there any way to analyse an XLS file provided via System.IO.Stream?

10p
  • 5,488
  • 22
  • 30
  • Is someone passing a stream to your application? Or are you creating the stream in your application? – Chuck Buford Sep 24 '14 at 16:21
  • Someone is passing the stream, I have no control over it. – 10p Sep 24 '14 at 16:22
  • So, just to clarify, someone is passing a spreadsheetdocument object to you if the file is XLS. Correct? – Chuck Buford Sep 24 '14 at 16:24
  • It is a binary file. Reading it and understanding it are not the same. Write the binary to disc and then open it with Excel. I know it seems wasteful but it I don't think Interop.Excel will accept a stream. – paparazzo Sep 24 '14 at 16:27
  • You might be able to open the stream using the Excel ODBC driver. Search MSDN for Excel ODBC driver. – paparazzo Sep 24 '14 at 16:29
  • possible duplicate of [Read excel file from a stream](http://stackoverflow.com/questions/560435/read-excel-file-from-a-stream) – C-Pound Guru Sep 24 '14 at 16:40
  • @Blam, I know I can save the stream to disc and then open it with Excel, but this is something I am trying to avoid. Cannot find how it can be done with the Excel ODBC driver so far. – 10p Sep 24 '14 at 16:45

1 Answers1

1

Here is another answer. It involves a new library (exceldatareader).

Read excel file from a stream

Community
  • 1
  • 1
Chuck Buford
  • 321
  • 1
  • 9
  • Thanks, Chuck, but as it was mentioned in the question I am familiar with the `SpreadsheetDocument` solution - it can be used if the Excel file is in XML format (they usually have extension XLSX). I need to open an XLS file (Excel 97-2003 Workbook). – 10p Sep 24 '14 at 16:34
  • Thank you for the clarification (I must have a reading comprehension problem today). I updated the answer for you. – Chuck Buford Sep 24 '14 at 16:37
  • I had hopes for a standard solution but it seems there is no standard way of doing it. I'll check the suggested solution, thanks. – 10p Sep 24 '14 at 16:50
  • I won't be able to use the Excel Data Reader library on the project, so I am going to upvote but not accept the answer. I'll just have to find another solution or a workaround that doesn't involve 3rd party libraries. Thanks. – 10p Sep 25 '14 at 10:33