未写入 CSV 文件

本文关键字:文件 CSV | 更新日期: 2023-09-27 18:37:04

我正在尝试写入CSV文件,并且已经研究了一下如何做到这一点,这似乎很简单,但是当我运行它时它并没有填充我的CSV文件。该程序将写入 txt 和我的控制台,没有问题。

    StreamWriter vWriteFile = new StreamWriter("Positions2.csv");
        var path = @"C:'Users'jhochbau'documents'visual studio 2015'Projects'CsvReader'CSVReader3'Position_2016_02_25.0415.csv";
        if (vShowBoth == false)
        {
            //This determines whether to view by account or by underlying.
            Console.WriteLine("Please enter 0 if you wish to see sums by account, or 12 if you wish to see by underlying");
            int vViewControl = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Account" + "  Settle Sum" + " Open Sum" + " Buy Sum" + " Sell Sum");
            using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
            {
                bool vMore = parsedLines.MoveNext();
                while (vMore)
                {
                    // Initialize
                    bool vWasSuccessful;
                    var vAccount = parsedLines.Current[vViewControl];
                    double vSettleMMSum = 0;
                    double vOpenSum = 0;
                    int vBuySum = 0;
                    int vSellSum = 0;
                    do
                    {
                        double vParseSettleMM = 0;
                        double vParseOpen = 0;
                        int vParseBuy = 0;
                        int vParseSell = 0;
                        //Parsing data read in from strings, into temp variables
                        vWasSuccessful = double.TryParse(parsedLines.Current[7], out vParseSettleMM);
                        vWasSuccessful = double.TryParse(parsedLines.Current[8], out vParseOpen);
                        vWasSuccessful = int.TryParse(parsedLines.Current[6], out vParseBuy);
                        vWasSuccessful = int.TryParse(parsedLines.Current[10], out vParseSell);
                        //adding temp variabels to sum
                        vSettleMMSum += vParseSettleMM;
                        vOpenSum += vParseOpen;
                        vBuySum += vParseBuy;
                        vSellSum += vParseSell;
                        vMore = parsedLines.MoveNext();
                    }
                    //sets up when to break
                    while (vMore && vAccount == parsedLines.Current[vViewControl]);
                    //After each Break need to print out Account name and sums from above.
                    // Do printing here as part of the loop, at the very end of the loop code block.
                    Console.WriteLine("--------------------------------------------------------");
                    Console.WriteLine(vAccount + "  " + vSettleMMSum + "  " + vOpenSum + "   " + vBuySum + " " +
                        vSellSum);
                    //vWriteFile.Write(vAccount + "," + vSettleMMSum + "," + vOpenSum + "," + vBuySum + "," +
                    //   vSellSum);
                    vWriteFile.WriteLine("{0},{1},{2},{3},{4}", vAccount, vSettleMMSum, vOpenSum, vBuySum, vSellSum);
                    //reset sums for next loop
                    vSettleMMSum = 0;
                    vOpenSum = 0;
                    vBuySum = 0;
                    vSellSum = 0;
                }
            }
        }

未写入 CSV 文件

您应该养成使用 using 关键字包装流的习惯,这样您就不会遇到忘记关闭它的情况。最好在继承的任何内容中使用 using 关键字IDisposable

所以你的代码看起来像这样。

using(StreamWriter vWriteFile = new StreamWriter("Positions2.csv"))
{
        var path = @"C:'Users'jhochbau'documents'visual studio 2015'Projects'CsvReader'CSVReader3'Position_2016_02_25.0415.csv";
        if (vShowBoth == false)
        {
            //This determines whether to view by account or by underlying.
            Console.WriteLine("Please enter 0 if you wish to see sums by account, or 12 if you wish to see by underlying");
            int vViewControl = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Account" + "  Settle Sum" + " Open Sum" + " Buy Sum" + " Sell Sum");
            using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
            {
                bool vMore = parsedLines.MoveNext();
                while (vMore)
                {
                    // Initialize
                    bool vWasSuccessful;
                    var vAccount = parsedLines.Current[vViewControl];
                    double vSettleMMSum = 0;
                    double vOpenSum = 0;
                    int vBuySum = 0;
                    int vSellSum = 0;
                    do
                    {
                        double vParseSettleMM = 0;
                        double vParseOpen = 0;
                        int vParseBuy = 0;
                        int vParseSell = 0;
                        //Parsing data read in from strings, into temp variables
                        vWasSuccessful = double.TryParse(parsedLines.Current[7], out vParseSettleMM);
                        vWasSuccessful = double.TryParse(parsedLines.Current[8], out vParseOpen);
                        vWasSuccessful = int.TryParse(parsedLines.Current[6], out vParseBuy);
                        vWasSuccessful = int.TryParse(parsedLines.Current[10], out vParseSell);
                        //adding temp variabels to sum
                        vSettleMMSum += vParseSettleMM;
                        vOpenSum += vParseOpen;
                        vBuySum += vParseBuy;
                        vSellSum += vParseSell;
                        vMore = parsedLines.MoveNext();
                    }
                    //sets up when to break
                    while (vMore && vAccount == parsedLines.Current[vViewControl]);
                    //After each Break need to print out Account name and sums from above.
                    // Do printing here as part of the loop, at the very end of the loop code block.
                    Console.WriteLine("--------------------------------------------------------");
                    Console.WriteLine(vAccount + "  " + vSettleMMSum + "  " + vOpenSum + "   " + vBuySum + " " +
                        vSellSum);
                    //vWriteFile.Write(vAccount + "," + vSettleMMSum + "," + vOpenSum + "," + vBuySum + "," +
                    //   vSellSum);
                    vWriteFile.WriteLine("{0},{1},{2},{3},{4}", vAccount, vSettleMMSum, vOpenSum, vBuySum, vSellSum);
                    //reset sums for next loop
                    vSettleMMSum = 0;
                    vOpenSum = 0;
                    vBuySum = 0;
                    vSellSum = 0;
                }
            }
        }
}