Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
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. –  Paul Tomblin Oct 13 '09 at 12:19
    
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 '09 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 '09 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 '11 at 11:59
    
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 '13 at 15:49

7 Answers 7

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)

share|improve this answer
3  
+1 for being serious - it is! –  sybreon Oct 13 '09 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 '11 at 12:18

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.

share|improve this answer
    
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'. –  Christopher Apr 4 '13 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. –  Christopher Apr 4 '13 at 8:18

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.

share|improve this answer

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


share|improve this answer

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
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.