将查询中的数据另存为csv文件

编程入门 行业动态 更新时间:2024-10-26 21:22:35
本文介绍了将查询中的数据另存为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文件

本文发布于:2023-10-26 11:44:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:另存为   文件   数据   csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!