Workbook object creation fails in case of XLSB files

Hello,


I am using Aspose.Cells v8.7.1.2

In case of XLSB files, when I create a workbook object using the stream of the XLSB file, it gives an exception. I am creating a temp file using the XLSB file and giving the path of this temp file to create the workbook object. This works perfectly fine for XLSX files. The exception comes only in case of XLSB files.

Below code sample can be used to reproduce this issue :

try (FileInputStream in = new FileInputStream(“test.xlsb”);
FileOutputStream out = new FileOutputStream(“temp.tmp”):wink: {
byte[] data = new byte[2048];
int count;
try {
while ((count = in.read(data)) != -1)
out.write(data, 0, count);
} catch (Exception e) {
System.out.println(“Exception 1”);
}
} catch (Exception e) {
System.out.println(“Exception 2”);
}
try {
Workbook workbook = new Workbook(“temp.tmp”);
} catch (Exception e) {
System.out.println(“Exception 3”);
}

Can you please analyse this issue.

Thanks,
Neha

Hi,

Thanks for your posting and using Aspose.Cells.

Please change this line of your code

Workbook workbook = new Workbook(“temp.tmp”);

into these lines

LoadOptions opts = new LoadOptions(LoadFormat.XLSB);
Workbook workbook = new Workbook(“temp.tmp”, opts);

and it will fix your issue and you will get no more exception.

Hello,


Thanks for your reply.

The problem is that we are getting only the stream of the file and we create a temp file from that. We don’t have a way to identify from the stream if we are getting an XLSB file or an XLSX file.

So, we can’t use : LoadOptions opts = new LoadOptions(LoadFormat.XLSB); as it requires prior knowledge of the file extension.

So, there should be a way to create a workbook object from the file-stream. Also, since this is working fine for XLSX files, so it should also work for XLSB files.

Can you please look into this.

Thanks,
Neha

Hi Neha,


You can detect the load type from the stream using the FileFormatUtil.detectFileFormat method which returns an object of the FileFormatInfo. The aforementioned class has a method getLoadFormat which provides you the load type of the file. Please check following piece of code for elaboration.

Java

File file = new File(dir + “temp.tmp”);
FileInputStream fis = new FileInputStream(file);
FileFormatInfo info = FileFormatUtil.detectFileFormat(fis);
LoadOptions opts = new LoadOptions(info.getLoadFormat());
Workbook workbook = new Workbook(fis, opts);

Hi,


Well, you may detect the file format type from the streams prior to instantiate the Workbook object and accommodate your code accordingly, see the following sample code for your reference. Please refer to the code segment and you may write/update the code accordingly for your requirements.
e.g
Sample code:

try (FileInputStream in = new FileInputStream(“test.xlsb”);
FileOutputStream out = new FileOutputStream(“temp.tmp”):wink: {
byte[] data = new byte[2048];
int count;
try {
while ((count = in.read(data)) != -1)
out.write(data, 0, count);
} catch (Exception e) {
System.out.println(“Exception 1”);
}
} catch (Exception e) {
System.out.println(“Exception 2” + e.getMessage());
}
try {

//Detect file format
FileFormatInfo info = FileFormatUtil.detectFileFormat(“temp.tmp”);

//Gets the detected load format
String ext = FileFormatUtil.loadFormatToExtension(info.getLoadFormat());

Workbook workbook = null;
switch(ext)
{

case “.xls”:
workbook = new Workbook(“temp.tmp”);
break;

case “.xlsx”:
workbook = new Workbook(“temp.tmp”);
break;

case “.xlsb”:
workbook = new Workbook(“temp.tmp”, new LoadOptions(LoadFormat.XLSB));
System.out.println(“Done!”);
break;

}

} catch (Exception e) {
System.out.println(“Exception 3”);
}


Hope, this helps a bit.

Thank you.