Articles in this section
Category / Section

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

1 min read

Excel files can be created from PowerShell commands using our WinForms Excel feature tour page. 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 the below command in Windows PowerShell.

 

Note:

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

 

 

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

 

 

The sample files can be downloaded here.

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

 

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 XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, 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  to leave a comment
Access denied
Access denied