CSVReader.cs
上传用户:zijide88
上传日期:2022-07-10
资源大小:77k
文件大小:2k
源码类别:

数据库系统

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6.     class CSVReader
  7.     {
  8.         public CSVReader() { 
  9.         
  10.         
  11.         }
  12.         public ItemSet Read(string fileName) {
  13.             ItemSet rowSet = new ItemSet();
  14.             ItemSet colSet = null;
  15.             string col="";
  16.             string[] head=null;
  17.             if (File.Exists(fileName))
  18.             {
  19.                 // Create a file to write to.
  20.                 StreamReader sr = new StreamReader(File.OpenRead(fileName), Encoding.Default, true);
  21.                 string row = "";
  22.                 int k = 0;
  23.                 while(!sr.EndOfStream){
  24.                     k++;
  25.                     row=sr.ReadLine();
  26.                     
  27.                     string[] cols=row.Split(",".ToCharArray());
  28.                     
  29.                     if (k == 1)
  30.                     {
  31.                         head=cols;
  32.                     }
  33.                     else {
  34.                         colSet = new ItemSet();
  35.                         for(int i=1;i<cols.Length+1;i++)
  36.                         {
  37.                             col=cols[i-1];
  38.                             if (col.Equals("1"))
  39.                             {
  40.                                 colSet.Add(new DataItem(i,head[i-1]));
  41.                             }
  42.                         }
  43.                         rowSet.Add(colSet);
  44.                     }
  45.                 }
  46.               
  47.              
  48.                
  49.                 sr.Close();
  50.                
  51.             }
  52.             return rowSet;
  53.         }
  54.     }
  55.