Click here to Skip to main content
15,127,310 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Experts,



here is the file structure what I am getting:-

FileName ,Docname, Repeat1, Repeat2(these are the header of the file)
tronox, tronoxdoc, one,
tronox, tronoxdoc, two,
tronox, tronoxdoc, three,

expected output is:-

FileName ,Docname, Repeat1, Repeat2(these are the header of the file)
tronox, tronoxdoc, one, five
tronox, tronoxdoc, two, five
tronox, tronoxdoc, three,

I am not getting how to achieve this.

below is my code:-

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Export();

        }
        public void Export()
        {
            string ExportFileName = @"F:\Mukesh\Tronox\try.csv";


            //check if file exists
            if (!File.Exists(ExportFileName))
            {
                //file does not exist, add the header line
                string header = @"FileName," + "DocumentName," + "Repeat1," + "Repeat2," +

                    Environment.NewLine;
                File.WriteAllText(ExportFileName, header);
            }
            string repgr1 = string.Empty;

            string filename = "tronox";
            string docname = "tronoxdoc";
            string Print = filename + "," + docname+",";
            string[] rep1 = {"one","two","three" };
            string[] rep2 = {"five","five" };
            string row = string.Empty;

            if(rep1.Count()>0)
            {
                for (int i = 0; i < rep1.Count(); i++ )
                {
                    repgr1 = rep1[i];

                    row = Print + repgr1 + "," + Environment.NewLine;
                    File.AppendAllText(ExportFileName, row);
                }
            }

            else
            {
            File.AppendAllText(ExportFileName, Print);
            }
        }
    }
    }





Please anyone help me..
Thanks in Advance
Posted
Updated 15-Apr-15 2:07am
v3
Comments
Maciej Los 15-Apr-15 7:16am
   
Sorry, but your question is unclear...

1 solution

Try this:
C#
string ExportFileName = @"F:\Mukesh\Tronox\try.csv";

//check if file exists
if (!File.Exists(ExportFileName))
{
    //file does not exist, add the header line
    string header = @"FileName," + "DocumentName," + "Repeat1," + "Repeat2," +  Environment.NewLine;
    File.WriteAllText(ExportFileName, header);
}

string filename = "tronox";
string docname = "tronoxdoc";
string Print = filename + "," + docname + ",";
string[] rep1 = { "one", "two", "three" };
string[] rep2 = { "five", "five" };

int repCount = Math.Max(rep1.Length, rep2.Length);
if (repCount > 0)
{
    StringBuilder text = new StringBuilder();

    for (int i = 0; i < repCount; i++)
    {
        text.Append(Print);

        if (i < rep1.Length)
            text.Append(rep1[i]);

        text.Append(",");

        if (i < rep2.Length)
            text.Append(rep2[i]);

        text.AppendLine();
    }

    File.WriteAllText(ExportFileName, text.ToString());
}
else
{
    File.AppendAllText(ExportFileName, Print);
}

In short I'm using StringBuilder to collect all the necessery text and I'm appending the rep1 and rep2 values if there is any for the current row.
   
v2
Comments
Mukesh Pr@sad 15-Apr-15 8:22am
   
HI Mario, Thanks for the solution its working fine, only problem is I am not getting the header of the file.
Mario Z 15-Apr-15 8:32am
   
Hi, I haven't change the logic of your header writing, it remained the same.
I'm not sure if you are aware of how you made it... I hope you are, but just in case you aren't the header text is only written in case the file does not exist:
if (!File.Exists(ExportFileName))
I presume that what happens is that you have an empty "try.csv" file and because of that the above if condition is false so the header is not added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900