如何在 C# 中向 CSV 添加附加列

问题描述 投票:0回答:1

我创建了一个小型 C# 程序来获取 CSV 文件、修改数据、拆分一些行项目以及添加其他数据列。最后一部分在运行时会引入错误。

Error: Header with name 'Payee_Country'[0] was not found. Header with name 'Payer_Address1'[0] was not found. Header with name 'Payer_Address2'[0] was not found. Header with name 'Payer_City'[0] was not found. Header with name 'Payer_State' [0] was not found. Header with name 'Payer_Zip'[0] was not found. Header with name 'Payer_Country'[0] was not found.

下面的代码是我到目前为止所拥有的,例如上面使用的值并不预先存在于 CSV 输入文件中,我不确定如何让它工作,因为我已经尝试过映射,但这只是引入了更多错误。

    public class CsvRecord
    {
        public string GLAccountName { get; set; }
        public string BankRoutingNumber { get; set; }
        public string BankAccountNumber { get; set; }
        public DateOnly FileDate { get; set; }
        public string Amount { get; set; }
        public string InvoiceNumbers { get; set; }
        public string InvoiceDates { get; set; }
        public string InvoiceAmounts { get; set; }
        public string InvoiceTotals { get; set; }
        public string InvoiceDescriptions { get; set; }
        public string CheckNumber { get; set; }
        public string VendorID { get; set; }
        public string VendorName { get; set; }
        public string VendorAddressLine1 { get; set; }
        public string VendorAddressLine2 { get; set; }
        public string VendorCity { get; set; }
        public string VendorState { get; set; }
        public string VendorZip { get; set; }
        public string Payee_Country { get; set; }
        public string VendorEmail { get; set; }
        public string Payer_Address1 { get; set; }
        public string Payer_Address2 { get; set; }
        public string Payer_City { get; set; }
        public string Payer_State { get; set; }
        public string Payer_Zip { get; set; }
        public string Payer_Country { get; set; }
    }




    private void SelectFileButton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == true)
        {
            FilePathTextBox.Text = openFileDialog.FileName;
        }
    }

    private void RunButton_Click(object sender, RoutedEventArgs e)
    {

        if (!string.IsNullOrEmpty(FilePathTextBox.Text))
        {
            try
            {
                // Read the CSV file
                List<CsvRecord> records;

                using (var reader = new StreamReader(FilePathTextBox.Text))
                using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
                {
                    records = csv.GetRecords<CsvRecord>().ToList();
                }

                // Create a new list to store the modified records
                List<CsvRecord> modifiedRecords = new List<CsvRecord>();

                // Split records containing pipes

                foreach (var record in records)
                {

                    var invoiceAmountsArray = record.InvoiceAmounts.Split('|');
                    var invoiceTotalsArray = record.InvoiceTotals.Split('|');
                    var invoiceNumbersArray = record.InvoiceNumbers.Split('|');
                    var invoiceDatesArray = record.InvoiceDates.Split('|');
                    var invoiceDescriptionsArray = record.InvoiceDescriptions.Split('|');

                    // Ensure the arrays have the same length
                    int itemCount = Math.Min(invoiceAmountsArray.Length, invoiceTotalsArray.Length);

                    // Create separate line items for each amount
                    for (int i = 0; i < itemCount; i++)
                    {
                        var newRecord = new CsvRecord
                        {
                            GLAccountName = record.GLAccountName.Trim(' ', '"'),
                            Payer_Address1 = "xxx",
                            Payer_Address2 = "xxx",
                            Payer_City = "xxx",
                            Payer_State = "xxx",
                            Payer_Zip = "xxx",
                            Payer_Country = "US",
                            BankRoutingNumber = record.BankRoutingNumber.Trim(' ', '"'),
                            BankAccountNumber = record.BankAccountNumber.Trim(' ', '"'),
                            FileDate = record.FileDate,
                            Amount = record.Amount,
                            InvoiceNumbers = invoiceNumbersArray[i].Trim(' ', '"'),
                            InvoiceDates = invoiceDatesArray[i].Trim(' ', '"'),
                            InvoiceAmounts = invoiceAmountsArray[i].Trim(' ', '"'),
                            InvoiceTotals = invoiceTotalsArray[i].Trim(' ', '"'),
                            InvoiceDescriptions = invoiceDescriptionsArray[i].Trim(' ', '"'),
                            CheckNumber = record.CheckNumber.Trim(' ', '"'),
                            VendorID = record.VendorID.Trim(' ', '"'),
                            VendorName = record.VendorName.Trim(' ', '"'),
                            VendorAddressLine1 = record.VendorAddressLine1.Trim(' ', '"'),
                            VendorAddressLine2 = record.VendorAddressLine2.Trim(' ', '"'),
                            VendorCity = record.VendorCity.Trim(' ', '"'),
                            VendorState = record.VendorState.Trim(' ', '"'),
                            VendorZip = record.VendorZip.Trim(' ', '"'),
                            Payee_Country = "US",
                            VendorEmail = record.VendorEmail.Trim(' ', '"')
                        };

                        // Add the new record to the list
                        modifiedRecords.Add(newRecord);
                    }
                }

                // Perform your edits on the 'records' list here

                foreach (var record in modifiedRecords)
                {
                    if (!string.IsNullOrWhiteSpace(record.CheckNumber) && !record.CheckNumber.All(char.IsDigit))
                    {
                        record.CheckNumber = "";
                    }

                    record.BankRoutingNumber = record.BankRoutingNumber.PadLeft(9, '0');
                    record.BankAccountNumber = record.BankAccountNumber.PadLeft(13, '0');
                    record.CheckNumber = record.CheckNumber.PadLeft(10, '0');

                    if (!string.IsNullOrEmpty(record.VendorID) && record.VendorID.Length > 17)
                    {

                        record.VendorID = record.VendorID.Substring(0, 17);
                    }

                    decimal tempValue = decimal.Parse(record.Amount);
                    record.Amount = tempValue.ToString("F2");

                    tempValue = decimal.Parse(record.InvoiceTotals);
                    record.InvoiceTotals = tempValue.ToString("F2");

                    tempValue = decimal.Parse(record.InvoiceAmounts);
                    record.InvoiceAmounts = tempValue.ToString("F2");

                    if (!string.IsNullOrEmpty(record.InvoiceNumbers) && record.InvoiceNumbers.Length > 30)
                    {

                        record.InvoiceNumbers = record.InvoiceNumbers.Substring(0, 30);
                    }

                    record.FileDate = DateOnly.ParseExact(record.FileDate.ToString("MM/dd/yyyy"), "MM/dd/yyyy", CultureInfo.InvariantCulture);

                    DateTime tempDate = DateTime.Parse(record.InvoiceDates);
                    record.InvoiceDates = tempDate.ToString("MM/dd/yyyy");
                    record.InvoiceDescriptions = record.InvoiceDescriptions.Trim(' ', '"');

                    if (!string.IsNullOrEmpty(record.InvoiceDescriptions) && record.InvoiceDescriptions.Length > 50)
                    {

                        record.InvoiceDescriptions = record.InvoiceDescriptions.Substring(0, 50);
                    }

                    if (!string.IsNullOrEmpty(record.GLAccountName) && record.GLAccountName.Length > 35)
                    {

                        record.GLAccountName = record.GLAccountName.Substring(0, 35);
                    }
                }

                // Save the modified CSV file to the desktop
                var desktopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
                var outputPath = System.IO.Path.Combine(desktopPath, "Fixed_IP.csv");

                using (var writer = new StreamWriter(outputPath))
                using (var csv = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture)
                { HasHeaderRecord = false }))
                {
                    csv.WriteRecords(modifiedRecords);
                }

                MessageBox.Show($"File converted and saved to: {outputPath}", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        else
        {
            MessageBox.Show("Please select a file first.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
c# csv csvhelper
1个回答
0
投票

你是对的。

这似乎是由原始 CSV 文件不包含这些标题引起的

当您使用

csv.GetRecords<CsvRecord>().ToList()
时,CsvHelper 期望这些标头位于原始文件中,但您可以通过将这些属性标记为可选来修复它。

直接上课

public class CsvRecord
{
   public string GLAccountName { get; set; }
   ...
   [Optional]
   public string Payee_Country { get; set; }
   ...
}

或通过

ClassMap

Map(m => m.Payee_Country).Optional();
© www.soinside.com 2019 - 2024. All rights reserved.