【问题标题】:C# Lumenworks Csvreader- Display Error in Message Box while reading a csv file with some Empty/Null fieldsC# Lumenworks Csvreader-在读取带有一些空/空字段的 csv 文件时在消息框中显示错误
【发布时间】:2014-11-01 21:10:13
【问题描述】:

我有此代码来读取 csv 文件并将字段存储在“记录”类的 objects_Records 列表中。

private String text2,text3,text4,text5,text6;
    private double text7,text8,text9;
    private int text1;
    private int count = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "CSV files (*.csv)|*.csv";  // Show only .csv files among all the different files        
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            String file = openFileDialog1.FileName;
            try
            {
                textBoxFilePath.Text = file;
                using (CsvReader csv = new CsvReader(new StreamReader(file), true))
                {
                    int fieldCount = csv.FieldCount;
                    string[] headers = csv.GetFieldHeaders();
                    while (csv.ReadNextRecord())
                    {
                        count += 1;
                            for (int i = 0; i < fieldCount; i++)
                            {                         
                                switch (headers[i].ToLower())
                                {
                                    case "plot":
                                        text1 = int.Parse(csv[i]);
                                        break;
                                    case "local name":
                                        text2 = csv[i];
                                        break;
                                    case "botanical name":
                                        text3 = csv[i];
                                        break;
                                    case "genera":
                                        text4 = csv[i];
                                        break;
                                    case "species":
                                        text5 = csv[i];
                                        break;
                                    case "family":
                                        text6 = csv[i];
                                        break;
                                    case "dbh":
                                        text7 = Double.Parse(csv[i]);
                                        break;
                                    case "ba(sqm)":
                                        text8 = Double.Parse(csv[i]);
                                        break;
                                    case "ba":
                                        text8 = Double.Parse(csv[i]);
                                        break;
                                    case "height":
                                        text9 = Double.Parse(csv[i]);
                                        break;
                                    default:
                                        MessageBox.Show("Please check the column headers of the fields once!");
                                        continue;
                                    }
                            }
                            object_Records.Add(new Records(text1, text2, text3, text4, text5, text6, text7, text8, text9));
                        }
                    }
                    textBox1.Text = count.ToString(); 
            }
            catch (IOException)
            {
            }
        }
    }

对于没有“NULL/EMPTY”字段的 csv 文件,我的代码运行良好。遇到包含“NULL”字段的 csv 文件时会抛出异常。

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

附加信息:输入字符串的格式不正确。

请帮我解决可以处理该异常的异常处理机制。

【问题讨论】:

  • 您需要向我们展示一个示例文件。哪些单元格是空的?如果是数字字段之一,请将Double.Parse 更改为double.TryParse() 并处理错误。
  • 是的。当我对数值使用 double.TryParse() 方法时,会处理异常。但是对于字符串值没有必要使用这种方法。我想知道为什么?

标签: c# winforms csv formatexception


【解决方案1】:

按照 cmets 中的建议,您是否尝试过 switch 声明:

switch (headers[i].ToLower())
{
    case "plot":
        int.TryParse(csv[i], out text1);
        break;
    case "local name":
        text2 = csv[i];
        break;
    case "botanical name":
        text3 = csv[i];
        break;
    case "genera":
        text4 = csv[i];
        break;
    case "species":
        text5 = csv[i];
        break;
    case "family":
        text6 = csv[i];
        break;
    case "dbh":
        Double.TryParse(csv[i], out text7);
        break;
    case "ba(sqm)":
        Double.TryParse(csv[i], text8);
        break;
    case "ba":
        Double.TryParse(csv[i], out text8);
        break;
    case "height":
        Double.TryParse(csv[i], out text9);
        break;
    default:
        MessageBox.Show("Please check the column headers of the fields once!");
        continue;
}

如果您尝试过但它不起作用,您能否深入了解异常并查看它究竟是在哪里抛出的?

【讨论】:

  • 非常感谢@Matt Ko。有效。有没有一种方法可以重载 TryParse 方法以向用户显示“在使用应用程序之前必须在文件中更正一些 NULL 值”?
  • 当然,TryParse 返回一个您可以使用的boolean。如果某些事情不成功,您可以在默认路径中显示一条消息。在我的示例中,我根本没有使用返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2016-01-20
相关资源
最近更新 更多