Read write XLSX file via Stream on SharePoint using C#.NET

Hi Aspose Team!

I´m working on a solution which copy the content of different workbooks with the same columns into one workbook to have a better overview and be able to filter the whole data. My code works fine with files that i have stored local, but now I am trying to do it with streams to sharepoint files and I dont understand what´s the problem. At the moment the source files are local and the destination file is on Sharepoint. I get no exception and it seems like everything is working, but in the destination file is no data like I forgot to save. I´ll post my code here, but the information where the files are lying come from the user via a forms application as you will recognize. The paths to the files are correct I´ve proofed that, so can you please help me find the problem? My Sourcefile is attached.

Thanks in advance!

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.2.0.4

and let us know your feedback.

I have checked with the above version if there is any issue with streams and I found no issue.

Please see the code below, the code basically loads the source file into byte array, then it create a memory stream object and finally it create a new workbook from the memory stream object and save the workbook. And now if you see the output file, you find it is alright.

Please see the source and output files attached.

If you still find any issue, then please provide us your sample runnable project that we could run or test at our end. The code you provided has some build errors.

C#

e.g
Sample code:

string filePath = @“F:\Shak-Data-RW\Downloads\source.xlsx”;


byte[] bytes = File.ReadAllBytes(filePath);


//Create a memory stream from source bytes


MemoryStream ms = new MemoryStream(bytes);


//Use this memory stream to create a new workbook object

//and save it


Workbook workbook = new Workbook(ms);

workbook.Save(filePath + “.out.xlsx”);


Hi!

I´ve already tryed to use the File.ReadAllBytes method cause I saw it in a Video tutorial, but this method does not support URI format and my file lies on Sharepoint which i can only access with URI format I think. I give you an example of my filepath: http://testserver/testlibrary/Test%20Datei.xlsx

I´m able to open my files on sharepoint with

Stream streamname = filename.OpenBinaryStream();

Wokrbook wokrbook = new Workbook(streamname,LoadOptions.XLSX);

But it looks like the save methode doesnt work with the stream. There is no exception but also no data in my file after saving it with:

workbook.save(streamname,SaveFormat.XLSX);

Thanks in advance for helping me

Hi,

Please confirm that you are downloading your source workbook bytes from URI correctly.

You can check it by using the following code.

Suppose, you have downloaded all your bytes from your URI in the streamname object.

Please Note: You can download your bytes from URI using .NET framework APIs.

Now, if you open output.xlsx file, it should open fine. As you can notice in my code, I have not used any Aspose.Cells code at all. I am just downloading the bytes from URI (by supposing it) and then writing it on disk to see if the downloaded bytes are correct.

C#



//Suppose you have downloaded all the bytes from your URI in streamname

//Now you can check if your bytes are intact or corrupt using the following



byte[] yourStreamBytes = new byte[streamname.Length];


streamname.Read(yourStreamBytes, 0, yourStreamBytes.Length);


File.WriteAllBytes(“output.xlsx”, yourStreamBytes);



Hi and thanks for your reply!

I´m pretty sure that I´m downloading the source workbooks correctly, because I´m able to read the data out of them and save all the data into one workbook on my local machine. I´m getting the correct data on this way and everything is fine. The only thing I would like to change is that the destination workbook should be placed in sharepoint library. The problem of that is when I´m saving my workbook with workbook.save(streamname,saveformat.xlsx) it seems that the is no data put into my destination workbook. is there a known problem of the workbook.save methode in combination with streams or is there anything I be missing out?

Thanks in advance

Hi,

I think, there is something missing or wrong in your project because there is no such known issues with workbook.save(streamname,saveformat.xlsx) method.

One more thing, please try running your sharepoint library project in full trust mode and see if it works fine.

Since your question is also related to SharePoint, so I will recommend you to go through these threads.

( )