Visual Studio Filehelpers只读取一行数据

mwkjh3gx  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(127)

我写了一个代码,使用filehelpers读入一个csv文件,然后输出到控制台,只输出我想要的两列的值。问题是它只输出了一行的数据。我该如何解决这个问题?
这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileHelpers;

namespace prototype_using_filehelpers
{
    [DelimitedRecord("|")]
    [IgnoreEmptyLines()]
    [IgnoreFirst()]
    [IgnoreLast(2)]
    public class ManagerReportNames
    {    
        public int? MASTER_VALUE_ORDER;
        public int? MASTER_VALUE;
        public string RESORT;
        public int? CS_HEADING_COUNT_MASTER;
        public int? CS_FS_ARR_ROOMS_MASTER;
        public int? CS_FS_DEP_ROOMS_MASTER;
        public int? CS_FS_NO_ROOMS_MASTER;
        public int? CS_FS_GUESTS_MASTER;
        public int? CS_FS_TOTAL_REVENUE_MASTER;
        public int? CS_FS_ROOM_REVENUE_MASTER;
        public int? CS_FS_INVENTORY_ROOMS_MASTER;
        public int? CF_FS_PERC_OCC_ROOMS_MASTER;
        public int? CF_FS_AVG_ROOM_RATE_MASTER;
        public int? LAST_YEAR_01;
        public int? SUB_GRP_1_ORDER;
        public string SUB_GRP_1;
        public string DESCRIPTION;
        public string AMOUNT_FORMAT_TYPE;
        public string PRINT_LINE_AFTER_YN;
        public int? HEADING_1_ORDER;
        public int? HEADING_1;
        [FieldOptional]
        public string HEADING_2;
        [FieldOptional]
        [FieldConverter(ConverterKind.Decimal, ".")]
        public decimal? SUM_AMOUNT;
        [FieldOptional]
        public decimal? FORMATTED_AMOUNT;
    }

    class ManagerReport
    {
        static void Main(string[] args)
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(ManagerReportNames));
            var records = engine.ReadFile(@"C:\Users\bt\Documents\report.txt") as ManagerReportNames[];
            
            foreach (var record in records)
            {
                Console.WriteLine (record.RESORT);
                Console.WriteLine(record.FORMATTED_AMOUNT);
                Console.ReadLine();                 
            }              
        }
    }
}

输出如下:
土耳其里拉
但是,该文件有超过50行。我如何让它们全部显示
我试着用另一种方式写:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using FileHelpers;
namespace prototype_3
{
    class Program
    {

        [DelimitedRecord("|")]
        [IgnoreEmptyLines()]
        [IgnoreFirst()]
        [IgnoreLast(2)]
        public class ManagerReportNames
        {
            public int MASTER_VALUE_ORDER;
            public int? MASTER_VALUE;
            public string RESORT;
            public int? CS_HEADING_COUNT_MASTER;
            public int? CS_FS_ARR_ROOMS_MASTER;
            public int? CS_FS_DEP_ROOMS_MASTER;
            public int? CS_FS_NO_ROOMS_MASTER;
            public int? CS_FS_GUESTS_MASTER;
            public int? CS_FS_TOTAL_REVENUE_MASTER;
            public int? CS_FS_ROOM_REVENUE_MASTER;
            public int? CS_FS_INVENTORY_ROOMS_MASTER;
            public int? CF_FS_PERC_OCC_ROOMS_MASTER;
            public int? CF_FS_AVG_ROOM_RATE_MASTER;
            public int LAST_YEAR_01;
            public int SUB_GRP_1_ORDER;
            public string SUB_GRP_1;
            public string DESCRIPTION;
            public string AMOUNT_FORMAT_TYPE;
            public string PRINT_LINE_AFTER_YN;
            public int? HEADING_1_ORDER;
            public int? HEADING_1;
            [FieldOptional]
            public string HEADING_2;
            [FieldOptional]
            [FieldConverter(ConverterKind.Decimal, ".")]
            public decimal? SUM_AMOUNT;
            [FieldOptional]
            public decimal? FORMATTED_AMOUNT;
        }    

        static void Main(string[] args)
        {
            ProcessFilesCSVFiles(@"C:\Users\bt\Documents", @"C:\Users\bt\Documents\Complete");
            Console.Read();
        }

        static void ProcessFilesCSVFiles(string copyPath, string destinationPath)
        {
            // first check if path exists
            if (!Directory.Exists(copyPath))
                // doesn't exist then exit, can't copy from something that doesn't exist
                return;

            var copyPathDirectory = new DirectoryInfo(copyPath);
            // using the SearchOption.AllDirectories will search sub directories
            var copyPathCSVFiles = copyPathDirectory.GetFiles("*.txt", SearchOption.AllDirectories);
            for (var i = 0; i < copyPathCSVFiles.Length; i++)
            {
                // get the file
                var csvFile = copyPathCSVFiles[i];
                string lines = csvFile.FullName;

                // read the csv file line by line
                FileHelperEngine engine = new FileHelperEngine(typeof(ManagerReportNames));
                var records = engine.ReadFile(lines) as ManagerReportNames[];

                foreach (var record in records)
                {
                    Console.WriteLine(record.RESORT);
                    Console.WriteLine(record.FORMATTED_AMOUNT);
                }

                // move the files over to another place
                var destinationFilePath = Path.Combine(destinationPath, csvFile.Name);
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                csvFile.MoveTo(destinationFilePath);
            }
        }
    }
}

因为我想开始集成使用循环通过多个文件,然后将它们移动到一个单独的文件夹。我一直在测试它只使用一个文件和移动文件的工作。我也得到了一个以上的行。但是,输出的行开始通过文件的一半。我的代码有什么问题,导致这两个问题。

bwntbbo3

bwntbbo31#

使用您的数据范例进行测试后,第一个程式码范例可以运作,但需要您在每笔数据录之间按ENTER键。如果您变更:

foreach (var record in records)
{
    Console.WriteLine (record.RESORT);
    Console.WriteLine(record.FORMATTED_AMOUNT);
    Console.ReadLine();                 
}

foreach (var record in records)
{
    Console.WriteLine (record.RESORT);
    Console.WriteLine(record.FORMATTED_AMOUNT);
}
Console.ReadLine();

您将看到所有行。
在第二个代码示例中,您发布的数据缺少几个字段,并引发异常。如果您将LAST_YEAR_01SUB_GRP_1_ORDER设置为可空,它将适用于所提供的数据。也就是说,将这些行更改为

public int? LAST_YEAR_01;
public int? SUB_GRP_1_ORDER;

还请注意,您指定跳过最后2行,因此我手动在文件末尾添加了2个虚拟行以进行测试。
我相信它可能在文件的中途开始,因为它正在丢弃无效的行(尽管我不确定为什么没有得到异常)。
如果您在进行这些更改后仍有问题,请将完整的数据文件以代码格式发布在问题中(作为评论发布会有点混乱,需要手动调整)。

相关问题