Button to Export To CSV

I am going to assume you know your .NET, given that you are attempting to do something a bit more advanced.

You might want to .NET Reflect or dotPeek on:

  • Ice.Lib.SharedUtilities.dll
  • Ice.Lib.EpiClientLib.dll

Now these won’t write the CSV for you, but they will help with making sure your CSV is up to the standards. Such as escaping characters, encapsulating spacings or chars with a comma into “” etc…

2018-11-20_1332
c1x

or you could take a chance and just DIY-self: You have to handle the file stream yourself.

foreach (DataRow row in drUD100A)
{
	// Prepare Vars
	string sColumns = String.Empty;
	string sRecords = String.Empty;

	// Prepare Field Formats
	string sColumnFormat = @"'{0}',";
	string sRecordsFormat = @"'{0}',";

	// Determine Field Type and decide if the field gets
	// quotation marks or not.
	// HORIZONTAL LOOP
	foreach (DataColumn colName in row.Table.Columns)
	{
		switch (colName.DataType.Name)
		{
			case "DateTime":
			case "String":
				sRecordsFormat = @"'{0}',";
				break;

			case "Int32":
			case "Decimal":
			case "Boolean":
				sRecordsFormat = @"{0},"; // No paranthesis
				break;

			default:
				sRecordsFormat = @"'{0}',";
				break;
		}

		sColumns += String.Format(sColumnFormat, colName.ToString() );
		sRecords += String.Format(sRecordsFormat, row[ colName ].ToString() );
	}
}

It even has a helper, to give your user a Prompt to select the output location =)


BPM Version: Could be refactored

// Write CSV Headers
Ice.Lib.writeFileLib.FileWriteLine(csvArchiveFullPath, "Company,GroupID,PartNum,RevisionNum,MtlPartNum,MtlSeq,QtyPer,UOMCode,TypeCode,NonStock,MfgComment");
 
bool csvGenerated = false;
bool hasECOMtls = false;
foreach (var ECOMtlRow in ParentECOMtls)
{
    // Create CSV Structure
    string[] csvLine = new string[] {
        ECOMtlRow.Company,
        ECOMtlRow.GroupID,
        ECOMtlRow.PartNum,
        ECOMtlRow.RevisionNum,
        ECOMtlRow.MtlPartNum,
        ECOMtlRow.MtlSeq.ToString(),
        ECOMtlRow.QtyPer.ToString(),
        ECOMtlRow.UOMCode,
        ECOMtlRow.TypeCode,
        ECOMtlRow.NonStock.ToString().ToLower(),
        ECOMtlRow.MfgComment
    };
 
    // Cleanse the Line Just like jcgomez always does
    string x = Ice.Lib.SharedUtilities.ImportExport.CsvWriter.GetCSVLine(csvLine);
 
    log( string.Format("  Writing to CSV: {0}", x) );
    Ice.Lib.writeFileLib.FileWriteLine(csvArchiveFullPath, x);
}

Also in a BPM I like to use Ice.Lib.writeFileLib.FileWriteLine its Async safe and you can write to the same log without getting File Lock issue(s). Like hitting it with 10 DMT’s at the same time, no problemo.

5 Likes