cancel
Showing results for 
Search instead for 
Did you mean: 

Microsoft 365 - Excel VBO Reading and Writing CSV

EasyBluePrism
Level 5

I have two questions about handling CSV files with Microsoft 365 Excel VBO.
Firstly, I should be able to read a CSV and save it as a collection. In other words, I am looking for an action analogous to Get CSV Text as Collection.
On the other hand, I should be able to save an Excel file as a CSV file. Again, I did not find any action.
Can you help me with these two questions?
Best regards,

1 REPLY 1

naveed_raza
Level 6

for CSV to Collection - you can use Utility - File Management object, which will have Get CSV Collection

for Collection to CSV - you can use this below code , create new object, add new action , add code stage with 2 input parameters (csv File Path, Collection to save as CSV) , create 2 output parameter (Success and Error Message)

this code will handle all double quotes , single quotes between names and comma between names

Dim dt As DataTable = InCollection ' "collection" is your Blue Prism collection (DataTable)
Dim filePath As String = csvFilePath

Try
	Success = True
	ErrorMessage = ""

	Using sw As New StreamWriter(filePath, False, System.Text.Encoding.UTF8)
		
		'Write headers
		Dim columnNames As String = String.Join(",", dt.Columns.Cast(Of DataColumn)().Select(Function(column) """" & column.ColumnName.Replace("""", """""") & """"))
		sw.WriteLine(columnNames)
 
		'Write rows
		For Each row As DataRow In dt.Rows

			Dim fields As New List(Of String)()
			For Each column As DataColumn In dt.Columns
				Dim value As String = row(column).ToString()
				'Escape double-quotes in values, always use double quotes around value
				value = value.Replace("""", """""")
				fields.Add("""" & value & """")
			Next
			sw.WriteLine(String.Join(",", fields))
		Next
	End Using

Catch Ex as Exception
	Success = False
	ErrorMessage = Ex.ToString()

End Try