从.csv文件导入数据

时间:2015-06-05 03:53:50

标签: vb.net csv

此应用程序的工作原理:
用户输入商店编号 点击GO后,它将从.csv文件中提取地址/城市/州/邮编

.css文件示例:

在Excel中

StoreNumber Address                           City          State   ZipCode
295         4425 14TH ST W                    BRADENTON     FL      34207
296         4942 S TAMIAMI TRL                SARASOTA      FL      34231
297         10261 RIVER MARSH DR UNIT 143     JACKSONVILLE  FL      32246

在记事本中

StoreNumber,Address,City,State,ZipCode,Telephone
295,4425 14TH ST W,BRADENTON,FL,34207,
296,4942 S TAMIAMI TRL,SARASOTA,FL,34231,
297,10261 RIVER MARSH DR UNIT 143,JACKSONVILLE,FL,32246,

我以前尝过这个,但是感到沮丧 我想在这次帮助下重新开启这项任务!

我想我已经包含了所有必需的信息 如果我遗失任何东西,请告诉我。

2 个答案:

答案 0 :(得分:0)

您的问题是询问如何阅读CSV文件? 下面是如何读取CSV文件的示例代码(C#)希望它能帮助您^^

private const string COMMENTSTRING = "@#@#";//Comment string

        /// <summary>
        /// Use to import CSV item Info
        /// </summary>
        /// <param name="FileLocation">Import From</param>
        /// <returns>CSVItemInfo Object</returns>
        public System.Collections.ArrayList Import(string FileLocation)
        {
            ArrayList arrData = new ArrayList();;

            FileStream fs = new FileStream(FileLocation, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(fs);
            reader.BaseStream.Seek(0, SeekOrigin.Begin);

            try
            {
                //Read all line
                while (reader.Peek() >= 0)
                {
                    string val = reader.ReadLine();

                    if (val.Trim().Contains(COMMENTSTRING)) //Skip read comment
                    {
                        continue;
                    }

                    string[] retVal = val.Split(new string[] { "," }, StringSplitOptions.None);

                    string StoreNumber = retVal[0];
                    string Address = retVal[1];
                    //ETC.......

                }
            }
            catch (Exception ex)
            {
                //Log exception
                throw ex;
            }
            finally
            {
                reader.Close();
                fs.Close();
            }


            return arrData;
        }

答案 1 :(得分:0)

嗯,这是我的镜头。

Using sr As New StreamReader("your file path")
  Dim data = sr.ReadToEnd.Split(Environment.NewLine).
                FirstOrDefault(Function(line) line.Split(","c)(0) = "1234")
  'data now contains the comma separated data
  If Not data Is Nothing
     'can be nothing due to the `FirstOrDefualt` function
  End If
End Using