1

We have generated a .csv file using Open CSV library in java. Our requirement is to change the extension from .csv to .xls .

When we changed the extension blindly(in java code) by renaming the file name to .xls in java, the data is not aligned or formatted properly.

In .csv file when we open it with excel values inside table are aligned properly. But when we change to .xls and open it, everything is comma separated values and populated inside one column i.e., the values in table are not populated under respective column. Please find below the screenshot.

enter image description here

user1292755
  • 23
  • 1
  • 5
  • csv != xls - use Apache POI if you want **real** xls – Scary Wombat May 24 '17 at 05:26
  • What do you mean you want to "change the extension"? Do you literally have _one_ .csv file that you want to turn into .xls? Or do you want to change your app that writes .csv files into an app that writes the same data to .xls files? – Kevin Anderson May 24 '17 at 05:35
  • We have one .csv file that we want to turn into .xls. – user1292755 May 24 '17 at 05:58
  • The conversion of CSV into XLS will ask you to read the CSV with an API to write the value in an XLS using a different API (well probably the same API), but the data are not store the same way in both extension. Apache POI is the obvious choice for XLS – AxelH May 24 '17 at 05:59
  • So just import it into Excel and then save in .XLS format. The Help in Excel should be able to tell you how to do that. No Java programming needed. – Kevin Anderson May 24 '17 at 06:15

3 Answers3

0

So why not open the .csv file in excel and then do a "Save As" and for file type select excel spreadsheet.

That is the part you are missing. Changing the extension does not change the file type. You are just changing the way most computers see the file. Open up an real excel spreadsheet in a text editor and I assure you will see alot more than comma separated values.

Scott Conway
  • 975
  • 7
  • 13
0

You should look for vbs scripts, I know that I'm doing the opposite (xlsx to csv) using one of those script that I found here so I guess that it should be possible to do the opposite, I hope that you find your solution there !

Here is a script to convert a xlsx to a csv :

if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
Tylones
  • 117
  • 10
-1

I think you need to use "Apache POI - the Java API" for .xls