将查询中的数据另存为csv文件 [英] saving data from a query as a csv file

查看:126
本文介绍了将查询中的数据另存为csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不确定为什么。

推荐答案

除非您的代码准确无误,否则您不能为代码指定一个常量变量:

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

filter = saveFileDialog1.FileName;

filter = saveFileDialog1.FileName;

您将 filter声明为常量变量:

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

const string filter = CSV file( .csv)| .csv |所有文件()| ;

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属性 Filter定义列表进行过滤。

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

这篇关于将查询中的数据另存为csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆