cancel
Showing results for 
Search instead for 
Did you mean: 

What is the best way to paste collection into a csv file?

Venkata_Pranav_
Level 6
What is the best way to paste collection into a csv file? Should I use the Word VBO?
Pranav
11 REPLIES 11

AmiBarrett
Level 12

I would have sworn one of the default utility object had a basic action for this, but I'm not finding it. Below is the version I wrote to accept custom delimiters using c#.  

Inputs:
Collection - Collection
Delimiter - Text
Target - Text

StringBuilder sb = new StringBuilder();
string[] columnNames = Collection.Columns.Cast().Select(column => column.ColumnName).ToArray();
sb.AppendLine(string.Join(Delimiter, columnNames));
foreach (DataRow row in Collection.Rows)
{
	string[] fields = row.ItemArray.Select(field => field.ToString()).ToArray();
	sb.AppendLine(string.Join(Delimiter, fields));
}
File.WriteAllText(Target, sb.ToString());

david.l.morris
Level 14
You talking about the Utility - Strings action 'Collection to CSV'?
Dave Morris 3Ci at Southern Company Atlanta, GA

AmiBarrett
Level 12
That sounds right - I changed divisions at my company a while back, which also meant changing environments. This one doesn't seem to have that action for some reason. (Not a huge loss I suppose, when we already have an alternative.)

david.l.morris
Level 14
When you can write code like you do @amibarrett, I wouldn't feel at a loss at all not having that action. Hahah!
Dave Morris 3Ci at Southern Company Atlanta, GA

Venkata_Pranav_
Level 6
Hahah @david.l.morris. That's exactly what I was thinking. @amibarrett has code solutions for every problem (which is amazing)  I figured later this question was pretty dumb as I just wanted to paste a collection in csv file. I used Excel VBO to create instance, opened workbook, pasted the collection and closed the instance. I could use the code if there's no excel on the machine, but it worked out. 
Pranav

MatthewRoss
Level 4
Hi and nice code! I added this to the code object setup the inputs. When "checking" th code, it comes up with a handful of errors.   Any ideas on this one? As would like to give this ago for a problem we have. Thanks MoD

AndreyKudinov
Level 10
As long as none of the data contains delimiters/quotes or nulls (should not apply to bp) - that would do.

AmiBarrett
Level 12
Did you add this to an existing object? If so, is that object set up to run VB instead of C#? The above snippet is written in C#.

MatthewRoss
Level 4
I have a new object setup in C# Copied and pasted the snippet into a new code block, I'm recieving 5 errors currently when checking the code. Type: Error Action: Validate Description: Compiler error at line 1: The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?) Type: Error Action: Validate Description: Compiler error at line 1: The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?) Type: Error Action: Validate Description: Compiler error at line 7: 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) Type: Error Action: Validate Description: Compiler error at line 2: 'System.Data.DataColumnCollection' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive or an assembly reference?) Type: Error Action: Validate Description: Compiler error at line 10: The name 'File' does not exist in the current context   Any thoughts on these? Â