Free Support Forum - aspose.com

Request: workbook.Open() should support FileFormatType auto-detection

Scenario:
- A user provides an Excel document in any Excel format (eg 97, 2000, 2003, SpreadsheetML, 2007 XLSX). I do not want to have to ask the user which format (s)he provided.

Sample code:
Stream stream = … // get stream from upload or file system

Workbook workbook = new Workbook();

workbook.Open(stream);



Problem:
When you use this with a non-default Excel format eg SpreadsheetML, you’ll get an Exception "Aspose.Cells.CellsException : This file’s format is not supported or you don’t specify a correct format."

Workaround 1:
Ask user to specify the provided format (not user friendly)

Workaround 2:
Trial and error: try to open the Excel document using each available file format type, catching Exceptions along the way (not ideal)
workbook.Open(stream, fileFormatType)

Ideal solution:
The workbook.Open should allow autodetecting the file format type when no FileFormatType is provided OR using a new FileFormatType.AutoDetect

What do you recommend?

Thanks,

Bernard

Hi Bernard,

Thanks for your considerations.

Well, then what about other overloaded versions of Open() method. Don't you think it will put some false impact to the existing overloaded list of Open() method version. We will still check its feaibility if we can consider it.

I think you may use your own code to manually check the file's extension of the file which you are opening using Workbook.Open() method.

E.g.,

.
.
.
string path = myFilePath;
string ext = path.Substring(path.LastIndexOf(".")+1, path.Length);

switch(ext)
{

case ".xls":
workbook.Open(myfilePath,FileFormatType.Default);
break;

case ".xlsx":
workbook.Open(myfilePath,FileFormatType.Excel2007Xlsx);
break;

case ".xml":
workbook.Open(myfilePath,FileFormatType.SpreadsheetML);
break;

case ".txt":
.....
break;

case ".csv":
......
break;

.
.
.
}

Thank you.

Hello Amjad,

Thanks for your help, I will try to apply your recommendation.

I still see added value in having this built in into the product, since it probably has to be done in many applications, and this code needs to be updated/tweaked for any version change.

Don’t you think it will put some false impact to the existing overloaded list of Open() method version.

If there would be a new type
FileFormatType.AutoDetect, then existing code would not change its behavior, while allowing new code to be simplified.

Best regards,

Bernard