C# 如何循环数据以获取标题、明细和总发票

C# 如何循环数据以获取标题、明细和总发票,c#,C#,我有一个数据(在.txt中) 如何处理数据,以便获得如下输出: 0|ABC|550656019|99828350|2 1|ABC|asdasd@gmail.com 2|956VII2019|2667378 1|ABC|bcdbcd@gmail.com 2|190700185|2,786,589 2|190700184|10,191,000 2|190700164|11,844,283 2|190700218|11,930,350 2|190700165|19,017,75

我有一个数据(在.txt中)

如何处理数据,以便获得如下输出:

0|ABC|550656019|99828350|2
1|ABC|asdasd@gmail.com
2|956VII2019|2667378
1|ABC|bcdbcd@gmail.com
2|190700185|2,786,589   
2|190700184|10,191,000   
2|190700164|11,844,283   
2|190700218|11,930,350   
2|190700165|19,017,750   
2|190700163|41,391,000  
0|DEF|550656019|100186049|1
1|DEF|efghij@gmail.com
2|A019128B|2,726,903   
2|A019206|3,119,665   
2|A019126|6,296,663   
2|A019138|6,405,257   
2|A019036|6,700,000   
2|19149|7,101,957   
2|A019128A|9,171,400   
2|A019164|9,814,861   
2|A019168|12,638,424   
2|19109|12,675,303   
2|A018928|23,535,617   
说明:

0 is for header
it will check if the data (not the row with invoice) has the same field or not (field 1("ABC") - field 4("IDR")) if yes, it will total the amount of the total invoice , and count the total record who has the same field 
so the format will be "0|field1|field3|total invoice amount|total record"

1 is for detail
it will list the detail of the header -> you can check that each row has different email , the detail will be split between the email.

2 is for invoice detail
just list the Invoice (after references) and the amount, but not the total invoice 

after you put the detail for one email, it will list the invoice then it will start to list the detail with different emails
我已经被困在这里,并且已经尝试将.txt转换为.xlsx(希望更容易),但事实并非如此

要转换为xlsx的代码

 //string[] 
            var lines = File.ReadAllLines(textBox1.Text);
            //read line then split
            xlApp = new excel.Application();
            xlWb = xlApp.Workbooks.Add();
            xlWs = (excel.Worksheet)xlWb.Worksheets.get_Item(1);

            row = 1;
            col = 1;
            for (int i = 0; i < lines.Length; i++)
            {
                textsplit = lines[i].Split('@');
                for (int j = 0; j < textsplit.Length; j++)
                {
                    try
                    {
                        xlWs.Cells[row, col] = textsplit[j].ToString();
                        col++;
                    }
                    catch (Exception ex)
                    {

                    }
                }

                row++;
                col = 1;
            }
            //save ke file excel tampungan
            xlWb.SaveAs(appPath + "testing.xlsx", excel.XlFileFormat.xlOpenXMLWorkbook, missing, missing,
                 false, false, excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
            xlWb.Close();
            closeExcel(xlApp); closeExcel(xlApp);

            textBox2.Text = appPath + "testing.xlsx";
            //ambil field penting dan dimasukkkan ke .txt
//字符串[]
var lines=File.ReadAllLines(textBox1.Text);
//读行然后拆分
xlApp=new excel.Application();
xlWb=xlApp.Workbooks.Add();
xlWs=(excel.Worksheet)xlWb.Worksheets.get_项(1);
行=1;
col=1;
对于(int i=0;i
只能生成标题,没有总量和总记录,因为我不知道循环的最佳逻辑

for (int i = 0; i < rowheader; i++)
            {
                totalrecord= 1;
                temp = 1;
                foreach (string datas in data)
                {
                    if (!fulldataheader[i][0].Equals("INV"))
                    {
                        if (data.Equals(fulldataheader[i][0] + " " + fulldataheader[i][1] + " " + fulldataheader[i][2] + " " + fulldataheader[i][3]))
                        {
                            totalrecord++;
                            //temp = 2;
                        }
                        else
                        {
                            data.Add(fulldataheader[i][0] + " " + fulldataheader[i][1] + " " + fulldataheader[i][2] + " " + fulldataheader[i][3]);
                            break;
                        }
                    }
                }

                foreach (string datas in data)
                {
                   {
                    if (fulldataheader[i][0].Equals("ABC"))
                    {
                        path = appPath + "LLG.txt";
                        header = "0|CR|SP/MP|" + rnd.Next(10000000, 99999999) + "|||" + 
                     fulldataheader[i][6] + "|" + fulldataheader[i][2] + "|123|123|totalamount|" + 
                     totalrecord + "|LLG|IDR|||";
                        temp = 1;
                    }
                    }
                    else
                    {

                    }
if (temp == 1)
                    {
                        if (!File.Exists(path))
                        {
                            using (StreamWriter sw = File.CreateText(path))
                            {
                                sw.WriteLine(header);
                            }
                        }
                        else if (File.Exists(path))
                        {
                            using (StreamWriter sw = File.AppendText(path))
                            {
                                sw.WriteLine(header);
                            }
                        }
                    }
                }
                #endregion
for(int i=0;i
我会通过客户+电子邮件将所有数据分组到一个SortedDictionary中,然后在最后使用LINQ表达式来收集结果。示例实现:

# Read data
var data = new SortedDictionary<(string id, string idr, string email), List<(string reference, decimal amount)>>();
using (var input = File.OpenText("input.txt"))
{
    List<(string reference, decimal amount)> currentInvoice = null;

    for (var line = input.ReadLine(); line != null; line = input.ReadLine())
    {
        var fields = line.Split(new char[] { '@' }, 6);
        switch (fields.Length)
        {
            case 2:
                // Sanitize input
                if (fields[0] != "INV")
                {
                    throw new Exception("Unknown record type.");
                }
                if (currentInvoice == null)
                {
                    throw new Exception("Invoice without context.");
                }

                // Parse
                var invoiceEntry = fields[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (invoiceEntry.Length == 2)
                {
                    decimal amount;
                    if (decimal.TryParse(invoiceEntry[1],
                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
                        CultureInfo.InvariantCulture,
                        out amount))
                    {
                        currentInvoice.Add((invoiceEntry[0], amount));
                    }
                }

                break;

            case 6:
                var currentContext = (id: fields[0], idr: fields[2], email: fields[5]);
                if (!data.TryGetValue(currentContext, out currentInvoice))
                {
                    currentInvoice = new List<(string reference, decimal amount)>();
                    data.Add(currentContext, currentInvoice);
                }
                break;

            default:
                throw new Exception("Unknown record.");
        }
    }
}

# Print data
foreach (var customerData in data.Keys.GroupBy(x => (x.id, x.idr), x => x.email))
{
    Console.WriteLine(String.Join("|",
        "0",
        customerData.Key.id,
        customerData.Key.idr,
        data.Where(x => x.Key.id == customerData.Key.id && x.Key.idr == customerData.Key.idr).Sum(x => x.Value.Sum(d => d.amount)),
        customerData.Count()
        ));

    foreach (var email in customerData)
    {
        Console.WriteLine(String.Join("|",
            "1",
            customerData.Key.id,
            email
            ));

        foreach (var invoiceEntry in data[(customerData.Key.id, customerData.Key.idr, email)])
        {
            Console.WriteLine(String.Join("|",
                "2",
                invoiceEntry.reference,
                invoiceEntry.amount.ToString("n0")
                ));
        }
    }
}
#读取数据
var data=新的SortedDictionary();
使用(var input=File.OpenText(“input.txt”))
{
List currentInvoice=null;
for(var line=input.ReadLine();line!=null;line=input.ReadLine())
{
var fields=line.Split(新字符[]{'@},6);
开关(字段.长度)
{
案例2:
//净化输入
如果(字段[0]!=“INV”)
{
抛出新异常(“未知记录类型”);
}
如果(当前发票==null)
{
抛出新异常(“无上下文发票”);
}
//解析
var invoiceEntry=字段[1]。拆分(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
if(invoiceEntry.Length==2)
{
小数金额;
如果(十进制三分位)(发票条目[1],
NumberStyles.AllowDecimalPoint | NumberStyles.AllowThounds,
CultureInfo.InvariantCulture,
超出金额)
{
当前发票.添加((发票分录[0],金额));
}
}
打破
案例6:
var currentContext=(id:fields[0],idr:fields[2],email:fields[5]);
如果(!data.TryGetValue(currentContext,out currentInvoice))
{
currentInvoice=新列表();
添加数据(currentContext、currentInvoice);
}
打破
违约:
抛出新异常(“未知记录”);
}
}
}
#打印数据
foreach(data.Keys.GroupBy中的var customerData(x=>(x.id,x.idr),x=>x.email))
{
Console.WriteLine(String.Join(“|”),
"0",
customerData.Key.id,
customerData.Key.idr,
数据。其中(x
# Read data
var data = new SortedDictionary<(string id, string idr, string email), List<(string reference, decimal amount)>>();
using (var input = File.OpenText("input.txt"))
{
    List<(string reference, decimal amount)> currentInvoice = null;

    for (var line = input.ReadLine(); line != null; line = input.ReadLine())
    {
        var fields = line.Split(new char[] { '@' }, 6);
        switch (fields.Length)
        {
            case 2:
                // Sanitize input
                if (fields[0] != "INV")
                {
                    throw new Exception("Unknown record type.");
                }
                if (currentInvoice == null)
                {
                    throw new Exception("Invoice without context.");
                }

                // Parse
                var invoiceEntry = fields[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (invoiceEntry.Length == 2)
                {
                    decimal amount;
                    if (decimal.TryParse(invoiceEntry[1],
                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
                        CultureInfo.InvariantCulture,
                        out amount))
                    {
                        currentInvoice.Add((invoiceEntry[0], amount));
                    }
                }

                break;

            case 6:
                var currentContext = (id: fields[0], idr: fields[2], email: fields[5]);
                if (!data.TryGetValue(currentContext, out currentInvoice))
                {
                    currentInvoice = new List<(string reference, decimal amount)>();
                    data.Add(currentContext, currentInvoice);
                }
                break;

            default:
                throw new Exception("Unknown record.");
        }
    }
}

# Print data
foreach (var customerData in data.Keys.GroupBy(x => (x.id, x.idr), x => x.email))
{
    Console.WriteLine(String.Join("|",
        "0",
        customerData.Key.id,
        customerData.Key.idr,
        data.Where(x => x.Key.id == customerData.Key.id && x.Key.idr == customerData.Key.idr).Sum(x => x.Value.Sum(d => d.amount)),
        customerData.Count()
        ));

    foreach (var email in customerData)
    {
        Console.WriteLine(String.Join("|",
            "1",
            customerData.Key.id,
            email
            ));

        foreach (var invoiceEntry in data[(customerData.Key.id, customerData.Key.idr, email)])
        {
            Console.WriteLine(String.Join("|",
                "2",
                invoiceEntry.reference,
                invoiceEntry.amount.ToString("n0")
                ));
        }
    }
}