将查询数据保存为 csv 文件

发布于 2024-10-28 12:15:40 字数 1083 浏览 1 评论 0原文

我有一个应用程序,可以打开 csv 文件并将所有内容显示到格式化的 datagridview 中。从那里我有一个按钮可以打开另一个包含一系列复选框的表单。复选框具有我们之前打开的csv文件的所有属性,用户应该能够根据他们想要的属性查询文件,然后保存文件。

例如,如果他们只想要一个显示带翅膀的动物的所有条目的文件,则他们只选择翅膀复选框。从那里,您选择保存按钮,它应该保存文件。

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    const string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
    const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
    StreamWriter writer = null;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {        
        filter = saveFileDialog1.FileName;
        writer = new StreamWriter(filter);

        writer.WriteLine(header);
        foreach (Animal animal in animalQuery)
        {
            writer.Write(animal);
        }  
        writer.Close();
    }
}

这是保存按钮的代码,但是下面有错误:

filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter); 

我不知道为什么。

I've got an application which opens a csv file and displays all the contents into a formatted datagridview. From there I have a button which opens another form that contains a series of checkboxes. The check boxes have all the attributes of the csv file we opened before, and the user is supposed to be able to query the file based on witch attributes they want, then save the file.

For example, if they only want a file which displays all the entries for animals with wings, they select the wings check box only. From there, you select the save button and it's supposed to save the file.

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    const string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
    const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
    StreamWriter writer = null;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {        
        filter = saveFileDialog1.FileName;
        writer = new StreamWriter(filter);

        writer.WriteLine(header);
        foreach (Animal animal in animalQuery)
        {
            writer.Write(animal);
        }  
        writer.Close();
    }
}

This is the code for the save button, but there are errors under:

filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter); 

I'm not sure why.

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

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

发布评论

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

评论(1

陈甜 2024-11-04 12:15:40

除非您的代码是准确的,否则您不能为代码分配一个常量变量:

filter = saveFileDialog1.FileName;

您将“filter”进一步声明为常量变量:

const string filter = "CSV file (.csv)|.csv| All Files (.)| .”;

尝试一下:

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
        saveFileDialog1.Filter = filter;
        const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
        StreamWriter writer = null;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filter = saveFileDialog1.FileName;
            writer = new StreamWriter(filter);

            writer.WriteLine(header);

            writer.Close();
        }

您使用 SavefileDialog 属性“过滤器”来定义要过滤的列表。

unless your code is exact, you cannot assign to a constant variable for your code saying:

filter = saveFileDialog1.FileName;

You declared "filter" as a constant variable further up:

const string filter = "CSV file (.csv)|.csv| All Files (.)|.";

Try that:

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
        saveFileDialog1.Filter = filter;
        const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
        StreamWriter writer = null;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filter = saveFileDialog1.FileName;
            writer = new StreamWriter(filter);

            writer.WriteLine(header);

            writer.Close();
        }

You use the SavefileDialog property "Filter" to define your list to filter by.

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