Powershell - Convert CSV to XLS without Excel installed

I have a server that generates reports automatically. Reports are presented in CSV format. I need to be able to encrypt a file directly, without third-party compression (without WinZIP or WinRAR).

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

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

+2
source share
3 answers

.net 4.5 now includes full zip compression with passwords. Therefore, once you install 4.5, you can access this library from powershell without any other dependencies.

+1
source

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

  • Download an external .NET library that does not require Excel installed like this , or it
  • Download it to a PowerShell session.
  • Try using the types defined in this DLL in your code.

Note! . This approach is complicated. You should expect strange behavior and a lot of security issues depending on the build you decided to create excel files.

#, . , .

P.S. PowerShell script . , PowerShell .

+1

CSV Excel Excel . Excel, EPPlus. CSV , EPPlus.

# Load EPPlus
$DLLPath = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\EPPlus\EPPlus.dll"
[Reflection.Assembly]::LoadFile($DLLPath) | Out-Null

# Create Excel File
$ExcelPackage = New-Object OfficeOpenXml.ExcelPackage 
$Worksheet = $ExcelPackage.Workbook.Worksheets.Add("Protected")

# Encryption
$ExcelPackage.Encryption.Algorithm = [OfficeOpenXml.EncryptionAlgorithm]::AES256
$ExcelPackage.Encryption.IsEncrypted = $true
$ExcelPackage.Encryption.Password = 'Excel'

# Protection of Workbook
$ExcelPackage.Workbook.Protection.LockRevision = $true
$ExcelPackage.Workbook.Protection.LockStructure = $true
$ExcelPackage.Workbook.Protection.LockWindows = $true
$ExcelPackage.Workbook.Protection.SetPassword("Workbook")

$ExcelPackage.Workbook.View.SetWindowSize(150, 525, 14500, 6000)
$ExcelPackage.Workbook.View.ShowHorizontalScrollBar = $false
$ExcelPackage.Workbook.View.ShowVerticalScrollBar = $false
$ExcelPackage.Workbook.View.ShowSheetTabs = $false

# Protection of Worksheet
$Worksheet.Protection.AllowAutoFilter = $false
$Worksheet.Protection.AllowDeleteColumns = $false
$Worksheet.Protection.AllowDeleteRows = $false
$Worksheet.Protection.AllowEditObject = $false
$Worksheet.Protection.AllowEditScenarios = $false
$Worksheet.Protection.AllowFormatCells = $false
$Worksheet.Protection.AllowFormatColumns = $false
$Worksheet.Protection.AllowFormatRows = $false
$Worksheet.Protection.AllowInsertColumns = $false
$Worksheet.Protection.AllowInsertHyperlinks = $false
$Worksheet.Protection.AllowInsertRows = $false
$Worksheet.Protection.AllowPivotTables = $false
$Worksheet.Protection.AllowSelectLockedCells = $false
$Worksheet.Protection.AllowSelectUnlockedCells = $false
$Worksheet.Protection.AllowSort = $false
$Worksheet.Protection.IsProtected = $true
$Worksheet.Protection.SetPassword("Worksheet")

# Save Excel File
$ExcelPackage.SaveAs("$HOME\Downloads\test.xlsx") 
+1
source

Source: https://habr.com/ru/post/1680372/


All Articles