Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Convert CSV to XLS without Excel installed

I have a server generating reports automatically. The reports are in CSV format. I need to be able to encrypt the file directly, without third party compression (no WinZIP or WinRAR).

I thought the best idea would be to convert the CSV to XLS and then password protect the XLS file, all through Powershell. Unfortunately, I do not have Office installed on the server and all the examples I have found for converting a file this way require that Excel be installed.

Does anyone know of a way to convert CSV to XLS in Powershell without having Excel installed? Or if not, can you think of a better way to password protect the CSV file without compressing it to ZIP or RAR?

like image 206
blizz Avatar asked Mar 25 '26 18:03

blizz


2 Answers

.net 4.5 now includes complete zip compression with passwords. So once you install 4.5 you should be able to access this library from powershell without any other dependencies.

like image 117
Jimbo Avatar answered Mar 28 '26 12:03

Jimbo


You can try to (if you really need use PowerShell):

  1. Download external .NET library which does not require Excel installed, like this one, or this
  2. Load it into your PowerShell session.
  3. Try to use types defined in that dll in your code.

Note! That this approach is hard one. You should expect strange behavior and plenty of security issues depending on assembly you choose to create excel files.

As for me, the best option for you is to create a simple console application in c#, which will use the assemblies above. Then launch it on a target machine every time you need.

P.S. I've tried to create PowerShell script for your issue with no success. Unfortunatelly, PowerShell works unpredictably with external libraries.

like image 36
kravasb Avatar answered Mar 28 '26 10:03

kravasb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!