■■■

2016年4月2日土曜日

CSVファイルを配列に格納する方法

CSVファイルを配列に格納する方法
VB.NET
Public Shared Function CsvToArrayList1(ByVal csvText As String) _
As System.Collections.ArrayList
Dim csvRecords As New System.Collections.ArrayList

csvText = csvText.Trim( _
New Char() {ControlChars.Cr, ControlChars.Lf})

Dim regLine As New System.Text.RegularExpressions.Regex( _
"^.*(?:\n|$)", _
System.Text.RegularExpressions.RegexOptions.Multiline)

Dim regCsv As New System.Text.RegularExpressions.Regex( _
"\s*(""(?:[^""]|"""")*""|[^,]*)\s*,", _
System.Text.RegularExpressions.RegexOptions.None)

Dim mLine As System.Text.RegularExpressions.Match = _
regLine.Match(csvText)
While mLine.Success
Dim line As String = mLine.Value
While CountString(line, """") Mod 2 = 1
mLine = mLine.NextMatch()
If Not mLine.Success Then
Throw New
ApplicationException("不正なCSV")
End If
line += mLine.Value
End While
line = line.TrimEnd( _
New Char() {ControlChars.Cr, ControlChars.Lf})
line += ","

Dim csvFields As New System.Collections.ArrayList
Dim m As System.Text.RegularExpressions.Match = _
regCsv.Match(line)
While m.Success
Dim field As String = m.Groups(1).Value
field = field.Trim()
If field.StartsWith("""") And field.EndsWith("""") Then
field = field.Substring(1, field.Length - 2)
field = field.Replace("""""", """")
End If
csvFields.Add(field)
m = m.NextMatch()
End While

csvFields.TrimToSize()
csvRecords.Add(csvFields)

mLine = mLine.NextMatch()
End While

csvRecords.TrimToSize()
Return csvRecords
End Function

Public Shared Function CountString( _
ByVal strInput As String, _
ByVal strFind As String) As Integer
Dim
foundCount As Integer = 0
Dim sPos As Integer = strInput.IndexOf(strFind)
While sPos > -1
foundCount += 1
sPos = strInput.IndexOf(strFind, sPos + 1)
End While

Return
foundCount
End Function
C#
public static System.Collections.ArrayList CsvToArrayList1(string csvText)
{
System.Collections.ArrayList csvRecords =
new System.Collections.ArrayList();

csvText = csvText.Trim(new char[] {'\r', '\n'});

System.Text.RegularExpressions.Regex regLine =
new System.Text.RegularExpressions.Regex(
"^.*(?:\\n|$)",
System.Text.RegularExpressions.RegexOptions.Multiline);

System.Text.RegularExpressions.Regex regCsv =
new System.Text.RegularExpressions.Regex(
"\\s*(\"(?:[^\"]|\"\")*\"|[^,]*)\\s*,",
System.Text.RegularExpressions.RegexOptions.None);

System.Text.RegularExpressions.Match mLine = regLine.Match(csvText);
while (mLine.Success)
{
string line = mLine.Value;
while ((CountString(line, "\"") % 2) == 1)
{
mLine = mLine.NextMatch();
if (!mLine.Success)
{
throw new ApplicationException("不正なCSV");
}
line += mLine.Value;
}
line = line.TrimEnd(new char[] {'\r', '\n'});
line += ",";

System.Collections.ArrayList csvFields =
new System.Collections.ArrayList();
System.Text.RegularExpressions.Match m = regCsv.Match(line);
while (m.Success)
{
string field = m.Groups[1].Value;
field = field.Trim();
if (field.StartsWith("\"") && field.EndsWith("\""))
{
field = field.Substring(1, field.Length - 2);
field = field.Replace("\"\"", "\"");
}
csvFields.Add(field);
m = m.NextMatch();
}

csvFields.TrimToSize();
csvRecords.Add(csvFields);

mLine = mLine.NextMatch();
}

csvRecords.TrimToSize();
return csvRecords;
}

public static int CountString(string strInput, string strFind)
{
int foundCount = 0;
int sPos = strInput.IndexOf(strFind);
while (sPos > -1)
{
foundCount++;
sPos = strInput.IndexOf(strFind, sPos + 1);
}

return foundCount;
}
CSVファイルの文字列を解析して配列に格納する方法
VB.NET
Public Shared Function CsvToArrayList2(ByVal csvText As String) _
As System.Collections.ArrayList
csvText = csvText.Trim( _
New Char() {ControlChars.Cr, ControlChars.Lf})

Dim csvRecords As New System.Collections.ArrayList
Dim csvFields As New System.Collections.ArrayList

Dim csvTextLength As Integer = csvText.Length
Dim startPos As Integer = 0
Dim endPos As Integer = 0
Dim field As String = ""

While True
While startPos < csvTextLength _
AndAlso (csvText.Chars(startPos) = " "c _
OrElse csvText.Chars(startPos) = ControlChars.Tab)
startPos += 1
End While

If startPos < csvTextLength _
AndAlso csvText.Chars(startPos) = ControlChars.Quote Then
endPos = startPos
While True
endPos = csvText.IndexOf(ControlChars.Quote, endPos + 1)
If endPos < 0 Then
Throw New
ApplicationException("""が不正")
End If
If endPos + 1 = csvTextLength OrElse _
csvText.Chars((endPos + 1)) <> ControlChars.Quote Then
Exit While
End If

endPos += 1
End While

field = csvText.Substring(startPos, endPos - startPos + 1)
field = field.Substring(1, field.Length - 2). _
Replace("""""", """")

endPos += 1
While endPos < csvTextLength AndAlso _
csvText.Chars(endPos) <> ","c AndAlso _
csvText.Chars(endPos) <> ControlChars.Lf
endPos += 1
End While
Else

endPos = startPos
While endPos < csvTextLength AndAlso _
csvText.Chars(endPos) <> ","c AndAlso _
csvText.Chars(endPos) <> ControlChars.Lf
endPos += 1
End While

field = csvText.Substring(startPos, endPos - startPos)
field = field.TrimEnd()
End If

csvFields.Add(field)

If endPos >= csvTextLength OrElse _
csvText.Chars(endPos) = ControlChars.Lf Then
csvFields.TrimToSize()
csvRecords.Add(csvFields)
csvFields = New System.Collections.ArrayList( _
csvFields.Count)

If endPos >= csvTextLength Then
Exit While
End If
End If


startPos = endPos + 1
End While

csvRecords.TrimToSize()
Return csvRecords
End Function
C#
public static System.Collections.ArrayList CsvToArrayList2(string csvText)
{
csvText = csvText.Trim(new char[] {'\r', '\n'});

System.Collections.ArrayList csvRecords =
new System.Collections.ArrayList();
System.Collections.ArrayList csvFields =
new System.Collections.ArrayList();

int csvTextLength = csvText.Length;
int startPos = 0, endPos = 0;
string field = "";

while (true)
{
while (startPos < csvTextLength &&
(csvText[startPos] == ' ' || csvText[startPos] == '\t'))
{
startPos++;
}

if (startPos < csvTextLength && csvText[startPos] == '"')
{
endPos = startPos;
while (true)
{
endPos = csvText.IndexOf('"', endPos + 1);
if (endPos < 0)
{
throw new ApplicationException("\"が不正");
}
if (endPos + 1 == csvTextLength || csvText[endPos + 1] != '"')
{
break;
}
endPos++;
}

field = csvText.Substring(startPos, endPos - startPos + 1);
field = field.Substring(1, field.Length - 2).Replace("\"\"", "\"");

endPos++;
while (endPos < csvTextLength &&
csvText[endPos] != ',' && csvText[endPos] != '\n')
{
endPos++;
}
}
else
{
endPos = startPos;
while (endPos < csvTextLength &&
csvText[endPos] != ',' && csvText[endPos] != '\n')
{
endPos++;
}

field = csvText.Substring(startPos, endPos - startPos);
field = field.TrimEnd();
}

csvFields.Add(field);

if (endPos >= csvTextLength || csvText[endPos] == '\n')
{
csvFields.TrimToSize();
csvRecords.Add(csvFields);
csvFields = new System.Collections.ArrayList(
csvFields.Count);

if (endPos >= csvTextLength)
{
break;
}
}

startPos = endPos + 1;
}

csvRecords.TrimToSize();
return csvRecords;
}
TextFieldParserでCSVファイルを解析する方法
VB.NET
Dim csvRecords As New System.Collections.ArrayList()

Dim csvFileName As String = "C:\create.csv"

Dim tfp As New FileIO.TextFieldParser(csvFileName, _
System.Text.Encoding.GetEncoding(932))
tfp.TextFieldType = FileIO.FieldType.Delimited
tfp.Delimiters = New String() {","}
tfp.HasFieldsEnclosedInQuotes = True
tfp.TrimWhiteSpace = True

While Not
tfp.EndOfData
Dim fields As String() = tfp.ReadFields()
csvRecords.Add(fields)
End While

tfp.Close()
C#
System.Collections.ArrayList csvRecords =
new System.Collections.ArrayList();

string csvFileName = "C:\\new.csv";

Microsoft.VisualBasic.FileIO.TextFieldParser tfp =
new Microsoft.VisualBasic.FileIO.TextFieldParser(
csvFileName,
System.Text.Encoding.GetEncoding(932));
tfp.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
tfp.Delimiters = new string[] { "," };
tfp.HasFieldsEnclosedInQuotes = true;
tfp.TrimWhiteSpace = true;

while (!tfp.EndOfData)
{
string[] fields = tfp.ReadFields();
csvRecords.Add(fields);
}

tfp.Close();






■■■