sharmila
sharmila

Reputation: 1533

Read contents of excel file from Stream

I have a file stream which is created from an excel file. I need to read the contents of this stream and get the excel sheets from it. Any ideas how can I do this?

Note: I have to do this without using any third party libraries

WebRequest request = WebRequest.Create(new Uri("http://servername:1056/ExcelFiles/Myfile.xlsx", UriKind.Absolute));
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
Stream fs = response.GetResponseStream() as Stream;

Here I need to read the contents of fs without having to saving the file to local system/disk

Upvotes: 0

Views: 8905

Answers (2)

MethodMan
MethodMan

Reputation: 18863

Try something like this

WebClient webClient= new WebClient();
Stream stream = webClient.OpenRead("http://servername:1056/ExcelFiles/Myfile.xlsx");
  //StreamReader streamReader = new StreamReader(stream);
  //String content = streamReader.ReadToEnd();
// Read the Document using SpreadSheetDocument Method 
var ssDoc = SpreadSheetDocument.Open(stream, false);

SpreadsheetDocument.Open method (Stream, Boolean)

Upvotes: 0

Peru
Peru

Reputation: 2971

you can use SpreadSheetDocument for reading the data from the stream which has a Static method

var doc = SpreadSheetDocument.Open(docName, true);

More on this link http://msdn.microsoft.com/en-us/library/ff478410

Reading as memmorystream

WebClient wc = new WebClient();

byte[] fileArray = wc.DownloadData("url is inserted here");

MemoryStream ms = new MemoryStream(fileArray);

Upvotes: 0

Related Questions