开发者

how to format data in a text file

开发者 https://www.devze.com 2023-01-10 02:42 出处:网络
ihave an string builder whereitconatinsemail id( itconatins thousandsof email id) StringBuilder sb = new StringBuilder();

ihave an string builder where it conatins email id( it conatins thousands of email id)

StringBuilder sb = new StringBuilder();
foreach (DataRow dr2 in dtResult.Rows)
{
    strtxt = dr2[strMailID].ToString()+";";
    sb.Append(strtxt);    
}

 string filepathEmail = Server.MapPath("Email");
 using (StreamWriter outfile = new StreamWriter(filepathEmail + "\\" + "Email.txt"))
 {
     outfile.Write(sb.ToString());
 }

now data is getting stored in text file like this:

abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com; abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;abc@gmail.com;ab@gmail.com;

But i need to store them like where every row should only only 10 email 开发者_如何学JAVAid, so that i looks good**

any idea how to format the data like this in .txt file? any help would be great


Just add a counter in your loop and append a line break every 10 lines.

int counter = 0;
StringBuilder sb = new StringBuilder();
foreach (DataRow dr2 in dtResult.Rows)
{
   counter++;
   strtxt = dr2[strMailID].ToString()+";";
   sb.Append(strtxt);
   if (counter % 10 == 0)
   {
     sb.Append(Environment.NewLine);
   }
}


Use a counter and add a line break each tenth item:

StringBuilder sb = new StringBuilder();
int cnt = 0;
foreach (DataRow dr2 in dtResult.Rows) {
  sb.Append(dr2[strMailID]).Append(';');
  if (++cnt == 10) {
    cnt = 0;
    sb.AppendLine();
  }
}
string filepathEmail = Path.Combine(Server.MapPath("Email"), "Email.txt");
File.WriteAllText(filepathEmail, sb.ToString());

Notes:

  • Concatentate strings using the StringBuilder instead of first concatenating and then appending.
  • Use Path.Combine to combine the path and file name, this works on any platform.
  • You can use the File.WriteAllText method to save the string in a single call instead of writing to a StreamWriter.


as it said you may add a "line break" I suggest to add '\t' tab after each address so your file will be CSV format and you can import it in Excel for instance.


Use a counter to keep track of number of mail already written, like this:

        int i = 0;
        foreach (string mail in mails) {
            var strtxt = mail + ";";
            sb.Append(strtxt);
            i++;
            if (i % 10==0)
                sb.AppendLine();
        }

Every 10 mails written, i modulo 10 equals 0, so you put an end line in the string builder. Hope this can help.


Here's an alternate method using LINQ if you don't mind any overheads.

string filepathEmail = Server.MapPath("Email");
using (StreamWriter outfile = new StreamWriter(filepathEmail + "\\" + "Email.txt"))
{
    var rows = dtResult.Rows.Cast<DataRow>(); //make the rows enumerable
    var lines = from ivp in rows.Select((dr2, i) => new {i, dr2})
                group ivp.dr2[strMailID] by ivp.i / 10 into line //group every 10 emails
                select String.Join(";", line); //put them into a string

    foreach (string line in lines)
        outfile.WriteLine(line);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消