You must Sign In to post a response.
  • Category: ASP.NET

    Read the text file(row by row format) save It to csv(columns format or text file in c#?

    Hi,
    I have created console application to read the text file and save it to csv or text file format.
    I want to read the text file(row by row format) save It to csv(columns format) or text file .
    My input text file-(row by row)
    //we can ignore Personnel Clearance Pair , Credential line , need only name, clearance name, cardnumber.
    Access.text file:
    Name:,4th Floor, IT
    Personnel Clearance Pair
    Clearance name:,iT
    Credential
    Card number:,1234
    Name:,Graham, John
    Personnel Clearance Pair
    Clearance name:,Temps
    Credential
    Card number:,23489
    Name:,lori, Julie
    Personnel Clearance Pair
    Clearance name:,Access Full Time Employees //No card number row
    Credential
    Name:,Badge 43511, Visitor
    Personnel Clearance Pair//No clearance name row
    Credential
    Card number:,43511

    I want only name and clearance name and card number in csv file(out put file).

    Please help me
    Thanks inadvance
  • #769721
    please try to check with below logic

    try
    {
    StreamReader sr = new StreamReader("C:\\Access.txt");
    string csv = string.Empty;
    string csv1 = string.Empty

    line = sr.ReadLine();
    line = line .split(":,");

    if(line.length>1){
    csv += line[0] + ','; // Adding header columns into string
    csv1 += line[1]; // Adding the values against each header
    }

    while (line != null) //Continue to read until you reach end of file
    {
    line = sr.ReadLine();
    }
    csv += "\r\n"; //Add new line.
    csv + = csv + csv1 + "\r\n";
    //close the file
    sr.Close();
    Console.ReadLine();

    //Download the CSV file.
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=AccessCSVFile.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";
    Response.Output.Write(csv);
    Response.Flush();
    Response.End();

    }
    catch(Exception e){}
    finally {}

    hope this will help you.

    Thanks!
    B.Ramana Reddy

  • #769725
    Hi,

    Thanks for your reply ,I have used below code

    try
    {
    System.IO.TextWriter Summarytw = System.IO.File.CreateText(sOutputFilePath + sOutFileName_DST);

    //Here I am adding column names to csv file(output file).
    string line1 = string.Empty;
    string strColumnname = string.Empty;//{"Name","Clearance name","Card number"};
    strColumnname = "Name,Clearance name,Card number";
    string[] arr = strColumnname.Split(',');
    int a = arr.Length;

    for (int y = 0; y < arr.Length; y++)
    {
    line += arr[y].ToString() + ",";
    }
    Summarytw.WriteLine(line);

    //here I am reading the text file(input file)
    using (System.IO.StreamReader sr = new System.IO.StreamReader(sInputPath + sInFile))
    {

    while ((line = sr.ReadLine()) != null)
    {
    //I need to save only name, clearance name, card number values to the csv file.
    if ((line.Contains("Name")) || (line.Contains("Clearance name")) || (line.Contains("Card number")))
    {
    string s = line.ToString();
    string pattern = ":,";
    string strdelimiter = ",";
    string[] substrings = Regex.Split(line, pattern);
    for (int i = 1; i < substrings.Length; i++)
    {
    string value = substrings[1].ToString();
    if (i == substrings.Length)
    {
    strconcat += " " + value + " ";
    }
    else
    {
    strconcat += "\" " + value + "\"";
    strconcat += strdelimiter;
    }

    }


    }


    if (line.StartsWith("Card number"))
    {
    strconcat += Environment.NewLine;
    }


    }

    Summarytw.WriteLine(strconcat);
    }
    }

    if the data format is like below it is working fine
    Name:,4th Floor, IT
    Personnel Clearance Pair
    Clearance name:,iT
    Credential
    Card number:,1234

    Name:,Graham, John
    Personnel Clearance Pair
    Clearance name:,Temps
    Credential
    Card number:,23489
    suppose the data is like below it is not working fine.
    //if there is no card number line then next record is placing in that column
    Name:,lori, Julie
    Personnel Clearance Pair
    Clearance name:,Access Full Time Employees //No card number row
    Credential

    //if clearance name line is missing then card number is saving into the clearance name field.
    Name:,Badge 43511, Visitor
    Personnel Clearance Pair//No clearance name row
    Credential
    Card number:,43511

    what is wrong in my code , can you please let me know.

    ACCESS.CSV:
    Name Clearance name card number
    4th Floor, IT iT 1234
    Graham, John Temps 23489
    lori, Julie Access (if no card number it is placing with next record details)
    Badge 56892 56892 (if clearance name is empty then card number Is saving into clearance name field in csv file.)


  • Sign In to post your comments