OT: Linux - convert csv to xlsx and xlsx to csv

Richard D. Williams richard at appgrp.net
Fri Mar 30 12:11:56 PDT 2018


This is probably old news to some out there, but I thought this was 
pretty cool.
Those of you using Windows, I am sure you can adapt this technique easily.

1) Downlaod PHPExcel zip file from 
https://github.com/PHPOffice/PHPExcel/releases
2) Make a folder under your web directory, i.e. /var/www/html, named 
Classes.
3) Extract the zip download into that folder.

You should end up with a directory under Classes named PHPExcel and
a file PHPExcel.php.

Here is the php script. Save it to /var/www/html/convert.xlsx.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
//** auto-sense convert
//** if file1 is .csv and file2 is .xlsx, with create the .xlsx
//** if file1 is .xlsx and file2 is .csv, with create the .csv
$file1 = $argv[1];
$file2 = $argv[2];
/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';
$objPHPExcel = PHPExcel_IOFactory::load($file1);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($file2);
?>

Here is the Linux script, /usr/local/bin/convert_xlsx
#This will convert a xlsx to csv and/or convert a csv to xlsx
#file1 is the loaded file
#file2 with an extension will determine the output formatted file
#
# Please note the arguments MUST be enclosed in quotes
#i.e. /usr/bin/php /var/www/html/convert_xlsx.php "/tmp/file.xlsx" 
"/tmp/file.csv"
#will create a CSV file from the XLSX file.
#
#i.e. /usr/bin/php /var/www/html/convert_xlsx.php "/tmp/file.csv" 
"/tmp/file.xlsx
#will create a XLSX file from the CSV file.
#
/usr/bin/php /var/www/html/convert_xlsx.php "$1" "$2"

To handle .xls file types, change the .xlsx to .xls and change 
'Excel2007' to 'Excel2005'
and save it to /usr/local/bin/convert.xls

This PHPExcel class is very powerful and I have now experimented with 
it's suptantail capabilities.  I now you can foramt cells for color, 
width, etc..
I just needed to do a conversion of my filepro csv files to xlsx.

My filepro command is;
system "/usr/local/bin/convert_xlsx \"file1.csv\" \"file2.xlsx\"";

Hope this helps somebody,

Richard D. Williams

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.celestial.com/pipermail/filepro-list/attachments/20180330/be78fa5e/attachment.html>


More information about the Filepro-list mailing list