FileFormatUtil.DetectFileFormat

DetectFileFormat(Stream)

Detects and returns the information about a format of an excel stored in a stream.

public static FileFormatInfo DetectFileFormat(Stream stream)
ParameterTypeDescription
streamStream

Return Value

A FileFormatInfo object that contains the detected information.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;
    using System.IO;

    public class FileFormatUtilMethodDetectFileFormatWithStreamDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            
            // Prepare a MemoryStream with workbook data
            using (MemoryStream stream = new MemoryStream())
            {
                // Save workbook to stream in XLSX format
                workbook.Save(stream, SaveFormat.Xlsx);
                stream.Seek(0, SeekOrigin.Begin); // Reset stream position

                try
                {
                    // Detect file format from stream
                    FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(stream);
                    
                    // Display detection results
                    Console.WriteLine($"Detected File Format Type: {fileFormatInfo.FileFormatType}");
                    Console.WriteLine($"Is Encrypted: {fileFormatInfo.IsEncrypted}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error detecting file format: {ex.Message}");
                }
            }

            // Save the original workbook for verification
            workbook.Save("DetectFileFormatWithStreamDemo.xlsx");
        }
    }
}

See Also


DetectFileFormat(Stream, string)

Detects and returns the information about a format of an excel stored in a stream.

public static FileFormatInfo DetectFileFormat(Stream stream, string password)
ParameterTypeDescription
streamStream
passwordStringThe password for encrypted ooxml files.

Return Value

A FileFormatInfo object that contains the detected information.

Examples

using System;
using System.IO;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class FileFormatUtilMethodDetectFileFormatWithStreamStringDemo
    {
        public static void Run()
        {
            string sourcePath = "example.xlsx";
            
            using (Stream stream = File.OpenRead(sourcePath))
            {
                FileFormatInfo info = FileFormatUtil.DetectFileFormat(stream, "test");
                Console.WriteLine("File format: " + info.FileFormatType);
                Console.WriteLine("Is encrypted: " + info.IsEncrypted);
                Console.WriteLine("Is password valid: " + FileFormatUtil.VerifyPassword(stream, "test"));
            }

            using (Stream stream = File.OpenRead(sourcePath))
            {
                FileFormatInfo info = FileFormatUtil.DetectFileFormat(stream, "1234");
                Console.WriteLine("File format: " + info.FileFormatType);
                Console.WriteLine("Is encrypted: " + info.IsEncrypted);
            }
        }
    }
}

See Also


DetectFileFormat(string)

Detects and returns the information about a format of an excel stored in a file.

public static FileFormatInfo DetectFileFormat(string filePath)
ParameterTypeDescription
filePathStringThe file path.

Return Value

A FileFormatInfo object that contains the detected information.

Remarks

Only supports checking some files with magic signature. If there is no magic signature, we can not precisely detect the file format.

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class FileFormatUtilMethodDetectFileFormatWithStringDemo
    {
        public static void Run()
        {
            string filePath = "example.xlsx";
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(filePath);
            
            Console.WriteLine("Load Format: " + info.LoadFormat);
            Console.WriteLine("File Format Type: " + info.FileFormatType);
            
            SaveFormat saveFormat = FileFormatUtil.FileFormatToSaveFormat(info.FileFormatType);
            LoadFormat loadFormat = FileFormatUtil.SaveFormatToLoadFormat(saveFormat);
            
            Console.WriteLine("Save Format: " + saveFormat);
            Console.WriteLine("Load Format (converted): " + loadFormat);
        }
    }
}

See Also


DetectFileFormat(string, string)

Detects and returns the information about a format of an excel stored in a file.

public static FileFormatInfo DetectFileFormat(string filePath, string password)
ParameterTypeDescription
filePathStringThe file path.
passwordStringThe password for encrypted ooxml files.

Return Value

A FileFormatInfo object that contains the detected information.

Remarks

Only supports checking some files with magic signature. If there is no magic signature, we can not precisely detect the file format.

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class FileFormatUtilMethodDetectFileFormatWithStringStringDemo
    {
        public static void Run()
        {
            string sourcePath = @"..\..\..\Data\";
            
            // Detect XLSX file format
            FileFormatInfo xlsxInfo = FileFormatUtil.DetectFileFormat(sourcePath + "example.xlsx", "password");
            Console.WriteLine("XLSX format: " + xlsxInfo.FileFormatType);

            // Detect DOCX file format
            FileFormatInfo docxInfo = FileFormatUtil.DetectFileFormat(sourcePath + "example.docx", "password");
            Console.WriteLine("DOCX format: " + docxInfo.FileFormatType);
        }
    }
}

See Also