Articles in this section
Category / Section

How to convert a CSV file into Excel document using XlsIO in PowerShell?

1 min read

Excel files can be created from PowerShell commands using .NET XlsIO. The following code sample illustrates how to convert a CSV file into an Excel document using XlsIO and Powershell.

Powershell commands to convert CSV to Excel file

#Getting Current Directory

$CurrentPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

 

#Defining input CSV file

$inputCSV = "$CurrentPath\Data\input.csv"

 

#Defining output XLSX file

$outputExcel = "$CurrentPath\Output\output.xlsx"

 

#Defining XlsIO and Compression Assemblies

$XlsIODLLPath = "$CurrentPath\Assemblies\Syncfusion.XlsIO.Base.dll"

$CompressionDLLPath = "$CurrentPath\Assemblies\Syncfusion.Compression.Base.dll"

 

#Load Assemblies

[Reflection.Assembly]::LoadFrom($XlsIODLLPath) | Out-Null

[Reflection.Assembly]::LoadFrom($CompressionDLLPath) | Out-Null

 

#Initialize the Excel engine Object

$Engine = New-object -TypeName Syncfusion.XlsIO.ExcelEngine

 

#Initialize the Excel application object.

$Application = $Engine.Excel

 

#Define separator

$Separator = ","

 

#Preserve data type from CSV file

$Application.PreserveCSVDataTypes = '1'

 

#Open CSV file using XlsIO

$Workbook = $Application.Workbooks.Open([string]$inputCSV ,[string]$Separator)

 

#Assigning Excel Version

$Workbook.Version = [Syncfusion.XlsIO.ExcelVersion]::Excel2016

 

#Saving the file as XLSX

$Workbook.SaveAs($outputExcel)

 

Write-Host "CSV File $inputCSV saved as Excel file $outputExcel using XlsIO."

 

Save the Powershell script file and run the file using below command in Windows Powershell.

 

Note:

If the script throws an exception “PowerShell script is not loaded in the system”, please use below command to run the script. 

 

 

powershell.exe -ExecutionPolicy ByPass -FileF:Sample\Sample.ps1 

 

 

The sample files can be downloaded here.

Conclusion

I hope you enjoyed learning about how to convert a CSV file into Excel document using XlsIO in PowerShell.

You can refer to our WinForms Excel feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Excel example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied