Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thank you for considering Aspose.
Also, to add to Amjad’s reply, you can use some custom coding to check the signature of the binary file to check whether it’s an Excel file or not (if that is your requirement). Please see the following code in this regard,
Sample Code:
private void Form1_Load(object sender, EventArgs e)
{
FileStream stream = new FileStream(@"e:\excels\oleObject1.bin", FileMode.Open);
bool valid = false;
int header = 0;
FileFormatType ft = GetFileType("oleObject1.bin", stream, out valid, out header);
if(valid&&ft.Equals(FileFormatType.Excel2003))
{
//open 2003 workbook;
}
//same way you can add the checks for other file formats too
}
//Function to check the file format by the signature
internal static FileFormatType GetFileType(string fileName, Stream stream, out bool isValid, out int header)
{
isValid = false;
FileFormatType type = FileFormatType.Default;
BinaryReader reader = new BinaryReader(stream);
ulong signature = reader.ReadUInt64();
header = (int)(signature & 0xFFFF);
stream.Seek(-8, SeekOrigin.Current);
if (signature == 0xe11ab1a1e011cfd0)
{
isValid = true;
type = FileFormatType.Excel2003;
}
else if ((signature & 0xFFFFFFFF) == 0x04034b50)
{
isValid = true;
type = FileFormatType.Excel2007Xlsx;
if (fileName != null)
{
string extension = Path.GetExtension(fileName);
if (extension != null)
{
switch (extension.ToLower())
{
case ".xlsx":
type = FileFormatType.Excel2007Xlsx;
break;
case ".xlsm":
type = FileFormatType.Excel2007Xlsm;
break;
case ".xltx":
type = FileFormatType.Excel2007Xltx;
break;
case ".xltm":
type = FileFormatType.Excel2007Xltm;
break;
}
}
}
}
else
{
if ((signature & 0xFF) == 0xEF)//Uicode Text
{
stream.Seek(3, SeekOrigin.Current);
}
char[] chs = reader.ReadChars(5);
if ((new string(chs)).ToLower().Equals("<?xml"))
{
isValid = true;
type = FileFormatType.SpreadsheetML;
}
stream.Seek(0, SeekOrigin.Begin);
}
return type;
}
Thank You & Best Regards,