简体   繁体   中英

Convert csv to excel worksheet with filters in linux

Currently I am generating a csv file from sql in a bash script. I am then emailing it out to other people. I was requested to add auto filters(an excel concept) to the file before emailing it out. I would like to be able to do this in a bash script with no human interaction. So far googling/stackoverflowing I have not found a way to do it.

CSV is not able applying AutoFilter. The simplest format which is readable by Excel and is able applying AutoFilter is Excel 2003 SpreadsheetML. See https://msdn.microsoft.com/en-us/library/bb226687%28v=office.11%29.aspx and https://msdn.microsoft.com/en-us/library/bb226693%28v=office.11%29.aspx

This is also pure text so it could be created by a bash script.

Simple example with AutoFilter:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>

<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>
   <Row>
    <Cell><Data ss:Type="String">name</Data></Cell>
    <Cell><Data ss:Type="String">value</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">a</Data></Cell>
    <Cell><Data ss:Type="Number">1</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">a</Data></Cell>
    <Cell><Data ss:Type="Number">2</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">b</Data></Cell>
    <Cell><Data ss:Type="Number">1</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">b</Data></Cell>
    <Cell><Data ss:Type="Number">2</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">c</Data></Cell>
    <Cell><Data ss:Type="Number">1</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">c</Data></Cell>
    <Cell><Data ss:Type="Number">2</Data></Cell>
   </Row>
  </Table>

  <AutoFilter x:Range="R1C1:R7C2" xmlns="urn:schemas-microsoft-com:office:excel">
  </AutoFilter>

 </Worksheet>

</Workbook>

Another possibility would be, that the bash script could call a software which can create really XLS or XLSX files.

If you have a Linux system then you can do the same via running the following command in your terminal:

libreoffice -display :0  --headless --convert-to xls --outdir "/path/" "/path/FileName.csv" 

Here, path means where your csv file is present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM