将查询数据保存为 csv 文件
我有一个应用程序,可以打开 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您的代码是准确的,否则您不能为代码分配一个常量变量:
filter = saveFileDialog1.FileName;
您将“filter”进一步声明为常量变量:
const string filter = "CSV file (.csv)|.csv| All Files (.)| .”;
尝试一下:
您使用 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:
You use the SavefileDialog property "Filter" to define your list to filter by.