Hello,
I am encountering below exception when converting a .xls file to pdf using aspose-cells 25.2 version:
com.aspose.cells.CellsException: The file format and extension do not match.
at com.aspose.cells.v1k.c(Unknown Source)
at com.aspose.cells.v1k.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.(Unknown Source)
The exception occurs at line:
Workbook workbook = new Workbook(sourceFilePath);
What could be the solution to this problem?
Template file to reproduce issue:
EmptyExcelWithException.zip (190 Bytes)
Thanks in advance
@aakanksha76
The exception you are encountering, com.aspose.cells.CellsException: The file format and extension do not match
, typically indicates that the file you are trying to open with the Workbook
class does not match the expected format based on its extension. Here are some steps you can take to resolve this issue:
-
Check File Format: Ensure that the file you are trying to convert is indeed a valid Excel file. Sometimes, files may have the .xls
extension but are not in the correct format. You can try opening the file in Excel to verify its integrity.
-
Update Aspose.Cells Version: It is recommended to use the latest version of Aspose.Cells. The version you are using (25.2) may have bugs or limitations that have been addressed in later releases. You can download the latest version from the Aspose Releases page.
-
Sample Code: If you are still facing issues, you can try using a simple code snippet to test the conversion. Here’s an example:
Workbook workbook = new Workbook("path_to_your_file.xls");
workbook.save("output_file.pdf");
Make sure to replace "path_to_your_file.xls"
with the actual path to your Excel file.
-
File Inspection: If the problem persists, consider inspecting the file for any corruption or unusual formatting that might be causing the issue. You can also try creating a new Excel file and saving it in the .xls
format to see if that works.
If you continue to experience difficulties after trying these suggestions, please provide additional details about the file or any other error messages you may encounter.
@aakanksha76
By testing on the latest version v25.2 using sample files and the following sample code, the program can run normally without any exceptions. Please refer to the attachment. out_java.pdf (916 Bytes)
Workbook wb = new Workbook(filePath + "EmptyExcelWithException.xls");
wb.save(filePath + "out_java.pdf");
We found that there were no characters in your sample file when we opened it using a text tool. Please confirm if the uploaded sample file is the one you tested. result.png (1.5 KB)
@John.He
The issue is, we see an exception on opening specific xls files in Microsoft Excel. Please find it here -
exception.png (1.9 KB)
We deleted the data from that file and still same exception can be seen when opened again in Microsoft Excel.
Please try opening the file in Microsoft Excel.
The conversion fails with same exception
@aakanksha76
Your sample file will indeed encounter an error when opened using Office 365. But when opened in Excel 2003, there were no abnormal messages. This is an empty file. How did you generate it? What do you need to do with this empty file? Through the testing code mentioned earlier, we can convert to PDF normally without any exceptions.
@aakanksha76
Please open this file with Notepad, you will see there are only two empty rows in the file. It’s not an xls file though its extension is “.xls”. So Excel throws such exception " the file format and extension don’t match". It’s better that you can change it to correct extension.
@simon.zhao
Okay…My issue got solved using,
TxtLoadOptions loadOptionsTxt = new TxtLoadOptions(LoadFormat.TAB_DELIMITED);
Thank you for the support.
@aakanksha76
Thank you for your feedback. You are welcome. I’m glad you solved the issue by using TxtLoadOptions(LoadFormat.TAB_DELIMITED)
. If you have any questions, please feel free to contact us at any time.