4

I'm using Debian Linux and I want to convert a CSV file to an Excel XLS Spreadsheet file. The catdoc package includes the xls2csv command that converts from XLS to CSV. However it doesn't do the reverse.

Since I just have a CSV file, I don't care about formatting or anything like that. I'm not worried if it only generates a very simple XLS file, and doesn't support the fancy new versions. Just so long as it's an XLS spreadsheet.

Amandasaurus
  • 33,461

9 Answers9

12

Try using the ssconvert tool from "gnumeric" package. On Debian, install the package with sudo apt-get install gnumeric and then from the command-line, run:

ssconvert file.csv file.xls

This should do the job.

Magellan
  • 4,471
xX0v0Xx
  • 129
  • 1
  • 4
9

Why would you want to do that? Since you are not interested in adding or modifying data/metadata, you could just leave it as csv. CSV is associated with MS Excel by default, so whoever is going to open it, is going to get it opened in MS Excel.

If that does not work for you for some reason, keep in mind, that, as far as I know, you can generate a (sort of) valid xls file by using the following skeleton:

<table>
<tr>
  <td>field0</td>
  <td>field1</td>
  ..
  <td>fieldX</td>
</tr>
... ad inf
</table>

(I am serious)

shylent
  • 820
6

There is a python based solution on Sourceforge called csv2xls which may fit the bill.

It does not appear to be maintained at the moment (last activity was over a year ago) but if it does what you need then that shouldn't matter too much.

1

Try using an Openoffice macro...

See for a start : How to convert Word -> PDF from the command line http://www.oooforum.org/forum/viewtopic.phtml?t=3772


flint
  • 11
1

There are indeed some problems with the filters from CSV and any form XLS or even openOffice. Like the: Record separator: , Line terminator: \n or ; String definition: " or the like.

If I get it correctly you want to do it on the command line within some scripts. This is exactly the same problem I have right now: from DATA in csv format, present it to the customer in XLS format.

I have found this post: https://forum.openoffice.org/en/forum/viewtopic.php?f=5&t=39844

But I am not totally satisfied. They might be an option.

Indeed if you have some sort of openOffice installed you can try to play with some info from: http://ask.libreoffice.org/en/question/2641/convert-to-command-line-parameter/

On my shell I typed:

soffice --help

And part of the output is:

--convert-to output_file_extension[:output_filter_name] [-outdir ouput_dir] files
      Batch convert files.
      If -outdir is not specified then current working dir is used as output_dir.
      Eg. -convert-to pdf *.doc
          -convert-to pdf:writer_pdf_Export -outdir /home/user *.doc
fuero
  • 9,879
mariotti
  • 111
1

I would write a little code to directly output a simple version of the Excel 2002 XML format: http://en.wikipedia.org/wiki/Microsoft_Excel#File_formats

XML Spreadsheet

Though the intended file extension for this format is .xml, the program also correctly handles XML files with .xls extension. This feature is widely used by third-party applications (e.g. MySQL Query Browser) to offer "export to Excel" capabilities without implementing binary file format. The following example will be correctly opened by Excel if saved either as Book1.xml or Book1.xls:

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1">
   <Row>
    <Cell><Data ss:Type="String">Name</Data></Cell>
    <Cell><Data ss:Type="String">Example</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Value</Data></Cell>
    <Cell><Data ss:Type="Number">123</Data></Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>

Alternately, if that didn't provide enough features, I'd directly implement minimal .xlsx output. It is a zip file with XML files which should be tractable compared to the old binary formats.

sdw
  • 111
1

There is a software called csv2xls (written in C++), that should do exactly what you want (not to be confused with the Python2 implementation mentioned in another answer).

https://github.com/ferkulat/csv2xls

The last commit was 2 months ago and from the looks of it, the software appears to be stable.

Slizzered
  • 814
  • 6
  • 17
0

I would argue that a .CSV file is already an Excel file with the features you want. There's only one annoyance to avoid and you are all set. If you really want to convert it to .XLS, have Excel do it later. But really there's no need to do so, given the description you provide.

adamo
  • 7,045
-1

There is effectively a need to convert CSV to Excel, when you are sending files to Users who do not want to Convert themselves CSV files to Excel. There is also the way within Excel to open a CSV file and save it to an Excel formatted-file. But we want to avoid this extra step for Users. I have found, to my frustration, that , apart from PowerShell scripting , there is no reliable product in the market to do the conversion from a command-line, respecting column separators, en-of-line, headers and parameters you might want to add.

Argy
  • 1