将winform数据导出到.txt文件
我有一个带有两个数据网格和多个文本框的 winform。我想让用户选择将此数据导出到驱动器上他们选择的位置的文本文档中。我还希望预先格式化文本文档,并插入文本框和数据网格中的值。
是否可以使用 StreamWriter 预先格式化 txt 文档?我该如何让用户选择保存此导出文件的位置?
I have a winform with two data grids, and multiple text boxes. I want to give the user the option to export this data to a text document in a location of their choice on their drive. I also want the text document to be pre-formatted, and the values from the text boxes and datagrids to be plugged in.
Is it possible to pre-format a txt document using StreamWriter? And how to I go about giving the user the option of where to save this exported file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须格式化要通过 StreamWriter 写入的字符串。
输出到文件
客户编号:[12345678] 客户名称:[客户名称]
地址:[地址]
其中方括号内的信息是用户通过文本框输入的信息。
You will have to format the string you want to write throught the StreamWriter.
Output to file
Customer number: [12345678] Customer name: [customer name]
Address: [address]
Where the information between square brackets is the information input by the user through the TextBoxes.
我不完全确定预格式化文本文档是什么意思。 StreamWriter 可用于以您指定的任何格式写出数据。这实际上取决于您如何向 StreamWriter 提供数据。例如,如果您希望网格行显示为 csv 的写入每个项目,请添加一个逗号(最后一个元素除外),然后在最后一个元素后面写入一个新行,对所有行重复此操作。如果我遗漏了什么,请告诉我。
至于如何让用户选择保存位置,您应该使用 SaveFileDialog 控件(它应该位于 Visual Studio 的工具箱中)。这将打开资源管理器视图,允许用户选择位置和名称)。有关如何实际使用该类的详细信息,请参阅链接的文档。这是相当简单的
I'm not entirely sure what you mean by pre-format a text document. The StreamWriter can be used to write out in whatever format you specify the data. Its really going to come down to how you provide the data to the StreamWriter. For example if you want your grid rows to appear as csv's write each one out item, add a comma (except for the last element) and then after the last element write a new line, repeat for all rows. If I'm missing something please let me know.
As far as how to give the user the option of where to save you should use the SaveFileDialog control (it should be in your toolbox in Visual Studio). That will open the explorer view that will allow the user to select the location and name). See the linked documentation for details on how to actually use the class. It is fairly straight forward