Reading an xls stream using GetResponseStream and SpreadsheetDocument.Open

I want to read xls from the server. I was able to download the file using WebClient, but now I want to open it without saving the file.

var request = WebRequest.Create(location); var response = request.GetResponse(); var stream = response.GetResponseStream(); var document = SpreadsheetDocument.Open(stream, false); if (stream != null) stream.Close(); 

My code stops at SpreadsheetDocument.Open, it causes the following error:

It is not possible to work in a stream that does not support search.

What am I doing wrong, why can not I open a document?

+1
source share
1 answer

You get this error because the Open method for SpreadsheetDocument expects a searchable object . Your stream variable is an instance of NetworkStream and does not support searching. You will need to copy the network stream to a local stream (e.g. MemoryStream ) in order to use it in your code

 var request = WebRequest.Create(location); var response = request.GetResponse(); var memoryStream = new MemoryStream(); using (var networkStream = response.GetResponseStream()) { if (networkStream != null) { // Copy the network stream to an in-memory variable networkStream.CopyTo(memoryStream); // Move the position of the stream to the beginning memoryStream .Seek(0, SeekOrigin.Begin); } } var document = SpreadsheetDocument.Open(memoryStream , false); 
+3
source

Source: https://habr.com/ru/post/923002/


All Articles