wpf 将列写入 CSV 文件 c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16197777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:40:26  来源:igfitidea点击:

Writing columns to a CSV file c#

c#wpf

提问by user2220794

is there a way to export data to a specific column in a CSV file? I have data exporting to appending rows but need to specify which columns to save to.

有没有办法将数据导出到 CSV 文件中的特定列?我有数据导出到附加行,但需要指定要保存到哪些列。

my saving code is:

我的保存代码是:

{
StreamWriter sw = File.AppendText("log.csv");                
            {

                    //need this in column 1
                    sw.WriteLine(TextBox21.Text + ",");
                    //need this in column 2
                    sw.WriteLine(TextBox41.Text + ",");

            }

            sw.Close();

回答by Timmerz

yes, like so:

是的,像这样:

var columns = new string[10];

columns[5] = "hello";

sw.WriteLine( string.Join( ",", columns ));

回答by Dour High Arch

Creating CSV output is a solved problem and you should not have to create your own writer. Naive "solutions" like appending commas will break as soon as you start getting data with commas, quotes, newlines, other encodings, and such.

创建 CSV 输出是一个已解决的问题,您不必创建自己的编写器。一旦您开始使用逗号、引号、换行符、其他编码等获取数据,像附加逗号这样的幼稚“解决方案”就会中断。

There are many working, debugged, documented, high-performance CSV writer ready for you to use, such as FastCSV, CSVHelper, LINQ to CSV and others. I have used FastCSV and found it simple to use, reliable, and performant.

有许多工作、调试、记录、高性能的 CSV 编写器可供您使用,例如FastCSVCSVHelper、 LINQ to CSV 等。我使用过 FastCSV 并发现它易于使用、可靠且高性能。

回答by Fls'Zen

The problem with your code is that you're calling WriteLinetwice, which results in each value appearing as a row in the table. The least amount of code change to make it put them both in the same row is to change the first WriteLineto a Write. And, there's no need to include the trailing comma.

您的代码的问题在于您调用了WriteLine两次,这导致每个值在表中显示为一行。将它们放在同一行的最少代码更改是将第WriteLine一个更改为 a Write。而且,不需要包含尾随逗号。

StreamWriter sw = File.AppendText("log.csv");                
{
    sw.Write(TextBox21.Text + ",");  // Column 1
    sw.WriteLine(TextBox41.Text);    // Column 2
}
sw.Close();

If you need to leave certain columns blank (to skip ahead to a later column) then just output additional commas without values.

如果您需要将某些列留空(以跳到后面的列),则只需输出不带值的附加逗号。

回答by Joel Coehoorn

Just change your first .WriteLine()to .Write().

只需将您的第一个更改.WriteLine().Write().

And while you're at it, you probably want to encase those text fields in quotes. If someone puts a comma in one of those fields it will throw your log reader off later on.

当您在使用它时,您可能希望将这些文本字段括在引号中。如果有人在这些字段之一中输入逗号,它会在稍后关闭您的日志阅读器。

回答by MatthewMartin

I'm interpreting your question as "how do I update/add a column in an already existing csv file".

我将您的问题解释为“如何在现有的 csv 文件中更新/添加列”。

You can use the oledb driver for text:

您可以将 oledb 驱动程序用于文本:

http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom

http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom

You may have to read the file in as a datatable, update the datatable's column and then re-export it as csv if the oledb driver doesn't support update (It might, I'm sometimes surprised by how much is supported by text drivers)

您可能必须将文件作为数据表读入,更新数据表的列,然后如果 oledb 驱动程序不支持更新,则将其重新导出为 csv(可能,有时我会惊讶于文本驱动程序支持多少)