I have an excel file (.csv) which I want to read line by line or to be more precise row by row and get it stored in a string in C# Any help is appreciated .
Asked
Active
Viewed 1.4k times
-1
-
2I suggest you do a search on SO as this has been answered many times. – Sorceri Dec 02 '13 at 15:48
-
1A CSV is just a comma-delimited text file if that makes it easier to figure out what to do. ([hint](http://stackoverflow.com/questions/15560011/how-to-read-a-csv-file-one-line-at-a-time-and-parse-out-keywords)) – D Stanley Dec 02 '13 at 15:48
-
@Sorceri sorry no idea what is SO , could you elaborate ? – fOcusWow Dec 02 '13 at 15:49
-
@fOcusWow SO = Stack Overflow (see [What do SO, SF and SU stand for?](http://meta.stackexchange.com/a/114549/167646)) – user247702 Dec 02 '13 at 15:50
-
1Pro tip: On pretty much any internet community, posting without using the search function will always result in a firestorm and down-votes where applicable. Stack Overflow even suggests related questions when you create one! – Gusdor Dec 02 '13 at 15:51
3 Answers
7
This should have your back;
var csvRows = File.ReadAllLines(@"C:\demo.csv");
It loads each line into a new entry in a string array. (Provided that the file's EOL char is \r\n
, and not just \n
)

Alexander Forbes-Reed
- 2,823
- 4
- 27
- 44
5
You can use System.IO.File.ReadAllLines()
function to Read
all the Lines
from the given filepath
.
Syntax : String [] lines= System.IO.File.ReadAllLines(String filepath)
Try This:
string path = @"C:\filename.csv";
String [] allLines=System.IO.File.ReadAllLines(path);

Sudhakar Tillapudi
- 25,935
- 5
- 37
- 67
0
Below snippet may be helpful for you. Have a look.
TextFieldParser parser = new TextFieldParser(@"D:\test.csv");
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
string test =field;
}
}
parser.Close();
include Microsoft.VisualBasic
as a reference.

Sudhakar Tillapudi
- 25,935
- 5
- 37
- 67

Kanisq
- 232
- 1
- 5