Saved workbook have file format changed (#1609)

Hi,

when I save workbook, its file format gets changed unexpectedly from xlt(52) to xlsx(6). Can you help me with this? I am using aspose.cells 17.11.0 for java.

    String fileName = "home/Book4.xlt";
    Workbook workbook = new Workbook(fileName);
    String path =  "home";
    String name = "Book4-aa.xlt";
    System.out.println(workbook.getFileFormat()); //prints 52 (xlt file format)
    workbook.save(new FileOutputStream(new File(path, name)), workbook.getFileFormat());
    System.out.println(workbook.getFileFormat()); //prints 6 (xlsx file format)

Book4.xlt.zip (6.5 KB)

Regards,
Zeljko

@Zeljko,

We were able to observe the issue but we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSJAVA-42698 - File format is changed after saving the workbook

@Zeljko,

Please use the constants defined in SaveFormat because the second parameter for Workbook.Save() is not FileFormatType but SaveFormat. Let us know your feedback.

Hi,

There is no XLT field in SaveFormat class. What field should I use to save xlt file?

@ Zeljko,

Please see the updated code segment, it works fine as we tested. I think it will work for your needs:
e.g
Sample code:

String fileName = "Book4.xlt";
    Workbook workbook = new Workbook(fileName);
    String path =  "f:\\files\\";
    String name = "Book4-aa.xlt";

    System.out.println(workbook.getFileFormat()); //prints 52 (xlt file format)
    
XlsSaveOptions options = new XlsSaveOptions(SaveFormat.EXCEL_97_TO_2003);
options.setTemplate(true);    
workbook.save(new FileOutputStream(new File(path, name)), options);
//re-open the file, it is compulsory
workbook = new Workbook("Book4-aa.xlt");
System.out.println(workbook.getFileFormat()); //prints 52 (xlt file format)

Hope, this helps a bit.

Thank you for the sample code. If I use workbook.save(fileName) for saving workbook, will the save format be specified automatically, depending on fileFormatType or extension of file?

@Zeljko,

When we save a file without extension and do not set SaveFormat, the default SaveFormat is XLSX. On the other hand, if we provide extension different than the save format, then extension is ignored and SaveFormat is followed. For example if I set extension to say XLSX, but SaveFormat is say CSV, the file is saved as CSV. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

@Zeljko,

Well, if you use workbook.save(fileName), I am afraid, it will be saved in XLS file format (by default). In short, you need to specify if the Excel 97-203 file is a template (i.e., “XLT”), so you got to use the following lines of code when saving:
e.g
Sample code:


XlsSaveOptions options = new XlsSaveOptions(SaveFormat.EXCEL_97_TO_2003);
options.setTemplate(true);
workbook.save(new FileOutputStream(new File(path, name)), options);