tags:

views:

229

answers:

6

For the ExcelPackage constructor you need a FileInfo object. I rather use some kind of stream object(f.i. MemoryStream), because I don't need to save the file to the server itself, but expose it as a FileStream anyway to the user. I don't want to make files which I have to delete lateron from servers which are only there for generating purposes and never used again. Apart from that, otherwise I need also the necessary rights for the application/user on the directory/file on the server.

So my question is then: How can I convert a stream object to a FileInfo object.

+2  A: 

A FileInfo class is a simple wrapper around a path to a file on disk.
It is not possible to have a FileInfo wrap a memory stream.

However, you can download the source code and add a constructor that takes a Stream. (The file path is only used in the WriteDebugFile method)

SLaks
+5  A: 

You can't convert the Stream as such to a FileInfo; they represent entirely different things. A Stream contains data thay may or may not represent a file on disk. A FileInfo on the other hand contains metadata about a file, that may or may not exist.

What you can do is to write the contents of the Stream to a file on disk, create a FileInfo pointing at that file and pass that FileInfo to the constructor.

Fredrik Mörk
He explicitly said that he doesn't want to do that.
SLaks
@SLaks: I know. And I informed him why his wish is unlikely to come true. However, your suggestion with adding a constructor in the code is a good one.
Fredrik Mörk
I was trying to avoid to have to change the source code, but that may become unavoidable then.
Michael
@Michael: yes it seems to stand between those two; fix the code or save the file. Either one will save the day.
Fredrik Mörk
A: 

You cant do that , what you should be doing is outputting the memory stream to file and getting the FileInfo object for the newly created file and passing it to the ExcelPackage.

RC1140
A: 

As mentioned by Fredrik Mörk, this is not possible as their is no default conversion available between both types and is not a recommended as well.

Just for reference, You can though provide your own Conversion logic by implementing IConvertible Interface. again not right approach in this scenario, but may be helpful somewhere else.

class CustomStream : Stream, IConvertible
    {
        public FileInfo ConvertToFileInfo()
        {
            return new FileInfo("");
        }
    }

This is how you convert It

 CustomStream stream = new CustomStream();
 FileInfo fileInfo = stream.ConvertToFileInfo();
Asad Butt
But whats the use... You can also create an empty FileInfo and give it to the function instead of making an extension. Probably the ExcelPackage uses the FileInfo to get values. Faking can give unpredictable results
PoweRoy
This will not work. `ExcelPackage` will try to read the file from the path in the `FileInfo`.
SLaks
+1  A: 

Adding the following constructor to ExcelPackage makes it possible to use Streams instead.

public ExcelPackage( Stream stream ) {
    _package = Package.Open( stream, FileMode.Create, FileAccess.ReadWrite );

    Uri uriDefaultContentType = new Uri( "/default.xml", UriKind.Relative );
    PackagePart partTemp = _package.CreatePart( uriDefaultContentType, "application/xml" );

    XmlDocument workbook = Workbook.WorkbookXml; 

    _package.CreateRelationship( Workbook.WorkbookUri, TargetMode.Internal, schemaRelationships + "/officeDocument" );

    _package.DeletePart( uriDefaultContentType );
}
Michael
A: 

Looking at the source code for ExcelPackage, it uses the Package.Open method on the FullName property of the FileInfo object passed to the constructor, with FileMode.Open and FileAccess.ReadWrite, to initialize a Package object.

The Package.Open method can also accept a Stream directly.

If you wanted, you could overload the ExcelPackage constructor yourself to accept a Stream parameter, and simply call Package.Open on that Stream object--which could easily be, for example, a MemoryStream.

Dan Tao