简体   繁体   English

CSV文件到.txt的总金额

[英]sum amounts from csv file to .txt

CSV data to .txt and sum all the amounts CSV数据为.txt,并将所有金额相加

string fileName = "../../TechFiles/";

using (var reader = new StreamReader(ConfigurationManager.AppSettings["ConfigurationSource"]))
{
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();

        var values = line.Split(',');

        string fullFileName = fileName + values[4] + ".txt";

        List<helper> package = new List<helper>
        {
            new helper() { bankName = values[4], amount = double.Parse(values[6])}
        };

        List<ResultLine> result = package.GroupBy(i => i.bankName)
                                         .SelectMany(cl => cl.Select(
                                             csLine => new ResultLine
                                             {
                                                 bankName = csLine.bankName,
                                                 Quantity = cl.Count().ToString(),
                                                 amount = cl.Sum(c => c.amount),
                                             }))
                                          .ToList<ResultLine>();

        List<string> listA = new List<string>();

        foreach (var book in result)
        {
            if (!listA.Contains(book.bankName))
            {
                listA.Add(book.bankName);

                File.WriteAllText(fullFileName,
                    book.bankName + " " + book.amount + " " + book.Quantity);

            }
        }

I put csv path on the app.config then retrieved all the data to the text file, but the problem is I want to some my value[4] which is a header and a sum of all the amounts to the header but it only returns a single amount, so I need a way to pass all the amounts same time so I can be able to sum the total. 我将csv路径放在app.config上,然后将所有数据检索到文本文件中,但是问题是我想要一些我的value[4] ,它是一个标头和标头的所有金额的总和,但仅返回单个金额,因此我需要一种同时传递所有金额的方法,以便能够对总计进行总计。

您将需要遍历所有值两次(一次要计算总和,一次要写入文本文件),或者在将值写入文本文件时求和,然后将标题行插入文本文件中您已经写了所有详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM