Convert csv file to xls file

Hi.

I have a csv file “myfile.csv” that contains XML data.
If I rename it to “myfile.xls” and I open it with Excel, I can see it like an excel file.

Now I want to read the csv file with Aspose Cells.
I try this code to read it :

LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);
Workbook workbook = new Workbook(file.getAbsolutePath(), loadOptions);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
System.out.println(cells.get(0, 0).getStringValue());

It return the xml content like this : <?xml version="1.0" encoding="utf-8"?>

I want to convert this in a xls file to read it like an Excel file.

So I try this :

workbook.save(“myfile.xls”, SaveFormat.EXCEL_97_TO_2003);

But when I read it, it contains xml.

What can I do to read the CSV file like an Excel file ?

Hi Yannick,


Thank you for contacting Aspose support.

As you have mentioned the file contains XML markup so it does not seem to be a valid CSV (Comma Separated Values). You may try loading it with LoadFormat.AUTO. In case the problem persists, please provide us the sample file for further investigation.

Hi.

I try to open the file with the option LoadFormat.Auto but I have this error :

java.lang.NullPointerException
at com.aspose.cells.a.c.h.b(Unknown Source)
at com.aspose.cells.akg.b(Unknown Source)
at com.aspose.cells.Workbook.isColorInPalette(Unknown Source)
at com.aspose.cells.apm.a(Unknown Source)
at com.aspose.cells.apm.d(Unknown Source)
at com.aspose.cells.apm.i(Unknown Source)
at com.aspose.cells.apm.h(Unknown Source)
at com.aspose.cells.apm.a(Unknown Source)
at com.aspose.cells.apm.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.(Unknown Source)
at Test.main(Test.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)


LoadOptions loadOptions = new LoadOptions(LoadFormat.AUTO);
Workbook workbook = new Workbook(file.getAbsolutePath(), loadOptions); // error here

Hi,

Thanks for your posting and using Aspose.Cells.

Please provide us your CSV file so that we replicate this issue at our end and in case of bug, log it in our database for a fix.

Hi.

Here is the file.

Hi,

Thanks for your posting and using Aspose.Cells.

Please provide us your source CSV file. The file you attached is the output XLS file. We need your source CSV file to test this issue at our end.

It’s the same file. The extension was change from csv to xls to open it with Excel.


Hi,


Thanks for providing us template file.

Your so called XLS file is actually a SpreadsheetML (.xml file). After an initial test, I observed the issue as you have mentioned. I found the exception: “java.lang.NullPointerException” on opening the SpreadsheetML file.
e.g
Sample code:

LoadOptions loadOptions = new LoadOptions(LoadFormat.SPREADSHEET_ML);
Workbook workbook = new Workbook(“export.xls”, loadOptions);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
System.out.println(cells.get(0, 0).getStringValue());

I have logged a ticket with an id “CELLSJAVA-41265” for your issue. We will look into it soon.

Once we have any update on it, we will let you know here.

Thank you.

Ok. Thanks for your help.

Hi,


Please try our latest version/fix: Aspose.Cells for Java v8.4.0.7

We have fixed your issue now.

Let us know your feedback.

Thank you.

Hi.

I download the new version and try it but I have an error.
I use this code :

public static void main(String[] args) {

LoadOptions loadOptions = new LoadOptions(LoadFormat.SPREADSHEET_ML);
Workbook workbook = null;
try {
workbook = new Workbook(“export.xls”, loadOptions);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
System.out.println(cells.get(0, 0).getStringValue());
} catch (Exception e) {
e.printStackTrace();
}
}


And the error is :

java.lang.NullPointerException
at com.aspose.cells.a.c.zh.b(Unknown Source)
at com.aspose.cells.zals.b(Unknown Source)
at com.aspose.cells.Workbook.isColorInPalette(Unknown Source)
at com.aspose.cells.zarn.a(Unknown Source)
at com.aspose.cells.zarn.d(Unknown Source)
at com.aspose.cells.zarn.i(Unknown Source)
at com.aspose.cells.zarn.h(Unknown Source)
at com.aspose.cells.zarn.a(Unknown Source)
at com.aspose.cells.zarn.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.(Unknown Source)
at Test.main(Test.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Hi,


Thanks for your template file and sample code with details.

I have tested your scenario with your template file and sample code, it does not fix the issue and I found the same exception as per your stack trace. I have logged your concerns and reopened your issue “CELLSJAVA-41265”, we will look into it soon.

We are sorry for any inconvenience caused!

Hi,

Thanks for using Aspose.Cells for Java.

Please download and try this fix: Aspose.Cells for Java v8.4.0.8 and let us know your feedback.

Hi.

This time it works fine.
The new version resolve my issue.

Thanks for your help.

Hi Yannick,


It is good to know that you are up & running again. Please feel free to contact us back in case you need our further assistance with Aspose APIs.

The issues you have found earlier (filed as CELLSJAVA-41265) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.