How to copy a csv file data including the headers into an excel file

Hi,

My requirement is to rename a csv file into excel file. But when I tried this with Rename activity, the output excel file shows a pop up like it is corrupted.

So Can anyone help me to copy the csv file data into an excel file?

Hi @rnahasnahasuddin

image

Hope it helps!!

@rnahasnahasuddin

Use read Csv
Write datatable to excel activity

Hi,

Step 1: Read CSV File

  • Use the “Read CSV” activity to read the content of the CSV file. Make sure to check the “IncludeColumnNames” property to include headers.

Step 2: Write to Excel

  • Use the “Write Range” activity from the “System > File > Workbook” activities (if you don’t have Excel installed) or “App Integration > Excel” activities (if you have Excel installed) to write the data to an Excel file.
  • Specify the Excel file path where you want to save the data.
  • Make sure to set the starting cell to “A1” to start from the top left corner.
  • Check the “AddHeaders” property to include the column names from the CSV file.

Step 3: Save and Close Excel File

  • If you’re using the Excel activities, the “Write Range” activity will handle saving automatically.
  • If you’re using the Workbook activities, you might need to use the “Save Workbook” activity afterward, depending on your version of UiPath.

This process will transfer all the data from the CSV file to an Excel file without needing to manually rename the file extension, which can cause Excel to think the file is corrupted. Here is an example in pseudo-code:

Read CSV (Path: “path/to/your.csv”, Output: DataTable variable)
Write Range (WorkbookPath: “path/to/your.xlsx”, DataTable: the output from Read CSV)

Note:
Using this method, the data structure and content are preserved, and the Excel file should not show any corruption warnings when opened.

Hi @rnahasnahasuddin

You can use read csv file activity and then write it in an excel write range activity.

You can also use copy file activity. Enter the csv file path in ‘from’ section and enter the destination path with “.xlsx” extension for the same file in ‘To’ section.

Hope it will work!

Hi @rnahasnahasuddin try using Invoke Powershell Activity and use this script


if (-not(Get-Module -Name ImportExcel)) {
    Install-Module -Name ImportExcel -Scope CurrentUser -Force
}

# Path to the CSV file
$csvFilePath = "C:\path\to\your\file.csv"

# Path to the Excel file (output)
$excelFilePath = "C:\path\to\your\output\file.xlsx"

# Read the CSV file
$data = Import-Csv -Path $csvFilePath

# Export the data to Excel
$data | Export-Excel -Path $excelFilePath -AutoSize -AutoFilter -FreezeTopRow -WorksheetName "Data"


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.