所需的每条记录CSV文件

时间:2018-07-09 12:58:35

标签: c# export-to-csv filehelpers

我正在使用文件助手库来构造一个csv,但是我需要为每条记录创建一个csv文件。

var engine = new FileHelperEngine(typeof(SalesOrders));
int count = 0;
foreach (SalesOrders item in purchaseOrder)
{
     count++;
     string fileName = "SalesOrder_" + item.OrderNumber.ToString() + ".csv";

     var feng = new FileHelperEngine<SalesOrders>();
     string str1 = feng.WriteString(purchaseOrder);
     List<SalesOrdersItems> _orderItemsForExport = SopOrderItems.Where(a => a.ORDER_NUMBER.ToString() == item.OrderNumber).ToList();

}

但是此行

  

string str1 = feng.WriteString(purchaseOrder);

正在打印所有内容而不是每个csv中的项目。我认为这只是我的逻辑不正确。

1 个答案:

答案 0 :(得分:1)

foreach (SalesOrders item in purchaseOrder)
            {
                using (StreamWriter sw = new StreamWriter($"SalesOrder_{item.OrderNumber.ToString()}.csv"))
                {
                    var lines = TakeAllStuffFrom(item); // like a collection of $"{SalesOrder.FirstField.Trim()};{SalesOrder.SecondField.Trim()}";

                    foreach (var line in lines)
                    {
                        sw.WriteLine(line);
                    }
                }
            }

写出您希望在csv文件中显示的内容的示例,我们将找出答案:)