如何利用 StreamWriter 写入 csv 文件?
这就是我正在做的事情。我正在尝试获取一个 XML 文件,从属性中提取信息,将它们附加在一起,然后将其写入 CSV 文件。我对编程还比较陌生,而另一位程序员今天不在办公室,所以我确实需要一些帮助。
我的第一个问题是关于 StringBuilder 的。我是否需要在 StringBuilder 的末尾添加一个 AppendLine,以便 foreach 循环输出的每个字符串都位于新行上?我需要在 foreach 循环内执行此操作吗?
我的第二个问题是关于将我的字符串实际写入 CSV 文件。它看起来会像什么吗?
swOutputFile.WriteLine(strAppendedJobData)
我认为这也会进入 foreach 循环,但我不太确定。
感谢您的帮助,我希望我以一种易于理解的方式表达了我的问题。
//Create a stream writer to write the data from returned XML job ticket to a new CSV
StreamWriter swOutputFile;
string strComma = ",";
swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read));
//Get nodes from returned XML ticket
XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
//Pull out data from XML attributes
foreach (XmlElement xeJobUpdate in xmlJobs)
{
//Break down the job data
string strProjectID = xeJobUpdate.GetAttribute("SharpOwlProjectID");
string strJobNumber = xeJobUpdate.GetAttribute("JobNumber");
string strClientCode = xeJobUpdate.GetAttribute("SharpOwlClientCode");
string strClient = xeJobUpdate.GetAttribute("Client");
string strVCAOffice = xeJobUpdate.GetAttribute("VCAOffice");
string strLoadStatus = xeJobUpdate.GetAttribute("LoadStatus");
//Build the string to be added to the new CSV file
StringBuilder sbConcatJob = new StringBuilder();
sbConcatJob.Append(strProjectID).Append(strComma).Append(strJobNumber)
.Append(strComma).Append(strClientCode).Append(strComma).Append(strClient).Append(strComma)
.Append(strVCAOffice).Append(strComma).Append(strLoadStatus).Append(strComma);
string strAppendedJobData = sbConcatJob.ToString();
So here's what I'm working with. I'm trying to take an XML file, pull the info from the attributes, append them together, and write it to a CSV file. I'm still relatively new to programming, and the other programmer is out of the office today, so I could really use some assistance.
My first question, regards the StringBuilder. Do I need to have an AppendLine at the end of my StringBuilder, so that each string output from the foreach loop is on a new line? And would I need to do that inside the foreach loop?
My second question regards actually writing my string to the CSV file. Would it look something like?
swOutputFile.WriteLine(strAppendedJobData)
And I think this would also go inside the foreach loop, but I'm not too sure.
Thanks for the help, I hope I've worded my question in a somewhat easy to understand manner.
//Create a stream writer to write the data from returned XML job ticket to a new CSV
StreamWriter swOutputFile;
string strComma = ",";
swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read));
//Get nodes from returned XML ticket
XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
//Pull out data from XML attributes
foreach (XmlElement xeJobUpdate in xmlJobs)
{
//Break down the job data
string strProjectID = xeJobUpdate.GetAttribute("SharpOwlProjectID");
string strJobNumber = xeJobUpdate.GetAttribute("JobNumber");
string strClientCode = xeJobUpdate.GetAttribute("SharpOwlClientCode");
string strClient = xeJobUpdate.GetAttribute("Client");
string strVCAOffice = xeJobUpdate.GetAttribute("VCAOffice");
string strLoadStatus = xeJobUpdate.GetAttribute("LoadStatus");
//Build the string to be added to the new CSV file
StringBuilder sbConcatJob = new StringBuilder();
sbConcatJob.Append(strProjectID).Append(strComma).Append(strJobNumber)
.Append(strComma).Append(strClientCode).Append(strComma).Append(strClient).Append(strComma)
.Append(strVCAOffice).Append(strComma).Append(strLoadStatus).Append(strComma);
string strAppendedJobData = sbConcatJob.ToString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想做得更优雅一点,你可以这样做:
if you want to do it a bit more elegant you could do something like that:
我唯一的建议是尝试一下,因为看来您还没有尝试过。这是你学习的唯一方法。
这会将整行文本写入文件。
My only advice since it appears you have not attempted this would be to try it. It is the only way you will learn.
This would write an entire line of text to a file.
这里确实有两个选择:
如果您在 foreach 循环内调用 sbConcatJob.AppendLine() ,您可以在一个字符串生成器中构建文件的内容,然后调用 swOutputFile.Write(sbConcatJob.ToString ()) 在 foreach 循环之外写入文件。
如果您保持代码不变,则可以在 foreach 循环内添加 sw.OutputFile.WriteLine(sbConcatJob.ToString()) 并一次写入文件一行。
You really have two options here:
If you call
sbConcatJob.AppendLine()
inside the foreach loop you can build the contents of the file in one string builder then callswOutputFile.Write(sbConcatJob.ToString())
outside of the foreach loop to write the file.If you keep your code as it is now you can add
sw.OutputFile.WriteLine(sbConcatJob.ToString())
inside the foreach loop and write the file one line at a time.