Click here to Skip to main content
14,971,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to some excel processing using excel interop and later to be displayed in winform. My source of data is from memorystream.

Why excel interop? So that later i can display it in winform using edraw office viewer.

After searching the internet for days on loading excel workbook and worksheet from memorystream, i found that i can use epplus to do so. The link is from http://stackoverflow.com/questions/5738123/using-epplus-with-a-memorystream[^]

If my understanding is correct, epplus is office open xml and not excel interop. How do i convert epplus workbook type to excel interop's workbook type so that i can do excel processing?

Of course the easier way would be just load excel interop's workbook from memorystream, but i do not know how to do it. Can somebody please help me? Thanks.

Btw, i'm using .net 4.0 and my memorystream is from excel 2007.
Posted
Updated 29-Jun-20 21:38pm
v2

Why don't you just save content of your memory stream into temporary file and process that file normally with Excel application?
See my example code:
C#
// Get content of your Excel file
var ms = new MemoryStream(...);

// Get temp file name
var temp = Path.GetTempPath(); // Get %TEMP% path
var file = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); // Get random file name without extension
var path = Path.Combine(temp, file + ".xlsx"); // Get random file path

using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
    // Write content of your memory stream into file stream
    ms.WriteTo(fs);
}

// Create Excel app
Excel.Application excel = new Excel.Application();

// Open Workbook
excel.Workbooks.Open(path, ReadOnly: true);
   
Comments
JoJo82 21-Jul-13 3:48am
   
I did thought of that. However, due to the sensitivity of the information contained in the memorystream, i prefer not to save it to file if possible.
Matej Hlatky 21-Jul-13 4:05am
   
Instead of TEMP folder you can use your own working folder, such as C:\top-secret with properly set security permissions.
After you process your file, rewrite its content with randomly generated bytes and delete it.
JoJo82 21-Jul-13 4:12am
   
That looks promising. Am still trying to understand tho.
What kind of permission is that? How do I set it programmatically?
Can you explain more, maybe some sample code if possible so that I can better understand it and know how to apply it.
Thanks.
Matej Hlatky 21-Jul-13 6:08am
   
Hi, unfortunately I have no experience with this kind of scenario... this was only basic idea.
Just check the Directory.CreateDirectory Method (String, DirectorySecurity).
Member 12080968 29-May-17 5:41am
   
maybe need this link
https://stackoverflow.com/questions/15285880/how-to-reference-microsoft-office-interop-excel-dll
Instead of using
Path.GetTempPath()
and
Path.GetFileNameWithoutExtension(Path.GetRandomFileName())
better use of
TempFileCollection()
of
System.CodeDom.Compiler
:

using (var tempfile = new TempFileCollection())
{
    string filePath = tempfile.AddExtension("temp"); // or whatever you want to use
    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
    {
        fs.Write(YourByteArray, 0, YourByteArray.Length);
    }
    // Here is your code of what you want to do with this file, fill it, print it, or whatever
}
// Here the file is deleted automatically
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900