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.

5
  • 1
    If you rename the file from foo.csv to foo.xls, when you put it on a Windows or Mac OS machine, Excel will open it quite happily. Same thing with html files renamed to foo.doc. Oct 13, 2009 at 12:19
  • 2
    not really, I've just tested it (renamed a .csv into .xls) and, while it surely opens in Excel, the cell structure is messed up, or should I say, nonexistent - the cells in the leftmost column contain a line of the .csv file each, - the fields don't get separated. Not very useful
    – shylent
    Oct 13, 2009 at 12:21
  • Was the original .csv well-formatted? Try pasting a snippet of it into the question, as Paul's original suggestion has worked wonderfully for me in the past (Directive from management to generate Excel reports instead of CSV format - my solution was merely changing the extension of the output file and they were happy :) )
    – MikeyB
    Oct 13, 2009 at 12:46
  • If a csv saved with a .xls extension doesn't work, you could try a tab separated text file. That's an easy conversion as long as there are no newlines or tabs in the data.
    – rjmunro
    Apr 14, 2011 at 11:59
  • 1
    I need this sort of conversion at my workplace because when I pass people CSV or TSV files, half the time Excel doesn't properly detect the column separator, or the fact that strings can be quoted, or whatever, and people get confused on what's going wrong and how to resolve it. It would be a smoother experience if I could just pass people a real XLS file I 100% knew Excel wouldn't have issues with.
    – spiffytech
    May 15, 2013 at 15:49

9 Answers 9

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.

3
  • 3
    ssconvert does not keep your data in the same state as it was going in, and is as such not a utility that should be used unless you are absolutely certain that all fields are quoted etc. Example: a field with a value of 0330 will be converted to '330'. Apr 4, 2013 at 8:05
  • Actually, when I now tested ssconvert again I found that it destroys your data, regardless of quoting, and as such is a tool that in my opinion can not be used at all. Sorry, but downvote from me. Apr 4, 2013 at 8:18
  • 1
    Yeah, ssconvert is useless for any serious work. I had been using ssconvert a lot until I realized it screws up formatting for any text containing numeric characters. i.e. ditto what Christoper says. This really sucks because otherwise ssconvert is painless.
    – Cerin
    Feb 20, 2015 at 19:33
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)

4
  • 3
    +1 for being serious - it is!
    – sybreon
    Oct 13, 2009 at 13:03
  • 1
    In Office 2007, I get a warning "The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and from a trusted souce before opening the file. Do you want to open the file now?", which probaly isn't acceptable in many cases.
    – rjmunro
    Apr 14, 2011 at 12:18
  • 2
    You wouldn't want to do this because Excel is terrible at interpreting and formatting CSV files. e.g. It will automatically interpret a numeric text as an integer, even if it's quoted or 30 characters long, causing it to render in scientific notation.
    – Cerin
    Feb 20, 2015 at 19:30
  • The OP asks for an Excel spreadsheet, not for arguments on whether CSV is sufficient
    – Philipp
    Jul 1, 2021 at 13:10
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


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
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.

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.

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.

-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.

1
  • PowerShell? This is Sparta... ergh, sorry, Linux! Apr 17, 2015 at 11:49

Not the answer you're looking for? Browse other questions tagged .