-
-
Save kiichi54321/5298464 to your computer and use it in GitHub Desktop.
CSVFileを読むクラス Silverlight対応
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CSVFile | |
{ | |
StreamReader steram; | |
public CSVFile(StreamReader stream) | |
{ | |
this.steram = stream; | |
} | |
public CSVFile(string fileName) | |
{ | |
this.steram = new StreamReader(fileName); | |
} | |
Dictionary<string, int> headerDic = new Dictionary<string, int>(); | |
internal int GetIndex(string column) | |
{ | |
if (headerDic.ContainsKey(column)) | |
{ | |
return headerDic[column]; | |
} | |
else | |
{ | |
return -1; | |
} | |
} | |
public static IEnumerable<CSVLine> ReadLines(StreamReader stream) | |
{ | |
CSVFile r = new CSVFile(stream); | |
return r.Lines; | |
} | |
public IEnumerable<CSVLine> Lines | |
{ | |
get | |
{ | |
StreamReader sr = steram; | |
string first = sr.ReadLine(); | |
int i =0; | |
headerDic.Clear(); | |
foreach (var item in first.Split(',')) | |
{ | |
headerDic.Add(item, i); | |
i++; | |
} | |
while (sr.Peek()>-1) | |
{ | |
yield return new CSVLine(sr.ReadLine(), this); | |
} | |
} | |
} | |
} | |
public class CSVLine | |
{ | |
string line; | |
string[] data; | |
CSVFile csvRead; | |
public CSVLine(string line,CSVFile read) | |
{ | |
this.line = line; | |
this.csvRead = read; | |
this.data = line.Split(','); | |
} | |
public string GetValue(int index) | |
{ | |
if (index > -1) | |
{ | |
if (data.Length > index) | |
{ | |
return data[index]; | |
} | |
else | |
{ | |
throw new Exception("列をはみ出しました"); | |
} | |
} | |
throw new Exception("0以上の数値を入れてください"); | |
} | |
public string GetValue(string column) | |
{ | |
var i = csvRead.GetIndex(column); | |
if (i > -1) | |
{ | |
if (data.Length > i) | |
{ | |
return data[i]; | |
} | |
else | |
{ | |
throw new Exception("列をはみ出しました"); | |
} | |
} | |
throw new Exception("存在しない列名です"); | |
} | |
public int GetIntValue(string column) | |
{ | |
return int.Parse(GetValue(column)); | |
} | |
public double GetDoubleValue(string column) | |
{ | |
return double.Parse(GetValue(column)); | |
} | |
public int GetIntValue(int index) | |
{ | |
return int.Parse(GetValue(index)); | |
} | |
public double GetDoubleValue(int index) | |
{ | |
return double.Parse(GetValue(index)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ボタンクリック次の処理の例 | |
private void button1_Click(object sender, RoutedEventArgs e) | |
{ | |
OpenFileDialog openFileDialog1 = new OpenFileDialog(); | |
// Set filter options and filter index. | |
openFileDialog1.Filter = "CSV Files (.csv)|*.csv|All Files (*.*)|*.*"; | |
openFileDialog1.FilterIndex = 1; | |
if (openFileDialog1.ShowDialog() == true) | |
{ | |
foreach (var item in CSVFile.ReadLines(openFileDialog1.File.OpenText())) | |
{ | |
int id = item.GetIntValue("id"); | |
string name = item.GetValue("Name"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment