Determine FileFormat when opening from Stream

Hi,

is it possible to determine the file format when I open an excel document from a stream?

Or is is possible to save the document in the format to a stream, which the document was loaded?


Hi,

Thanks for your question and considering Aspose.Cells.

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



Yes, it is possible to detect the file format from stream. Please use

CellsHelper.DetectFileFormat(Stream stream)

Similarly, you can save your workbook into a stream. Please use.

Workbook.Save(Stream stream,SaveFormat saveFormat)

Hi,


And, here is the sample code for your reference.

Sample code:

FileStream stream = File.OpenRead(@“e:\test2\Book1.xlsx”);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Position = 0;

MessageBox.Show(CellsHelper.DetectFileFormat(stream).ToString());
Workbook wb = new Workbook(stream);


Thank you.

Thanks for your quick help.

I solved it like this:
LoadFormat oLoadFormat = CellsHelper.DetectLoadFormat(oStream);
Workbook oWorkbook = new Workbook(oExcelStream);

SaveFormat oSaveFormat = FileFormatUtil.LoadFormatToSaveFormat(oLoadFormat);
oWorkbook.Save(oOutStream, oSaveFormat);
sFileExt = FileFormatUtil.SaveFormatToExtension(oSaveFormat);

Works perfect :slight_smile:

Hi,

Thanks for sharing your sample code.

It’s good to know that you were able to solve this issue…

Let us know if you face any other issue regarding Aspose.Cells, we will help you ASAP.