0

我是编程新手,我想将 csv 文件读入表单上的文本框中。现在我正在将文件读入数据表,并认为我会将其读入 texboxes,但我不确定我是否正确。有没有更简单的方法来做到这一点?这是我到目前为止所拥有的:

protected void getftp()
{

   //create Data table to temporary storage
   var myTable = new DataTable();

   //add columns
   myTable.Columns.Add("Start_date");
   myTable.Columns.Add("End_date");
   //...snip...
   myTable.Columns.Add("Comments");

   //The 'using' command close connection when it is done
   using (var reader = new StreamReader(File.OpenRead(@"C:\ftp\inbox\test.csv")))
   {
      while (!reader.EndOfStream)
      {
         //read in one line of the file
         string line = reader.ReadLine();

         //create an array of strings from each value in the current line
         string[] values = line.Split(',');

         //add the array as a row in the DataTable
         myTable.Rows.Add(values);
      }
   }
}
4

2 回答 2

0

请参阅以下链接。它显示了使用 CSV 格式http://www.codeproject.com/Articles/30705/C-CSV-Import-Export的基础知识

于 2013-01-24T15:50:58.267 回答
0

这是另一个关于在 .NET 中读取 CSV 文件的 SO 问题:

在 .NET 中读取 CSV 文件?

这个特定的答案引用了 2 个 CSV 阅读器。您可以使用这些读取 CSV 文件,然后在您的 Windows 窗体(或 Web 窗体或 ASPX 或 Razor 页面,您没有指明您的前端)上的文本框中设置值。

我建议重用这些项目之一,而不是重新发明轮子并滚动您自己的 CSV 解析器。用 C# 编写很容易,但是读取/解析 CSV 文件是一个已经解决了很多次的问题。

于 2013-01-24T15:51:37.773 回答