使用 C# 从单独的字符串创建 CSV

发布于 2024-11-01 03:50:43 字数 94 浏览 0 评论 0原文

我想从单独的字符串构建一个 CSV 文件,但由于字符串的大小很大,我试图避免在写入文件之前将字符串连接到单个字符串中。意思是,创建一个文件并向其中添加另一个字符串。 可以吗?

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 another string into it.
can it be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

舟遥客 2024-11-08 03:50:43

不要使用 String,而是使用 StringBuilder 来构建字符串。

比刷新文件中的字符串。

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

than flush string in file.

新人笑 2024-11-08 03:50:43

您可以使用 StreamWriter 对象 http://msdn.microsoft.com /en-us/library/system.io.streamwriter.aspx

看看此处出色地

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

Have a look here as well

梦过后 2024-11-08 03:50:43

如果您使用 StreamWriter 类,则只需调用Write 方法。

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

坏尐絯 2024-11-08 03:50:43

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

终难遇 2024-11-08 03:50:43

参考这个编码。

 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);
 }

使用上述方法创建文本文件并写入内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文