EDI to CSV

Exporting or importing EDI to or from CSV

To produce a CSV output out of an EDI POCO you need to first define the format of the CSV file. You can use the database model produced by Entity Framework for any of the EDIFACT or X12 transactions as a starting point, however, CSV format varies between trading partners and implementations.

Usually, the CSV format is proprietary and you'll have to convert EDI POCOs to CSV using custom code.

1. Export EDI to CSV using custom .NET method

string ExportToCsv(TS850 po)
{
	var result = new StringBuilder();
	result.AppendLine("LINE_NUMBER,UPC_NUMBER,QUANTITY,PRICE,UOM,PO_NUMBER," +
	    "PO_DATE,CUSTOMER_NAME,CUSTOMER_NUMBER,ADDRESS,CITY,STATE,POSTAL_CODE");

	string customer = "";
	string customerNr = "";
	string address = "";
	string city = "";
	string state = "";
	string postCode = "";

	var n1Loop = po.N1Loop.Where(n1 => n1.N1.EntityIdentifierCode_01 == "ST")
.FirstOrDefault(); if (n1Loop != null) { customer = n1Loop.N1.Name_02; customerNr = n1Loop.N1.IdentificationCode_04; var n3 = n1Loop.N3.FirstOrDefault(); if (n3 != null) address = n3.AddressInformation_01; var n4 = n1Loop.N4.FirstOrDefault(); if (n4 != null) { city = n4.CityName_01; state = n4.StateorProvinceCode_02; postCode = n4.PostalCode_03; } } foreach (var po1Loop in po.PO1Loop) { // Add line number var line = po1Loop.PO1.AssignedIdentification_01 + ","; // Add upc number line += po1Loop.PO1.ProductServiceID_07 + ","; // Add quantity line += po1Loop.PO1.QuantityOrdered_02 + ","; // Add price line += po1Loop.PO1.UnitPrice_04 + ","; // Add uom line += po1Loop.PO1.UnitorBasisforMeasurementCode_03 + ","; // Add po number line += po.BEG.PurchaseOrderNumber_03 + ","; // Add po date line += po.BEG.Date_05 + ","; // Add customer name line += customer + ","; // Add customer number line += customerNr + ","; // Add address line 1 line += address + ","; // Add city line += city + ","; // Add state line += state + ","; // Add postal code line += postCode + ","; result.AppendLine(line.TrimEnd(new[] { ',' })); } return result.ToString(); }

 

 

 


 

 

 

2. Import EDI from CSV using custom .NET method

TS850 ImportFromCsv(string[] lines)
{
	var result = new TS850();

	if(lines.Length < 2)
		throw new Exception("Insufficient number of lines");

	//  line[0] is the header
	var firstLine = lines[1];

	//  ST
	result.ST = new Core.Model.Edi.X12.ST();
	result.ST.TransactionSetIdentifierCode_01 = "850";
	result.ST.TransactionSetControlNumber_02 = "000000001";

	var firstLineItems = firstLine.Split(new[] { ',' });
	if (firstLineItems.Count() < 13)
		throw new Exception("Insufficient number of columns");

	//  BEG
	result.BEG = new BEG();
	result.BEG.TransactionSetPurposeCode_01 = "01";
	result.BEG.PurchaseOrderTypeCode_02 = "02";
	result.BEG.PurchaseOrderNumber_03 = firstLineItems[5];
	result.BEG.Date_05 = firstLineItems[6];

	//  Repeating N1 Loops
	result.N1Loop = new List();

	//  Begin N1 Loop
	var n1Loop = new Loop_N1_850();

	//  N1
	n1Loop.N1 = new N1();
	n1Loop.N1.EntityIdentifierCode_01 = "ST";
	n1Loop.N1.Name_02 = firstLineItems[7];
	n1Loop.N1.IdentificationCodeQualifier_03 = "ZZ";
	n1Loop.N1.IdentificationCode_04 = firstLineItems[8];

	//  Repeating N3
	n1Loop.N3 = new List();

	//  N3
	var n3 = new N3();
	n3.AddressInformation_01 = firstLineItems[9];
	n1Loop.N3.Add(n3);

	//  Repeating N4
	n1Loop.N4 = new List();

	//  N4
	var n4 = new N4();
	n4.CityName_01 = firstLineItems[10];
	n4.StateorProvinceCode_02 = firstLineItems[11];
	n4.PostalCode_03 = firstLineItems[12];
	n1Loop.N4.Add(n4);

	//  End N1 Loop
	result.N1Loop.Add(n1Loop);

	//  Repeating PO1 Loops
	result.PO1Loop = new List();

	foreach (var line in lines.Skip(1))
	{
		var lineItems = line.Split(new[] { ',' });
		if (lineItems.Count() < 13)
			throw new Exception("Insufficient number of columns");
		
		//  Begin PO1 Loop
		var po1Loop = new Loop_PO1_850();

		//  PO1
		po1Loop.PO1 = new PO1();
		po1Loop.PO1.AssignedIdentification_01 = lineItems[0];
		po1Loop.PO1.QuantityOrdered_02 = lineItems[2];
		po1Loop.PO1.UnitorBasisforMeasurementCode_03 = lineItems[4];
		po1Loop.PO1.UnitPrice_04 = lineItems[3];
		po1Loop.PO1.ProductServiceIDQualifier_06 = "UP";
		po1Loop.PO1.ProductServiceID_07 = lineItems[1];

		//  End PO1 Loop
		result.PO1Loop.Add(po1Loop);
	}

	return result;
}
Was this article helpful?
1 out of 1 found this helpful