开发者

creating CSV with C# from seperate strings

开发者 https://www.devze.com 2023-02-24 01:59 出处:网络
I want to build a CSV file from separate strings but since the size of the string is big I\'m trying to avoid from joining the strings into a single string before writing to the file. Meaning, creatin

I want to build a CSV file from separate strings but since the size of the string is big I'm trying to avoid from joining the strings into a single string before writing to the file. Meaning, creating a file and than adding anothe开发者_如何学Cr string into it. can it be done?


Rather than using String go for StringBuilder to build your string.

than flush string in file.


You could use the StreamWriter object http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx

Have a look here as well


If you are using StreamWriter Class then simply call Write method each time you want to write value to file.


Here is a full demo for creating a text file, including a function for appending strings to it after creation.


Refer to this coding.

 StringBuilder Content1 = new StringBuilder();
 Content1.Append("MailFrom:" + FromAdd + Constants.vbCrLf);
 Content1.Append("MailTo:" + EmailID + Constants.vbCrLf);
 Content1.Append("MailCC:" + MailCC + Constants.vbCrLf);
 Content1.Append("MailBCC:" + MailBCC + Constants.vbCrLf);
 Content1.Append("MailSubject:" + MailSubject + RequestNumber + Constants.vbCrLf);
 Content1.Append("MailAttachment:" + MailAttachment + Constants.vbCrLf);
 Content1.Append("MailBody:" + "" + Constants.vbCrLf);
 if (!File.Exists(Application.StartupPath)) {
SW = File.CreateText(Application.StartupPath + "\\" + FinalPath);
using (fs) {
}
Thread.Sleep(1000);
  }
  using (SW) {
SW.Write(Content1.ToString);
 }

Use the above method for creating a text file and writing the content.

0

精彩评论

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